Java實現簡易GUI貪吃蛇小遊戲
本文實例為大傢分享瞭Java實現貪吃蛇小遊戲的具體代碼,供大傢參考,具體內容如下
主啟動類
import javax.swing.*; /** * @author The Setting sun * @create 2021-09-11 15:19 */ //主啟動類 public class StartGame { public static void main(String[] args) { JFrame frame = new JFrame(); frame.setResizable(false);//窗口大小不可變 frame.setSize(900, 720);//窗口大小 frame.setLocationRelativeTo(null);//窗口居中 frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//窗口關閉事件 frame.add(new GamePanel()); frame.setVisible(true);//窗口可見 } }
資源類(圖標)
import javax.swing.*; import java.net.URL; /** @author The Setting sun @create 2021-09-11 15:40 */ public class Data { public static URL headerURL = Data.class.getResource("img/header.png"); public static ImageIcon header = new ImageIcon(headerURL); public static URL upURL = Data.class.getResource("img/tx.png"); public static ImageIcon up = new ImageIcon(upURL); public static URL downURL = Data.class.getResource("img/down.png"); public static ImageIcon down = new ImageIcon(downURL); public static URL rightURL = Data.class.getResource("img/right.png"); public static ImageIcon right = new ImageIcon(rightURL); public static URL leftURL = Data.class.getResource("img/left.png"); public static ImageIcon left = new ImageIcon(leftURL); public static URL bodyURL = Data.class.getResource("img/body.png"); public static ImageIcon body = new ImageIcon(bodyURL); public static URL foodURL = Data.class.getResource("img/food.png"); public static ImageIcon food = new ImageIcon(foodURL); }
程序類
import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.Random; /** * @author The Setting sun * @create 2021-09-11 15:24 */ //遊戲面板 public class GamePanel extends JPanel implements KeyListener, ActionListener { //蛇 int length;//蛇的長度 int[] snakeX = new int[600];//蛇的x坐標 25*25 int[] snakeY = new int[500];//蛇的y坐標 25*25 String orient;//初始方向 //食物 int foodX; int foodY; Random random = new Random(); boolean isStart = false;//遊戲當前狀態:默認為停止 boolean isFail = false;//遊戲是否失敗瞭,默認為沒有 int score;//積分 //定時器 ms為單位 Timer timer = new Timer(140, this);//100毫秒刷新一次 //構造器 public GamePanel() { init();//初始化 setFocusable(true);//獲得焦點 addKeyListener(this);//獲得鍵盤監聽事件 timer.start();//遊戲一開始定時器就啟動 } //初始化 public void init() { length = 3;//初始長度為3 snakeX[0] = 100; snakeY[0] = 100;//頭的坐標 snakeX[1] = 75; snakeY[1] = 100;//第1個身體的坐標 snakeX[2] = 50; snakeY[2] = 100;//第2個身體的坐標 orient = "R";//初始方向向右 //讓食物隨機分佈在界面上 foodX = 25 + 25 * random.nextInt(34); foodY = 75 + 25 * random.nextInt(24); score = 0;//積分 } //繪制面板,遊戲中的東西都用這個來畫 @Override protected void paintComponent(Graphics g) { super.paintComponent(g);//清屏 setBackground(Color.pink); //繪制靜態面板 Data.header.paintIcon(this, g, 25, 11); g.fillRect(25, 75, 850, 600); //繪制積分面板 g.setColor(Color.GREEN); g.setFont(new Font("微軟雅黑", Font.BOLD, 16)); g.drawString("長度:" + length, 750, 35); g.drawString("積分:" + score, 750, 50); //繪制食物 Data.food.paintIcon(this, g, foodX, foodY); //繪制蛇 switch (orient) { case "R": Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);//蛇初始化向右,通過判斷轉變方向 break; case "L": Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);//蛇初始化向右,通過判斷轉變方向 break; case "U": Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);//蛇初始化向右,通過判斷轉變方向 break; case "D": Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);//蛇初始化向右,通過判斷轉變方向 break; } //動態添加身體 for (int i = 1; i < length; i++) { Data.body.paintIcon(this, g, snakeX[i], snakeY[i]); } // Data.body.paintIcon(this,g,snakeX[1],snakeY[1]);//第1個身體的坐標 // Data.body.paintIcon(this,g,snakeX[2],snakeY[2]);//第2個身體的坐標 //遊戲狀態 if (isStart == false) { g.setColor(Color.WHITE); g.setFont(new Font("微軟雅黑", Font.BOLD, 40)); g.drawString("按下空格開始遊戲", 300, 300); } if (isFail) { g.setColor(Color.RED); g.setFont(new Font("微軟雅黑", Font.BOLD, 40)); g.drawString("猶豫就會敗北!", 300, 300); } } //鍵盤監聽器 @Override public void keyPressed(KeyEvent e) {//按下 int keyCode = e.getKeyCode();//獲得鍵盤按鍵碼 if (keyCode == KeyEvent.VK_SPACE) {//按下空格 if (isFail) { //重新開始 isFail = false; init(); } else { isStart = !isStart;//取反 repaint(); } } //修復瞭一個蛇可以反向移動的bug if (orient.equals("U") && keyCode == KeyEvent.VK_DOWN){ keyCode = KeyEvent.VK_UP; } if (orient.equals("D") && keyCode == KeyEvent.VK_UP){ keyCode = KeyEvent.VK_DOWN; } if (orient.equals("L") && keyCode == KeyEvent.VK_RIGHT){ keyCode = KeyEvent.VK_LEFT; } if (orient.equals("R") && keyCode == KeyEvent.VK_LEFT){ keyCode = KeyEvent.VK_RIGHT; } //控制蛇改變方向移動 switch (keyCode) { case KeyEvent.VK_UP: orient = "U"; break; case KeyEvent.VK_DOWN: orient = "D"; break; case KeyEvent.VK_LEFT: orient = "L"; break; case KeyEvent.VK_RIGHT: orient = "R"; break; } } //事件監聽:需要通過固定事件來刷新,1秒10次(幀率) @Override public void actionPerformed(ActionEvent e) { if (isStart && isFail == false) {//如果遊戲是開始狀態,就讓蛇動起來 //吃食物 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 (orient) { case "R": snakeX[0] = snakeX[0] + 25; //邊界判斷 if (snakeX[0] > 850) { snakeX[0] = 25; } break; case "L": snakeX[0] = snakeX[0] - 25; //邊界判斷 if (snakeX[0] < 25) { snakeX[0] = 850; } break; case "U": snakeY[0] = snakeY[0] - 25; //邊界判斷 if (snakeY[0] < 75) { snakeY[0] = 650; } break; case "D": snakeY[0] = snakeY[0] + 25; //邊界判斷 if (snakeY[0] > 650) { snakeY[0] = 75; } break; } //失敗判斷,撞到自己就GG for (int i = 1 ; i < length; i++) { if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]){ isFail = true; } } repaint();//刷新頁面 } timer.start();//定時器開啟 } @Override public void keyTyped(KeyEvent e) { } @Override public void keyReleased(KeyEvent e) { } }
以上就是本文的全部內容,希望對大傢的學習有所幫助,也希望大傢多多支持WalkonNet。