您好,登錄后才能下訂單哦!
小編給大家分享一下利用java實現貪吃蛇小游戲的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
GUI編程實現貪吃蛇小游戲
1、編寫主方法實現啟動類
2、準備好素材圖片,編寫數據類
3、代碼主體部分:在panel面板上實現游戲初始化,鍵盤和事件的監聽等功能
4、代碼運行效果圖
5、GitHub源碼鏈接
1、編寫主方法實現啟動類
import javax.swing.*; //主啟動類 public class StartGame { public static void main(String[] args) { JFrame jFrame = new JFrame("貪吃蛇小游戲"); jFrame.setBounds(10,10,900,720); jFrame.setResizable(false); //設置窗口大小不可變 jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //面板 jFrame.add(new GamePanel()); jFrame.setVisible(true); } }
2、準備好素材圖片,編寫數據類
import javax.swing.*; import java.net.URL; public class Data { //頭部圖片 public static URL headerURL = Data.class.getResource("statics/header.png"); public static ImageIcon header = new ImageIcon(headerURL); //頭部上下左右 public static URL upURL = Data.class.getResource("statics/up.png"); public static URL downURL = Data.class.getResource("statics/down.png"); public static URL leftURL = Data.class.getResource("statics/left.png"); public static URL rightURL = Data.class.getResource("statics/right.png"); public static ImageIcon up = new ImageIcon(upURL); public static ImageIcon down = new ImageIcon(downURL); public static ImageIcon left = new ImageIcon(leftURL); public static ImageIcon right = new ImageIcon(rightURL); //身體 public static URL bodyURL = Data.class.getResource("statics/body.png"); public static ImageIcon body = new ImageIcon(bodyURL); //食物 public static URL foodURL = Data.class.getResource("statics/food.png"); public static ImageIcon food = new ImageIcon(foodURL); }
3、代碼主體部分:在panel面板上實現游戲初始化,鍵盤和事件的監聽等功能
package com.abc.snake; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import java.util.Random; //游戲的面板 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 fx; //食物 int foodx; int foody; Random random = new Random(); int score; //游戲分數 //游戲當前的狀態 boolean isStart = false; boolean isFail = false; //定時器 Timer timer = new Timer(100,this);//100毫秒刷新一次 //構造方法 public GamePanel() { init();//初始化 this.setFocusable(true); //獲得焦點事件 this.addKeyListener(this); //獲得鍵盤監聽事件 timer.start(); //游戲一開始 定時器就啟動 } //初始化方法 public 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; } //繪制面板 @Override protected void paintComponent(Graphics g) { super.paintComponent(g);//清屏 this.setBackground(Color.white);//設置面板背景色 Data.header.paintIcon(this,g,25,11);//頭部 g.fillRect(25,75,850,600);//默認的黑色游戲區域 //繪制小蛇 if (fx.equals("R")){ Data.right.paintIcon(this,g,snakeX[0],snakeY[0]); //蛇頭初始化向右 }else if (fx.equals("L")){ Data.left.paintIcon(this,g,snakeX[0],snakeY[0]); //蛇頭初始化向左 }else if (fx.equals("U")){ Data.up.paintIcon(this,g,snakeX[0],snakeY[0]); //蛇頭初始化向上 }else if (fx.equals("D")){ Data.down.paintIcon(this,g,snakeX[0],snakeY[0]); //蛇頭初始化向下 } for (int i = 1; i < length; i++) { Data.body.paintIcon(this,g,snakeX[i],snakeY[i]); } //食物 Data.food.paintIcon(this,g,foodx,foody); //積分 g.setColor(Color.white); g.setFont(new Font("微軟雅黑",Font.BOLD,18)); g.drawString("長度 "+length,750,35); g.drawString("分數 "+score,750,50); //游戲狀態 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(); } //鍵盤控制小蛇移動 if (keyCode==KeyEvent.VK_UP){ fx="U"; }else if (keyCode==KeyEvent.VK_DOWN){ fx="D"; }else if (keyCode==KeyEvent.VK_LEFT){ fx="L"; }else if (keyCode==KeyEvent.VK_RIGHT){ fx="R"; } } //事件監聽 @Override public void actionPerformed(ActionEvent e) { if (isStart && isFail ==false){//如果游戲是開始狀態,就讓小蛇動起來 //移動 for (int i = length-1; i > 0 ; i--) { snakeX[i] = snakeX[i-1]; snakeY[i] = snakeY[i-1]; } //走向 if (fx.equals("R")){ snakeX[0] = snakeX[0]+25; //邊界判斷 if (snakeX[0]>850){ snakeX[0]=25; } }else if (fx.equals("L")){ snakeX[0] = snakeX[0]-25; if (snakeX[0]<25){ snakeX[0]=850; } }else if (fx.equals("U")){ snakeY[0] = snakeY[0]-25; if (snakeY[0]<75){ snakeY[0]=650; } }else if (fx.equals("D")){ snakeY[0] = snakeY[0]+25; if (snakeY[0]>650){ snakeY[0]=75; } } //吃食物 if (snakeX[0] == foodx && snakeY[0] == foody){ length++; score = score + 10; //再次隨機食物 foodx = 25 + 25*random.nextInt(34); foody = 75 + 25*random.nextInt(24); } //失敗判定,撞到自己 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) { } }
4、代碼運行效果圖
初始化界面:
游戲中界面:
游戲結束界面:
看完了這篇文章,相信你對利用java實現貪吃蛇小游戲的方法有了一定的了解,想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。