用GUI實現(xiàn)java版貪吃蛇小游戲
本文實例為大家分享了java版貪吃蛇小游戲的具體代碼,供大家參考,具體內(nèi)容如下
項目結(jié)構(gòu)
新建一個JFrame窗口,作為程序入口
public class GameStart{ public static void main(String[] args) { JFrame jFrame = new JFrame(); jFrame.setBounds(100,100,900,720); jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); jFrame.setResizable(false); jFrame.add(new GameJPanel()); jFrame.setVisible(true); }}
創(chuàng)建一個面板類
//面板public class GameJPanel extends JPanel implements ActionListener { int length;//蛇的長度 int[] snakeX = new int[600];//蛇的X的最大長度 int[] snakeY = new int[500];//蛇的Y的最大長度 String fx = 'R'; boolean isStart = false;//默認不開始 //定時器 Timer timer = new Timer(100, this);//100毫秒=1秒 int foodX; int foodY; Random random = new Random();//隨機數(shù) boolean isFail = false;//失敗條件 int score; public GameJPanel() { init(); this.setFocusable(true);//獲取焦點事件 addKeyListener(new GameKeyListener(this)); //開啟定時器 timer.start(); } //初始化 void init() { length = 3; snakeX[0] = 100; snakeY[0] = 100;//第一個身體 snakeX[1] = 75; snakeY[1] = 100;//第二個身體 snakeX[2] = 50; snakeY[2] = 100;//第三個身體 fx = 'R'; //食物隨機分布 foodX = 25 + 25 * random.nextInt(34); foodY = 75 + 25 * random.nextInt(24); score = 0; } //繪制面板,所有東西都是通過graphics這個畫筆繪制 @Override protected void paintComponent(Graphics graphics) { super.paintComponent(graphics);//清屏 //添加靜態(tài)布局 GameData.header.paintIcon(this, graphics, 25, 11); graphics.fillRect(25, 75, 850, 600); //積分板 graphics.setColor(Color.white); graphics.setFont(new Font('微軟雅黑', Font.BOLD, 18)); graphics.drawString('長度:'+length,750,35); graphics.drawString('得分:'+score,750,55); //先畫食物,防止吃掉有延遲 GameData.food.paintIcon(this, graphics, foodX, foodY); //畫小蛇 switch (fx) { case 'R': GameData.right.paintIcon(this, graphics, snakeX[0], snakeY[0]); break; case 'L': GameData.left.paintIcon(this, graphics, snakeX[0], snakeY[0]); break; case 'U': GameData.up.paintIcon(this, graphics, snakeX[0], snakeY[0]); break; case 'D': GameData.down.paintIcon(this, graphics, snakeX[0], snakeY[0]); break; } //身體 for (int i = 1; i < length; i++) { GameData.body.paintIcon(this, graphics, snakeX[i], snakeY[i]); } //游戲狀態(tài) if (!isStart) { graphics.setColor(Color.white); graphics.setFont(new Font('微軟雅黑', Font.BOLD, 40));//設(shè)置字體 graphics.drawString('按下空格,開始游戲', 300, 300); } //游戲失敗 if (isFail) { graphics.setColor(Color.red); graphics.setFont(new Font('微軟雅黑', Font.BOLD, 40)); graphics.drawString('游戲失敗,請按空格繼續(xù)', 300, 300); } } //事件監(jiān)聽--固定事件刷新一次,1s=100ms @Override public void actionPerformed(ActionEvent actionEvent) { if (isStart && !isFail) { //吃食物 if (snakeX[0] == foodX && snakeY[0] == foodY) { //長度+1 length++; score+=10; //重新隨機繪制食物 foodX = 25 + 25 * random.nextInt(34); foodY = 75 + 25 * random.nextInt(24); } //后一節(jié)移動到前一節(jié),從而由頭帶動身體移動 for (int i = length - 1; i > 0; i--) { snakeX[i] = snakeX[i - 1]; snakeY[i] = snakeY[i - 1]; } switch (fx) { case 'R': snakeX[0] += 25;//頭部移動 // 邊界判斷 if (snakeX[0] > 850) { snakeX[0] = 25; } break; case 'L': snakeX[0] -= 25;//頭部移動 if (snakeX[0] < 25) { snakeX[0] = 850; } break; case 'U': snakeY[0] -= 25;//頭部移動 if (snakeY[0] < 75) { snakeY[0] = 650; } break; case 'D': snakeY[0] += 25;//頭部移動 if (snakeY[0] > 650) { snakeY[0] = 75; } break; } //失敗判定 for (int i = 1; i < length; i++) { if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) { isFail = true; break; } } repaint();//重繪 } //開啟定時器 timer.start(); }}
所有組件添加流程基本一致,即先定義數(shù)據(jù),然后在畫筆paintComponent方法中繪制,最后添加到監(jiān)聽事件。
完整代碼請參考:GitHub完整貪吃蛇小游戲鏈接
更多有趣的經(jīng)典小游戲?qū)崿F(xiàn)專題,分享給大家:
C++經(jīng)典小游戲匯總
python經(jīng)典小游戲匯總
python俄羅斯方塊游戲集合
JavaScript經(jīng)典游戲 玩不停
java經(jīng)典小游戲匯總
javascript經(jīng)典小游戲匯總
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. php使用正則驗證密碼字段的復(fù)雜強度原理詳細講解 原創(chuàng)2. 基于PHP做個圖片防盜鏈3. jscript與vbscript 操作XML元素屬性的代碼4. Jsp+Servlet實現(xiàn)文件上傳下載 文件列表展示(二)5. XML在語音合成中的應(yīng)用6. Jsp servlet驗證碼工具類分享7. ASP將數(shù)字轉(zhuǎn)中文數(shù)字(大寫金額)的函數(shù)8. asp.net core 認證和授權(quán)實例詳解9. 基于javaweb+jsp實現(xiàn)企業(yè)車輛管理系統(tǒng)10. HTML5實戰(zhàn)與剖析之觸摸事件(touchstart、touchmove和touchend)
