JavaGUI编程--贪吃蛇小游戏代码(狂神学Java系列)

添加游戏功能点的四个步骤

1.定义数据

2.画上去

3.监听事件

​ 键盘

​ 事件

 

StartGame.java

 1 package com.kuang.snake;
 2 
 3 import javax.swing.*;
 4 
 5 //游戏的主启动类
 6 public class StartGame {
 7     public static void main(String[] args) {
 8         JFrame frame = new JFrame();
 9 
10         frame.setBounds(10,10,915,720);
11         frame.setResizable(false);//窗口大小不可变
12         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
13         //正常游戏界面都应该在面上!
14 
15         frame.add(new GamePanel());
16         frame.setVisible(true);
17 
18 
19     }
20 }

 

GamePanel.java

  1 package com.kuang.snake;
  2 
  3 import javax.swing.*;
  4 import java.awt.*;
  5 import java.awt.event.ActionEvent;
  6 import java.awt.event.ActionListener;
  7 import java.awt.event.KeyEvent;
  8 import java.awt.event.KeyListener;
  9 import java.util.Random;
 10 
 11 //游戏的面板
 12 public class GamePanel extends JPanel implements KeyListener, ActionListener {
 13 
 14     //定义蛇的数据结构
 15     int length;
 16     int[] snakeX = new int[600]; //蛇的X坐标 25*25
 17     int[] snakeY = new int[500]; //蛇的Y坐标 25*25
 18     String fx ; //初始化方向向右
 19 
 20     //食物的坐标
 21     int foodx;
 22     int foody;
 23     Random random = new Random();
 24 
 25     //成绩
 26     int score;
 27 
 28     //游戏当前的状态: 开始,停止
 29     boolean isStart = false; //默认是不开始;
 30 
 31     boolean isFail = false; //游戏失败状态
 32 
 33     //定时器   以毫秒为单位
 34     Timer timer = new Timer(100,this);// 100毫秒刷新一次
 35 
 36     public GamePanel() {
 37         init();
 38         this.setFocusable(true);//获得焦点事件
 39         this.addKeyListener(this);//获得键盘监听事件
 40         timer.start();
 41     }
 42 
 43     public void init(){
 44         length = 3;
 45         snakeX[0] = 100; snakeY[0] = 100; //脑袋的坐标
 46         snakeX[1] = 75; snakeY[1] = 100; //脑袋的坐标
 47         snakeX[2] = 50; snakeY[2] = 100; //脑袋的坐标
 48         fx = "R";
 49         foodx = 25 + 25*random.nextInt(34);
 50         foody = 75 + 25*random.nextInt(24);
 51         score = 0;
 52     }
 53 
 54 
 55     //绘制面板,我们游戏中的所有东西,都是用这个画笔来画
 56     @Override
 57     protected void paintComponent(Graphics g) {
 58         super.paintComponent(g);//清屏
 59         //绘制静态的面板
 60         this.setBackground(Color.white);
 61         Data.header.paintIcon(this,g,25,11);//头部广告栏画上去
 62         g.fillRect(25,75,850,600);//默认的游戏界面
 63 
 64         //画积分
 65         g.setColor(Color.white);
 66         g.setFont(new Font("微软雅黑",Font.BOLD,40)); //设置字体
 67         g.drawString("长度 "+length,750,30);
 68         g.drawString("分数"+score,750,55);
 69 
 70         Data.food.paintIcon(this,g,foodx,foody);
 71 
 72         //把小蛇画上去
 73         if(fx.equals("R")){
 74             Data.right.paintIcon(this,g,snakeX[0],snakeY[0]);
 75         }else if(fx.equals("L")){
 76             Data.left.paintIcon(this,g,snakeX[0],snakeY[0]);
 77         }else if(fx.equals("U")){
 78             Data.up.paintIcon(this,g,snakeX[0],snakeY[0]);
 79         }else if(fx.equals("D")){
 80             Data.down.paintIcon(this,g,snakeX[0],snakeY[0]);
 81         }
 82 
 83         for(int i=1; i<length; i++){
 84             Data.body.paintIcon(this,g,snakeX[i],snakeY[i]);
 85         }
 86 
 87 
 88         //游戏状态
 89         if(isStart == false){
 90             g.setColor(Color.white);
 91             g.setFont(new Font("微软雅黑",Font.BOLD,40)); //设置字体
 92             g.drawString("按下空格开始游戏",300,300);
 93         }
 94 
 95         if(isFail){
 96             g.setColor(Color.RED);
 97             g.setFont(new Font("微软雅黑",Font.BOLD,40)); //设置字体
 98             g.drawString("游戏失败,按下空格重新开始",300,300);
 99         }
100     }
101 
102 
103     //键盘监听事件
104     @Override
105     public void keyPressed(KeyEvent e) {
106         int keyCode = e.getKeyCode();
107         if(keyCode == KeyEvent.VK_SPACE){
108             if(isFail){
109                 //重新开始
110                 isFail = false;
111                 init();
112             }else{
113                 isStart =!isStart; //取反
114             }
115             repaint();
116         }
117         //小蛇移动
118         if(keyCode==KeyEvent.VK_UP){
119             fx="U";
120         }else if(keyCode==KeyEvent.VK_DOWN){
121             fx="D";
122         }else if(keyCode==KeyEvent.VK_LEFT){
123             fx="L";
124         }else if(keyCode==KeyEvent.VK_RIGHT){
125             fx="R";
126         }
127     }
128     @Override
129     public void keyReleased(KeyEvent e) {
130 
131     }
132     @Override
133     public void keyTyped(KeyEvent e) {
134 
135     }
136 
137 
138     //事件监听 -- 需要通过固定事件来
139     @Override
140     public void actionPerformed(ActionEvent e) {
141         if(isStart && isFail==false){//如果游戏是开始装填, 就让小蛇动起来!
142 
143             //吃食物
144             if(snakeX[0] == foodx && snakeY[0] == foody){
145                 //长度+1
146                 length++;
147                 //分数+10
148                 score = score + 10;
149                 //再次随机食物
150                 foodx = 25 + 25*random.nextInt(34);
151                 foody = 75 + 25*random.nextInt(24);
152             }
153 
154             //右移
155             for(int i=length-1;i>0;i--){
156                 snakeX[i] = snakeX[i-1];
157                 snakeY[i] = snakeY[i-1];
158             }
159             if(fx.equals("R")) {
160                 snakeX[0] = snakeX[0] + 25;
161                 if (snakeX[0] > 850) {snakeX[0] = 25;}
162             }else if(fx.equals("L")){
163                 snakeX[0]=snakeX[0]-25;
164                 if(snakeX[0]<25){snakeX[0]=850;};
165             }else if(fx.equals("U")){
166                 snakeY[0]=snakeY[0]-25;
167                 if(snakeY[0]<75){snakeY[0]=650;}
168             }else if(fx.equals("D")){
169                 snakeY[0]=snakeY[0]+25;
170                 if(snakeY[0]>650){snakeY[0]=75;}
171             }
172             //失败判定,撞到自己就算失败
173             for(int i=1;i<length;i++){
174                 if(snakeX[0]==snakeX[i] && snakeY[0]==snakeY[i]){
175                     isFail=true;
176                 }
177             }
178 
179             repaint();
180 
181         }
182         timer.start();//定时器开启
183     }
184 }
185 //键盘监听器

 

Data.java

 1 package com.kuang.snake;
 2 
 3 import javax.swing.*;
 4 import java.net.URL;
 5 
 6 //数据中心
 7 public class Data {
 8     //相对路径 tx.jpg
 9     //绝对路径 /相当于当前的项目
10     public static URL headerURL = Data.class.getResource("statics/header.png");
11     public static ImageIcon header = new ImageIcon(headerURL);
12 
13     public static URL upURL = Data.class.getResource("statics/up.png");
14     public static URL downURL = Data.class.getResource("statics/down.png");
15     public static URL leftURL = Data.class.getResource("statics/left.png");
16     public static URL rightURL = Data.class.getResource("statics/right.png");
17     public static ImageIcon up = new ImageIcon(upURL);
18     public static ImageIcon down = new ImageIcon(downURL);
19     public static ImageIcon left = new ImageIcon(leftURL);
20     public static ImageIcon right = new ImageIcon(rightURL);
21 
22     public static URL bodyURL = Data.class.getResource("statics/body.png");
23     public static ImageIcon body = new ImageIcon(bodyURL);
24 
25     public static URL foodURL = Data.class.getResource("statics/food.png");
26     public static ImageIcon food = new ImageIcon(foodURL);
27 
28 }

图片文件的地址

up.png

down.png

right.png

header.png

left.png

food.png

body.png

 

posted @ 2021-08-18 17:40  magnificent0121  阅读(356)  评论(0)    收藏  举报