用GUI實現java版貪吃蛇小游戲
本文實例為大家分享了java版貪吃蛇小游戲的具體代碼,供大家參考,具體內容如下
項目結構
新建一個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); }}
創建一個面板類
//面板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();//隨機數 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);//清屏 //添加靜態布局 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]); } //游戲狀態 if (!isStart) { graphics.setColor(Color.white); graphics.setFont(new Font('微軟雅黑', Font.BOLD, 40));//設置字體 graphics.drawString('按下空格,開始游戲', 300, 300); } //游戲失敗 if (isFail) { graphics.setColor(Color.red); graphics.setFont(new Font('微軟雅黑', Font.BOLD, 40)); graphics.drawString('游戲失敗,請按空格繼續', 300, 300); } } //事件監聽--固定事件刷新一次,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); } //后一節移動到前一節,從而由頭帶動身體移動 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(); }}
所有組件添加流程基本一致,即先定義數據,然后在畫筆paintComponent方法中繪制,最后添加到監聽事件。
完整代碼請參考:GitHub完整貪吃蛇小游戲鏈接
更多有趣的經典小游戲實現專題,分享給大家:
C++經典小游戲匯總
python經典小游戲匯總
python俄羅斯方塊游戲集合
JavaScript經典游戲 玩不停
java經典小游戲匯總
javascript經典小游戲匯總
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: