从C#过度开始学java时临时写的..不足的别见怪...
Snake类:
1 package com.leaf.snake;
2
3 import java.awt.Color;
4 import java.awt.Graphics;
5 import java.awt.Point;
6 import java.awt.Rectangle;
7 import java.awt.event.KeyAdapter;
8 import java.awt.event.KeyEvent;
9 import java.util.Random;
10 import javax.swing.JFrame;
11 import javax.swing.JPanel;
12
13 public class Snake {
14
15 JFrame f = new JFrame("Snake");
16
17 /**
18 * 蛇骨宽度
19 */
20 int snakeWidth = 20;
21
22 /**
23 * 蛇骨最长
24 */
25 int snakeLong = 300;
26
27 /**
28 * 每块蛇骨坐标
29 */
30 Point[] p = new Point[100];
31
32 /**
33 * 当前蛇骨数量
34 */
35 int nowSnakeCount = 1;
36
37 /**
38 * 方向
39 */
40 int n = 0;
41
42 /**
43 * 移动的时间间隔
44 */
45 int sleep = 100;
46
47 /**
48 * 画板
49 */
50 SnakeCanvas drawSnake = new SnakeCanvas();
51
52 /**
53 * 蛇运动线程
54 */
55 SnakeThread snakethread = new SnakeThread();
56
57 /**
58 * 蛇骨头位置
59 */
60 Point snakeBonePoint = null;
61
62 int tempCount = 1;
63
64 /**
65 * 构造器
66 */
67 public Snake() {
68 //设置好监听器
69 setEvent();
70 setControls();
71 }
72
73 /**
74 * 设置控件
75 */
76 public void setControls() {
77
78 //drawSnake.setBounds(300,300,this.snakeWidth * 30,this.snakeWidth * 30);
79 f.setBounds(300,300,this.snakeWidth * 30,this.snakeWidth * 30 - 10);
80 f.add(drawSnake);
81
82 p[0] = new Point(0 + this.snakeWidth * (this.nowSnakeCount - 1), 0);
83 }
84
85 /**
86 * 开始
87 */
88 public void run() {
89
90 f.setBounds(new Rectangle(this.snakeWidth * 30, this.snakeWidth * 30));
91 snakethread.start();
92 f.setVisible(true);
93 }
94
95 /**
96 * 设置控件的监听事件
97 */
98 private void setEvent() {
99 f.setDefaultCloseOperation(3);
100
101 f.addKeyListener(new KeyAdapter() {
102 public void keyPressed(KeyEvent e) {
103
104 switch (e.getKeyCode()) {
105 case 39:
106 if (n == 2)
107 break;
108 n = 0;
109 break;// 左下右上
110 case 40:
111 if (n == 3)
112 break;
113 n = 1;
114 break;
115 case 37:
116 if (n == 0)
117 break;
118 n = 2;
119 break;
120 case 38:
121 if (n == 1)
122 break;
123 n = 3;
124 break;
125 }
126 }
127 });
128 }
129
130 int j = 0;
131
132 boolean b = true;
133
134 /**
135 * @author leaf
136 * 画蛇类
137 */
138 public class SnakeCanvas extends JPanel{//Canvas {
139
140 /**
141 *
142 */
143 private static final long serialVersionUID = 5267804344937444534L;
144
145 public void paint(Graphics g) {
146
147 g.clearRect(0, 0,g.getClip().getBounds().width, g.getClipBounds().height);
148
149 // 画蛇身
150 for (int i = 0; i < nowSnakeCount; i++) {
151 g.setColor(Color.ORANGE);
152 g.fillRect(p[i].x + 1, p[i].y + 1, snakeWidth - 1,
153 snakeWidth - 1);
154 g.setColor(Color.BLUE);
155 g.drawRect(p[i].x, p[i].y, snakeWidth, snakeWidth);
156 }
157
158 // 画蛇骨
159 if (snakeBonePoint != null) {
160 g.setColor(Color.ORANGE);
161 g.fillRect(snakeBonePoint.x + 1, snakeBonePoint.y + 1, snakeWidth - 1,
162 snakeWidth - 1);
163 g.setColor(Color.BLUE);
164 g.drawRect(snakeBonePoint.x, snakeBonePoint.y, snakeWidth, snakeWidth);
165 }
166
167 }
168 }
169
170 /**
171 * 蛇移动线程函数
172 */
173 private void move() {
174
175 //如果吃到新的蛇骨
176 if (tempCount <= nowSnakeCount) {
177 snakeBonePoint = randSnake();
178 tempCount++;
179 }
180 Point p1 = new Point();
181 Point p2 = new Point();
182 p1 = new Point(p[0].x, p[0].y);
183
184 switch (n) {
185 case 0:
186 ;
187 p[0].setLocation(new Point(p[0].x + snakeWidth, p[0].y));
188 break;
189 case 1:
190 p[0].setLocation(new Point(p[0].x, p[0].y + snakeWidth));
191 break;
192 case 2:
193 p[0].setLocation(new Point(p[0].x - snakeWidth, p[0].y));
194 break;
195 case 3:
196 p[0].setLocation(new Point(p[0].x, p[0].y - snakeWidth));
197 break;
198 }
199
200 for (int i = 1; i < nowSnakeCount; i++) {
201
202 Point temp = new Point(p[i].x, p[i].y);
203
204 p2 = new Point(p[i].x, p[i].y);
205
206 p[i] = new Point(p1.x, p1.y);
207
208 p1 = new Point(p2.x, p2.y);
209 }
210
211 // 如果吃到蛇骨
212 if (this.snakeBonePoint.x == p[0].x && this.snakeBonePoint.y == p[0].y) {
213 p[nowSnakeCount] = new Point(p[0].x, p[0].y);
214 this.nowSnakeCount++;
215 // 达到最大时停止
216 if (this.nowSnakeCount > this.snakeLong) {
217 this.b = false;
218 this.snakethread.stop();
219 }
220 }
221
222 // 判断自撞
223 for (int i = 1; i < nowSnakeCount - 1; i++) {
224
225 if (p[0].x == p[i].x && p[0].y == p[i].y) {
226 stop();
227 }
228 }
229 // 撞墙
230 if (p[0].x < 0 || p[0].x > f.getWidth() - this.snakeWidth || p[0].y < 0
231 || p[0].y > f.getHeight() -26) {
232 stop();
233 }
234
235 // 开始重绘
236 drawSnake.repaint();
237
238 }
239
240 /**
241 * 停止游戏
242 */
243 public void stop() {
244 this.b = false;
245 this.snakethread.stop();
246 }
247
248 /**
249 * 生成新蛇骨的随机坐标
250 */
251 private Point randSnake() {
252 Random r = new Random();
253 int x = r.nextInt(f.getWidth() - this.snakeWidth * 2);
254 int y = r.nextInt((f.getHeight()-26) - this.snakeWidth * 2);
255
256 while (true) {
257 if (x % this.snakeWidth == 0) {
258 break;
259 } else {
260 x++;
261 }
262 }
263 while (true) {
264 if (y % this.snakeWidth == 0) {
265 break;
266 } else {
267 y++;
268 }
269 }
270
271 return new Point(x, y);
272 }
273
274 /**
275 * 线程类
276 */
277 public class SnakeThread extends Thread {
278 public void run() {
279 while (b) {
280 move();
281
282 try {
283 Thread.sleep(sleep);
284 } catch (InterruptedException e) {
285
286 e.printStackTrace();
287 }
288 }
289 }
290 }
291
292 }
运行类:
1 package com.leaf.snake;
2
3
4 public class Run {
5
6 public static void main(String[] args) {
7
8 Snake s = new Snake();
9 s.run();
10 }
11
12 }
浙公网安备 33010602011771号