GUI编程之贪吃蛇

本文最后更新于:1 年前

贪吃蛇小游戏

素材下载提取码7777

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package cn.linsip.snake;

import javax.swing.*;
//游戏主程序
public class StartGame {
public static void main(String[] args) {
//实例JFrame
JFrame jFrame = new JFrame();
//设置属性
jFrame.setBounds(0,0,900,720);
jFrame.setResizable(false);
jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//把GamePanel加进来
jFrame.add(new GamePanel());

//设置可见性
jFrame.setVisible(true);
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
package cn.linsip.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[500];
int[] snakeY = new int[500];
//蛇移动方向
String fx;

//食物
int foodx;
int foody;
Random random = new Random();


//游戏的暂停与开始
boolean flag;

//游戏失败判定
boolean isfail;

//分数
int score;


Timer timer = new Timer(200, this);

//构造器
public GamePanel() {
init();//初始化
//获得焦点和键盘事件
setFocusable(true);
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";
flag = false;
isfail = false;
score = 0;

foodx = 25+ 25 * random.nextInt(34);
foody = 75 + 25 * random.nextInt(24);
}


//绘制面板 游戏中的所有东西都是由Graphics这个画笔画的
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);//清屏
setBackground(Color.white);//定义面板背景颜色
Data.header.paintIcon(this, g, 25, 11);//绘制头部广告位
g.fillRect(25, 75, 850, 600);//绘制长方形,游戏的主界面

//画分数
g.setColor(Color.YELLOW);
g.setFont(new Font("微软雅黑", Font.BOLD,15));
g.drawString("当前分数为:" + score, 750, 35);
//画食物
Data.food.paintIcon(this, g, foodx, foody);
//画蛇头
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]);
}
//画开始与暂停游戏
if (flag == false) {
g.setColor(Color.WHITE);
g.setFont(new Font("微软雅黑", Font.BOLD, 40));
g.drawString("请按空格开始游戏", 300, 400);
}
//画游戏结束
if (isfail) {
g.setColor(Color.RED);
g.setFont(new Font("微软雅黑", Font.BOLD, 40));
g.drawString("GAME OVER", 350, 350);
}

}

@Override
public void keyTyped(KeyEvent e) {

}

//重写键盘按压事件
@Override
public void keyPressed(KeyEvent e) {

int keyCode = e.getKeyCode();
if (isfail) {
flag = false;
if (keyCode == KeyEvent.VK_SPACE) {
init();
}
}
if (keyCode == KeyEvent.VK_SPACE) {

flag = !flag;
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 keyReleased(KeyEvent e) {

}

//事件监听 通过固定事件来刷新 1s=10次
@Override
public void actionPerformed(ActionEvent e) {

if (flag) {
//身体移动
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 (foodx == snakeX[0] && foody == snakeY[0]) {
length++;
score += 10;
foodx = 25 + 25 * random.nextInt(34);
foody = 75 + 25 * random.nextInt(24);
}

//失败判定
for (int i = length - 1; i > 0; i--) {
if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
isfail = true;
flag = false;
}
}


repaint();
}
//启动计时器
timer.start();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package cn.linsip.snake;

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 bodyUrl = Data.class.getResource("/statics/body.png");
public static ImageIcon body = new ImageIcon(bodyUrl);
public static URL downUrl = Data.class.getResource("/statics/down.png");
public static ImageIcon down = new ImageIcon(downUrl);
public static URL foodUrl = Data.class.getResource("/statics/food.png");
public static ImageIcon food = new ImageIcon(foodUrl);
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 upUrl = Data.class.getResource("/statics/up.png");
public static ImageIcon up = new ImageIcon(upUrl);
}

游戏截图

贪吃蛇


博主码字不易,本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!