<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Java游戲:簡單的貪吃蛇小游戲

          共 17972字,需瀏覽 36分鐘

           ·

          2021-04-22 13:31

          點擊上方藍色字體,選擇“標星公眾號”

          優(yōu)質(zhì)文章,第一時間送達

          1. 程序結(jié)構(gòu)

          ??程序結(jié)構(gòu)圖如圖:


          2. 程序設(shè)計思路

          2.1 Data類

          • 作用:連接statics文件夾,將靜態(tài)資源包中的圖片轉(zhuǎn)化為圖標 方便在面板上繪制。

          • 實現(xiàn):使用class.getResource(String path)方法。

          ?? 代碼如下:

          package com.snake;
          import javax.swing.*;
          import java.net.URL;
          public class Data {
              //貪吃蛇頭部
              public static URL upUrl = Data.class.getResource("/statics/up.png");
              public static ImageIcon up = new ImageIcon(upUrl);
              public static URL downUrl = Data.class.getResource("/statics/down.png");
              public static ImageIcon down = new ImageIcon(downUrl);
              public static URL leftUrl = Data.class.getResource("/statics/left.png");
              public static ImageIcon left = new ImageIcon(leftUrl);
              public static URL rightUrl = Data.class.getResource("/statics/right.png");
              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);
          }

          2.2 StartGame類

          • 作用:創(chuàng)建游戲窗口,在窗口中添加一個游戲面板。

          • 實現(xiàn):使用JFrame類創(chuàng)建游戲窗口,利用其add()方法添加一個GamePanel類實例化對象。

          ?? 代碼如下:

          package com.snake;
          import javax.swing.*;
          import java.awt.*;
          public class StartGame {
              public static void main(String[] args){
                  //建立游戲窗口
                  JFrame frame = new JFrame("Java-貪吃蛇小游戲");//標題
                  frame.setSize(900,720);//窗口大小
                  frame.setLocationRelativeTo(null);//窗口顯示屏幕中間
                  frame.setResizable(false);//固定窗口大小
                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//設(shè)置窗體關(guān)閉事件
                  frame.add(new GamePanel());//添加游戲內(nèi)容
                  frame.setVisible(true);//設(shè)置窗體可見
              }
          }

          2.3 GamePanel類

          • 作用:實現(xiàn)游戲的動態(tài)頁面。

          • 實現(xiàn):(1)init()方法:初始化小蛇位置;

                     (2)eat()方法:用隨機種子隨機食物的位置,并進行判定,食物位置不能和小蛇位置重合;

                     (3)繼承JPanel類,重寫paintComponent(Graphics g)方法,在方法中繪制標題欄、小蛇的位置(根據(jù)direction小蛇頭部方向變量繪制小蛇頭部)、小蛇身體、積分欄、游戲提醒項與失敗判斷項;

                     (4)實現(xiàn)KeyListener 接口中的keyPressed(KeyEvent e)方法,獲取鍵盤輸入,根據(jù)鍵盤輸入對游戲狀態(tài)或者小蛇頭部方向direction變量進行更改;

                     (5)實現(xiàn)ActionListener接口中的actionPerformed(ActionEvent e)方法,根據(jù)游戲狀態(tài)和direction變量進行小蛇移動操作(注意禁用直接回頭操作),進行吃食物判定和死亡判定。使用Timer計時器讓游戲動態(tài)變化,用repaint()方法實時更新界面。

          ?? 代碼如下:

          package com.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[] snakeX = new int[500];//貪吃蛇橫坐標
              int[] snakeY = new int[500];//貪吃蛇縱坐標
              int foodX;//食物橫坐標
              int foodY;//食物蛇縱坐標
              int length;//貪吃蛇的長度
              String  direction;//貪吃蛇頭方向
              int score;//積分
              Random r = new Random();
              Timer timer = new Timer(100,this);
              boolean isStart;
              boolean isFail;
              //構(gòu)造函數(shù)
              public GamePanel(){
                  init();
                  this.setFocusable(true);
                  this.addKeyListener(this);
                  timer.start();
              }
              private void init(){
                  length=3;
                  snakeX[0]=100;snakeY[0]=100;
                  snakeX[1]=75;snakeY[1]=100;
                  snakeX[2]=50;snakeY[2]=100;
                  direction = "R";
                  eat(foodX,foodY);
                  isStart = false;
                  isFail = false;
                  score = 0;

              }
              private void eat(int x,int y){
                  x= 25 + 25*r.nextInt(34);
                  y= 75 + 25*r.nextInt(24);
                  for (int i = 0; i < length; i++) {
                      if(snakeX[i]==x&&snakeY[i]==y){
                          x = 25 + 25*r.nextInt(34);
                          y = 75 + 25*r.nextInt(24);
                      }
                  }
                  foodX = x;foodY = y;
              }
              protected void paintComponent(Graphics g) {
                  super.paintComponent(g);
                  this.setBackground(Color.white);//設(shè)置背景板為白色
                  //畫標題
                  g.setColor(Color.GREEN);
                  g.setFont(new Font("幼圓",Font.BOLD,50));
                  g.drawString("貪吃蛇游戲",300,60);
                  //繪制游戲區(qū)域
                  g.setColor(Color.GRAY);
                  g.fillRect(25,75,850,600);
                  //畫貪吃蛇頭部
                  if(direction=="R"){
                      Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
                  }
                  else if(direction=="L"){
                      Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
                  }
                  if(direction=="U"){
                      Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
                  }
                  else if(direction=="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.BLACK);
                  g.setFont(new Font("幼圓",Font.BOLD,20));
                  g.drawString("長度:"+length,730,30);
                  g.drawString("得分:"+score,730,60);
                  //游戲開始提醒
                  if(isStart==false){
                      g.setColor(Color.BLACK);
                      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_LEFT&&direction!="R"){
                      direction = "L";
                  }
                  else if(keyCode==KeyEvent.VK_RIGHT&&direction!="L"){
                      direction = "R";
                  }
                  else if(keyCode==KeyEvent.VK_UP&&direction!="D"){
                      direction = "U";
                  }
                  else if(keyCode==KeyEvent.VK_DOWN&&direction!="U"){
                      direction = "D";
                  }
              }
              @Override
              public void keyReleased(KeyEvent e) {

              }
              @Override
              public void keyTyped(KeyEvent e) {
              }


              @Override
              public void actionPerformed(ActionEvent e) {
                  //判斷游戲狀態(tài)
                  if(isStart&&!isFail){
                      //移動身體
                      for (int i = length-1; i > 0 ; i--) {
                          snakeX[i] = snakeX[i-1];
                          snakeY[i] = snakeY[i-1];
                      }
                      //移動頭部
                      if(direction=="R"){
                          snakeX[0] += 25;
                          if(snakeX[0]>850){
                              snakeX[0] = 25;
                          }
                      }
                      else  if(direction=="L"){
                          snakeX[0] -= 25;
                          if(snakeX[0]<25){
                              snakeX[0] = 850;
                          }
                      }
                      else  if(direction=="U"){
                          snakeY[0] -= 25;
                          if(snakeY[0]<75){
                              snakeY[0] = 650;
                          }
                      }
                      else  if(direction=="D"){
                          snakeY[0] += 25;
                          if(snakeY[0]>650){
                              snakeY[0] = 75;
                          }
                      }
                      //吃食物
                      if(snakeX[0]==foodX&&snakeY[0]==foodY){
                          length++;
                          score += 10;
                          eat(foodX,foodY);
                      }
                      //死亡判定
                      for (int i = 1; i < length; i++) {
                          if(snakeX[0]==snakeX[i]&&snakeY[0]==snakeY[i]){
                              isFail=true;
                          }
                      }
                      repaint();
                  }
                  timer.start();
              }
          }



          3. 游戲展示

          ————————————————

          版權(quán)聲明:本文為CSDN博主「just sh!」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。

          原文鏈接:

          https://blog.csdn.net/xfjssaw/article/details/115708947





          鋒哥最新SpringCloud分布式電商秒殺課程發(fā)布

          ??????

          ??長按上方微信二維碼 2 秒





          感謝點贊支持下哈 

          瀏覽 89
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  成人网站视频大香蕉 | 欧美大片豆花视频免费观看网站 | 美日韩黄色| 大鸡巴操嫩逼 | 精品人妻一区二区免费蜜桃视频 |