小編給大家分享一下利用java實現(xiàn)貪吃蛇小游戲的方法,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
成都創(chuàng)新互聯(lián)主要從事成都網(wǎng)站建設(shè)、成都網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)香格里拉,十載網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):028-86922220GUI編程實現(xiàn)貪吃蛇小游戲
1、編寫主方法實現(xiàn)啟動類
2、準(zhǔn)備好素材圖片,編寫數(shù)據(jù)類
3、代碼主體部分:在panel面板上實現(xiàn)游戲初始化,鍵盤和事件的監(jiān)聽等功能
4、代碼運行效果圖
5、GitHub源碼鏈接
1、編寫主方法實現(xiàn)啟動類
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); //設(shè)置窗口大小不可變 jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); //面板 jFrame.add(new GamePanel()); jFrame.setVisible(true); } }
2、準(zhǔn)備好素材圖片,編寫數(shù)據(jù)類
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面板上實現(xiàn)游戲初始化,鍵盤和事件的監(jiān)聽等功能
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 { //定義蛇的數(shù)據(jù)結(jié)構(gòu) int length; //蛇的長度 int[] snakeX = new int[600]; //蛇的x坐標(biāo) 25*25 int[] snakeY = new int[500]; //蛇的y坐標(biāo) 25*25 String fx; //食物 int foodx; int foody; Random random = new Random(); int score; //游戲分?jǐn)?shù) //游戲當(dāng)前的狀態(tài) boolean isStart = false; boolean isFail = false; //定時器 Timer timer = new Timer(100,this);//100毫秒刷新一次 //構(gòu)造方法 public GamePanel() { init();//初始化 this.setFocusable(true); //獲得焦點事件 this.addKeyListener(this); //獲得鍵盤監(jiān)聽事件 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"; //初始方向向右 //初始化食物數(shù)據(jù) foodx = 25 + 25*random.nextInt(34); foody = 75 + 25*random.nextInt(24); //初始化游戲分?jǐn)?shù) score = 0; } //繪制面板 @Override protected void paintComponent(Graphics g) { super.paintComponent(g);//清屏 this.setBackground(Color.white);//設(shè)置面板背景色 Data.header.paintIcon(this,g,25,11);//頭部 g.fillRect(25,75,850,600);//默認(rèn)的黑色游戲區(qū)域 //繪制小蛇 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("分?jǐn)?shù) "+score,750,50); //游戲狀態(tài) if (isStart == false){ g.setColor(Color.white); g.setFont(new Font("微軟雅黑",Font.BOLD,40));//設(shè)置字體 g.drawString("按下空格開始游戲",300,300); } //失敗判斷 if (isFail){ g.setColor(Color.red); g.setFont(new Font("微軟雅黑",Font.BOLD,40));//設(shè)置字體 g.drawString("游戲失敗,按下空格重新開始",300,300); } } //鍵盤監(jiān)聽事件 @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"; } } //事件監(jiān)聽 @Override public void actionPerformed(ActionEvent e) { if (isStart && isFail ==false){//如果游戲是開始狀態(tài),就讓小蛇動起來 //移動 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、代碼運行效果圖
初始化界面:
游戲中界面:
游戲結(jié)束界面:
看完了這篇文章,相信你對利用java實現(xiàn)貪吃蛇小游戲的方法有了一定的了解,想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝各位的閱讀!