-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeBoard.java
More file actions
382 lines (252 loc) · 7.73 KB
/
SnakeBoard.java
File metadata and controls
382 lines (252 loc) · 7.73 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import java.awt.*;
import java.io.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public class SnakeBoard extends JPanel{
private static int boardWidth;
private static int boardHeight;
private static int borderSize;
public SnakeDLL snake ;
private Animator animator;
private SnakePanel parent;
// Defining the motion of the snake, if the boolean is true the snkae moves in that position
public boolean up = false;
public boolean dn = false;
public boolean ryt= false;
public boolean lft = false;
//variables for food
private int foodY;
private int foodX;
private BufferedImage foodImage;
private BufferedImage ghostImage;
// ghost current and desired position
private int ghostY;
private int ghostX;
private int desiredX;
private int desiredY;
int score = 0;
int size; // number of snake segments
boolean game = true;// is the game running?
public boolean ghost;// does the user want a ghost
public SnakeBoard ( SnakePanel c){
parent = c;
animator = new Animator(this);
(new Thread(animator)).start();
setBackground (Color.white);
setMaximumSize ( new Dimension ( 400,400));
setMinimumSize ( new Dimension ( 400,400));
setPreferredSize ( new Dimension ( 400,400));
images();//load images
addKeyListener ( new MyKeyListener());
setFocusable(true);
requestFocusInWindow();
setVisible(true);
gameStart();
}
public void images(){
try{
foodImage = ImageIO.read(new File("food.png"));
}
catch (IOException e ) {
}
try{
ghostImage = ImageIO.read(new File("ghost.png"));
}
catch (IOException e ) {
}
}
public void gameStart(){
game = true;
score = 0;
size = 1;
setSnake();
setFood();
if (ghost) setGhost();
//requestFocusInWindow();
animator.start();
}
public synchronized void setGhost(){
ghostX = 270;
ghostY = 270;
}
public synchronized void setSnake(){
//set the snakes initial position
snake = new SnakeDLL ();
for (int i = 150; i > 130; i-=10) {
snake.addFirst(150,i);
}
}
public synchronized void setFood(){
// randomly place food
foodX = (int)(Math.random()*300);
foodY = (int)(Math.random()*300);
SnakeDLL.Cell p =snake.header.next;
while( p != snake.header){
if (p.x == foodX && p.y == foodY) {
setFood();
return;
}
p = p.next;
}
}
public synchronized void moveGhost(){
// if ghost is within a certain range of the snake atack head on, if not position the ghost between the snake and the food
if ((((snake.header.next.x - foodX) < 130) && ((snake.header.next.x - foodX) > -130 ))
&& (((snake.header.next.y - foodY)< 130) && ((snake.header.next.y - foodY)> -130))){
desiredY = snake.header.next.y;
desiredX = snake.header.next.x;
// if close enough snake is attacked from the side
if ( ryt || lft){
if ( ghostY < desiredY) ghostY+=5;
else ghostY -=5;
}
if ( dn || up){
if ( ghostX > desiredX) ghostX -=5;
else ghostX +=5;
}
}else {
// if farther away stay between the snake and the food
desiredY = (int)(((snake.header.next.y + foodY)/2));
desiredX = (int)(((snake.header.next.x + foodX)/2));
if ( up || dn){
if (ghostY < desiredY ) ghostY+=5;
else ghostY -=5;
}
if ( ryt || lft){
if ( (ghostX < desiredX)) ghostX +=5;
else ghostX -=5;
}
}
}
public synchronized void incrementSnake(){//s
int newX = snake.header.next.x;
int newY = snake.header.next.y;
if (lft){
newX= snake.header.next.x - 10;
snake.addFirst( newX , newY);
}
else if (ryt){
newX= snake.header.next.x + 10;
snake.addFirst( newX , newY);
}
else if (up){
newY= snake.header.next.y - 10;
snake.addFirst( newX , newY);
}
else if (dn) {
newY= snake.header.next.y + 10;
snake.addFirst( newX , newY);
}
}
public synchronized void moveSnake(){
int newX = snake.header.next.x;
int newY = snake.header.next.y;
if (lft){
newX= snake.header.next.x - 10;
snake.addFirst( newX , newY);
snake.removeLast();
}
else if (ryt){
newX= snake.header.next.x + 10;
snake.addFirst( newX , newY);
snake.removeLast();
}
else if (up){
newY= snake.header.next.y - 10;
snake.addFirst( newX , newY);
snake.removeLast();
}
else if (dn) {
newY= snake.header.next.y + 10;
snake.addFirst( newX , newY);
snake.removeLast();
}
collisionDetect();
if (ghost) moveGhost();
}
public synchronized void collisionDetect(){//s
//check if snake eats food
// is point snake.x,snake.y within x , y of foodX,foodY
if (((snake.header.next.x - foodX) < 10) && ((snake.header.next.x - foodX) > -10 ) && ((snake.header.next.y - foodY)< 10) && ((snake.header.next.y - foodY)> -10)){
size++;
score += 2*(size);
incrementSnake();
setFood();// set new food.
}
//check if snake eats itself or is eaten by ghost
SnakeDLL.Cell p =snake.header.next.next;
while( p != snake.header){
if (p.x == snake.header.next.x && p.y == snake.header.next.y){
game = false;
}
if ((ghost) && ((snake.header.next.x - ghostX) < 25) && ((snake.header.next.x - ghostX) > -25 ) && ((snake.header.next.y - ghostY)< 25) && ((snake.header.next.y - ghostY)> -25)){
snake.removeLast();
if ( size > 0) --size;
}
p = p.next;
}
// check if snakehead meets ghost
if ((ghost) && ((snake.header.next.x - ghostX) < 10) && ((snake.header.next.x - ghostX) > -10 ) && ((snake.header.next.y - ghostY)< 10) && ((snake.header.next.y - ghostY)> -10))game = false;
// check if snake falls of board
if ( snake.header.next.x < 0 || snake.header.next.x > 400)game = false;
if ( snake.header.next.y < 0 || snake.header.next.y > 400)game = false;
}
public synchronized void paintComponent (Graphics g){
super.paintComponent(g);
// drawing food
int scale = 10;
g.drawImage(foodImage, foodX,foodY,13,13,this);
//drawing ghost
if (ghost)g.drawImage(ghostImage, ghostX,ghostY,13,13,this);
// g.setColor(Color.black);
//g.fillOval(ghostX,ghostY, scale, scale);
//drawing snake
SnakeDLL.Cell p =snake.header.next;
while( p != snake.header){
g.setColor(Color.green);
g.fillOval(p.x, p.y,scale , scale);
p = p.next;
}
parent.score.setText("Score:" + score + " ");
if (!game){
animator.stop();
}
}
private class MyKeyListener extends KeyAdapter{
public void keyPressed (KeyEvent e){
if(!game) {
gameStart();
up = false;
dn = false;
lft = false;
ryt = false;
}
else {
int key = e.getKeyCode();
if ( up == false && (key == KeyEvent.VK_DOWN)){
lft = false;
ryt = false;
dn = true;
}
else if ( dn == false && (key == KeyEvent.VK_UP)){
lft = false;
ryt = false;
up = true;
}
else if ( lft == false && (key == KeyEvent.VK_RIGHT)){
up = false;
ryt = true;
dn = false;
}
else if ( ryt == false && (key == KeyEvent.VK_LEFT)){
lft = true;
up = false;
dn = false;
}
}
}
}
}