-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeGame.java
More file actions
611 lines (472 loc) · 14.3 KB
/
SnakeGame.java
File metadata and controls
611 lines (472 loc) · 14.3 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
import java.awt.BorderLayout;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.util.LinkedList;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* @author Adarsh Varshney
* Reference: Brendan Jones
*/
public class SnakeGame extends JFrame {
private static final long serialVersionUID = 12345L;
private static final long FRAME_TIME = 1000L / 50L;
private static final int MIN_SNAKE_LENGTH = 5;
private static final int MAX_DIRECTIONS = 3;
private BoardPanel board;
private SidePanel side;
private Random random;
private Clock logicTimer;
private boolean isNewGame;
private boolean isGameOver;
private boolean isPaused;
private LinkedList<Point> snake;
private LinkedList<Direction> directions;
private int score;
private int fruitsEaten;
private int nextFruitScore;
private SnakeGame() {
super("Adarsh's Snake Game");
setLayout(new BorderLayout());
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
this.board = new BoardPanel(this);
this.side = new SidePanel(this);
add(board, BorderLayout.CENTER);
add(side, BorderLayout.EAST);
addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_W:
case KeyEvent.VK_UP:
if (!isPaused && !isGameOver) {
if (directions.size() < MAX_DIRECTIONS) {
Direction last = directions.peekLast();
if (last != Direction.South && last != Direction.North) {
directions.addLast(Direction.North);
}
}
}
break;
case KeyEvent.VK_S:
case KeyEvent.VK_DOWN:
if (!isPaused && !isGameOver) {
if (directions.size() < MAX_DIRECTIONS) {
Direction last = directions.peekLast();
if (last != Direction.North && last != Direction.South) {
directions.addLast(Direction.South);
}
}
}
break;
case KeyEvent.VK_A:
case KeyEvent.VK_LEFT:
if (!isPaused && !isGameOver) {
if (directions.size() < MAX_DIRECTIONS) {
Direction last = directions.peekLast();
if (last != Direction.East && last != Direction.West) {
directions.addLast(Direction.West);
}
}
}
break;
case KeyEvent.VK_D:
case KeyEvent.VK_RIGHT:
if (!isPaused && !isGameOver) {
if (directions.size() < MAX_DIRECTIONS) {
Direction last = directions.peekLast();
if (last != Direction.West && last != Direction.East) {
directions.addLast(Direction.East);
}
}
}
break;
case KeyEvent.VK_P:
if (!isGameOver) {
isPaused = !isPaused;
logicTimer.setPaused(isPaused);
}
break;
case KeyEvent.VK_ENTER:
if (isNewGame || isGameOver) {
resetGame();
}
break;
}
}
});
pack();
setLocationRelativeTo(null);
setVisible(true);
}
private void startGame() {
this.random = new Random();
this.snake = new LinkedList<>();
this.directions = new LinkedList<>();
this.logicTimer = new Clock(10.0f);
this.isNewGame = true;
logicTimer.setPaused(true);
while (true) {
long start = System.nanoTime();
logicTimer.update();
if (logicTimer.hasElapsedCycle()) {
updateGame();
}
board.repaint();
side.repaint();
long delta = (System.nanoTime() - start) / 1000000L;
if (delta < FRAME_TIME) {
try {
Thread.sleep(FRAME_TIME - delta);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
private void updateGame() {
TileType collision = updateSnake();
if (collision == TileType.Fruit) {
fruitsEaten++;
score += nextFruitScore;
logicTimer.setCyclesPerSecond(10.0f + fruitsEaten / 9);
spawnFruit();
} else if (collision == TileType.SnakeBody) {
isGameOver = true;
logicTimer.setPaused(true);
} else if (nextFruitScore > 10) {
nextFruitScore--;
}
}
private TileType updateSnake() {
Direction direction = directions.peekFirst();
Point head = new Point(snake.peekFirst());
switch (direction) {
case North:
head.y--;
break;
case South:
head.y++;
break;
case West:
head.x--;
break;
case East:
head.x++;
break;
}
if (head.x < 0) {
head.x = BoardPanel.COL_COUNT - 1;
} else if (head.x >= BoardPanel.COL_COUNT) {
head.x = 0;
} else if (head.y < 0) {
head.y = BoardPanel.ROW_COUNT - 1;
} else if (head.y >= BoardPanel.ROW_COUNT) {
head.y = 0;
}
TileType old = board.getTile(head.x, head.y);
if (old != TileType.Fruit && snake.size() > MIN_SNAKE_LENGTH) {
Point tail = snake.removeLast();
board.setTile(tail, null);
old = board.getTile(head.x, head.y);
}
if (old != TileType.SnakeBody) {
board.setTile(snake.peekFirst(), TileType.SnakeBody);
snake.push(head);
board.setTile(head, TileType.SnakeHead);
if (directions.size() > 1) {
directions.poll();
}
}
return old;
}
private void resetGame() {
this.score = 0;
this.fruitsEaten = 0;
this.isNewGame = false;
this.isGameOver = false;
Point head = new Point(BoardPanel.COL_COUNT / 2, BoardPanel.ROW_COUNT / 2);
snake.clear();
snake.add(head);
board.clearBoard();
board.setTile(head, TileType.SnakeHead);
directions.clear();
directions.add(Direction.North);
logicTimer.reset();
spawnFruit();
}
public boolean isNewGame() {
return isNewGame;
}
public boolean isGameOver() {
return isGameOver;
}
public boolean isPaused() {
return isPaused;
}
private void spawnFruit() {
this.nextFruitScore = 100;
int index = random.nextInt(BoardPanel.COL_COUNT * BoardPanel.ROW_COUNT - snake.size());
int freeFound = -1;
for (int x = 0; x < BoardPanel.COL_COUNT; x++) {
for (int y = 0; y < BoardPanel.ROW_COUNT; y++) {
TileType type = board.getTile(x, y);
if (type == null || type == TileType.Fruit) {
if (++freeFound == index) {
board.setTile(x, y, TileType.Fruit);
break;
}
}
}
}
}
public int getScore() {
return score;
}
public int getFruitsEaten() {
return fruitsEaten;
}
public int getNextFruitScore() {
return nextFruitScore;
}
public Direction getDirection() {
return directions.peek();
}
public static void main(String[] args) {
SnakeGame snake = new SnakeGame();
snake.startGame();
}
public static class SidePanel extends JPanel {
private static final Font LARGE_FONT = new Font("Tahoma", Font.BOLD, 30);
private static final Font MEDIUM_FONT = new Font("Tahoma", Font.BOLD, 20);
private static final Font SMALL_FONT = new Font("Tahoma", Font.BOLD, 12);
private SnakeGame game;
public SidePanel(SnakeGame game) {
this.game = game;
setPreferredSize(new Dimension(300, BoardPanel.COL_COUNT * BoardPanel.TILE_SIZE));
setBackground(Color.BLACK);
}
private static final int STATISTICS_OFFSET = 150;
private static final int CONTROLS_OFFSET = 320;
private static final int MESSAGE_STRIDE = 30;
private static final int SMALL_OFFSET = 30;
private static final int LARGE_OFFSET = 50;
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.WHITE);
g.setFont(LARGE_FONT);
g.drawString("Snake Game", getWidth() / 2 - g.getFontMetrics().stringWidth("Snake Game") / 2, 50);
g.setFont(MEDIUM_FONT);
g.drawString("Statistics", SMALL_OFFSET, STATISTICS_OFFSET);
g.drawString("Controls", SMALL_OFFSET, CONTROLS_OFFSET);
g.setFont(SMALL_FONT);
int drawY = STATISTICS_OFFSET;
g.drawString("Total Score: " + game.getScore(), LARGE_OFFSET, drawY += MESSAGE_STRIDE);
g.drawString("Fruit Eaten: " + game.getFruitsEaten(), LARGE_OFFSET, drawY += MESSAGE_STRIDE);
g.drawString("Fruit Score: " + game.getNextFruitScore(), LARGE_OFFSET, drawY += MESSAGE_STRIDE);
drawY = CONTROLS_OFFSET;
g.drawString("Move Up: W / Up Arrowkey", LARGE_OFFSET, drawY += MESSAGE_STRIDE);
g.drawString("Move Down: S / Down Arrowkey", LARGE_OFFSET, drawY += MESSAGE_STRIDE);
g.drawString("Move Left: A / Left Arrowkey", LARGE_OFFSET, drawY += MESSAGE_STRIDE);
g.drawString("Move Right: D / Right Arrowkey", LARGE_OFFSET, drawY += MESSAGE_STRIDE);
g.drawString("Pause Game: P", LARGE_OFFSET, drawY += MESSAGE_STRIDE);
g.drawString("Designed By", getWidth() / 2 - g.getFontMetrics().stringWidth("Designed By") / 2,
drawY += 80);
g.setFont(MEDIUM_FONT);
g.drawString("Adarsh Varshney", getWidth() / 2 - g.getFontMetrics().stringWidth("Adarsh Varshney") / 2,
drawY += 25);
}
}
public enum TileType {
Fruit,
SnakeHead,
SnakeBody
}
public enum Direction {
North,
East,
South,
West
}
public static class Clock {
private float millisPerCycle;
private long lastUpdate;
private int elapsedCycles;
private float excessCycles;
private boolean isPaused;
public Clock(float cyclesPerSecond) {
setCyclesPerSecond(cyclesPerSecond);
reset();
}
public void setCyclesPerSecond(float cyclesPerSecond) {
this.millisPerCycle = (1.0f / cyclesPerSecond) * 1000;
}
public void reset() {
this.elapsedCycles = 0;
this.excessCycles = 0.0f;
this.lastUpdate = getCurrentTime();
this.isPaused = false;
}
public void update() {
long currUpdate = getCurrentTime();
float delta = (float) (currUpdate - lastUpdate) + excessCycles;
if (!isPaused) {
this.elapsedCycles += (int) Math.floor(delta / millisPerCycle);
this.excessCycles = delta % millisPerCycle;
}
this.lastUpdate = currUpdate;
}
public void setPaused(boolean paused) {
this.isPaused = paused;
}
public boolean isPaused() {
return isPaused;
}
public boolean hasElapsedCycle() {
if (elapsedCycles > 0) {
this.elapsedCycles--;
return true;
}
return false;
}
public boolean peekElapsedCycle() {
return (elapsedCycles > 0);
}
private static final long getCurrentTime() {
return (System.nanoTime() / 1000000L);
}
}
public static class BoardPanel extends JPanel {
private static final long serialVersionUID = -54321L;
public static final int COL_COUNT = 25;
public static final int ROW_COUNT = 25;
public static final int TILE_SIZE = 30;
public static final float STROKE_SIZE = TILE_SIZE / 10;
private static final int EYE_LARGE_INSET = TILE_SIZE / 3;
private static final int EYE_SMALL_INSET = TILE_SIZE / 6;
private static final int EYE_LENGTH = TILE_SIZE / 5;
private static final Font FONT = new Font("Tahoma", Font.BOLD, 40);
private SnakeGame game;
private TileType[] tiles;
public BoardPanel(SnakeGame game) {
this.game = game;
this.tiles = new TileType[ROW_COUNT * COL_COUNT];
setPreferredSize(new Dimension(COL_COUNT * TILE_SIZE, ROW_COUNT * TILE_SIZE));
setBackground(Color.BLACK);
}
public void clearBoard() {
for (int i = 0; i < tiles.length; i++) {
tiles[i] = null;
}
}
public void setTile(Point point, TileType type) {
setTile(point.x, point.y, type);
}
public void setTile(int x, int y, TileType type) {
tiles[y * ROW_COUNT + x] = type;
}
public TileType getTile(int x, int y) {
return tiles[y * ROW_COUNT + x];
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int x = 0; x < COL_COUNT; x++) {
for (int y = 0; y < ROW_COUNT; y++) {
TileType type = getTile(x, y);
if (type != null) {
drawTile(x * TILE_SIZE, y * TILE_SIZE, type, g);
}
}
}
g.setColor(Color.DARK_GRAY);
g.drawRect(0, 0, getWidth() - 1, getHeight() - 1);
for (int x = 0; x < COL_COUNT; x++) {
for (int y = 0; y < ROW_COUNT; y++) {
g.drawLine(x * TILE_SIZE, 0, x * TILE_SIZE, getHeight());
g.drawLine(0, y * TILE_SIZE, getWidth(), y * TILE_SIZE);
}
}
if (game.isGameOver() || game.isNewGame() || game.isPaused()) {
g.setColor(Color.WHITE);
int centerX = getWidth() / 2;
int centerY = getHeight() / 2;
String largeMessage = null;
String smallMessage = null;
if (game.isNewGame()) {
largeMessage = "Snake Game!";
smallMessage = "Press Enter to Start";
} else if (game.isGameOver()) {
largeMessage = "Game Over!";
smallMessage = "Press Enter to Restart";
} else if (game.isPaused()) {
largeMessage = "Paused";
smallMessage = "Press P to Resume";
}
g.setFont(FONT);
g.drawString(largeMessage, centerX - g.getFontMetrics().stringWidth(largeMessage) / 2, centerY - 50);
g.drawString(smallMessage, centerX - g.getFontMetrics().stringWidth(smallMessage) / 2, centerY + 50);
}
}
private void drawTile(int x, int y, TileType type, Graphics g) {
switch (type) {
case Fruit:
g.setColor(Color.RED);
g.fillOval(x + 2, y + 2, TILE_SIZE - 4, TILE_SIZE - 4);
break;
case SnakeBody:
g.setColor(Color.GREEN);
g.fillRect(x, y, TILE_SIZE, TILE_SIZE);
break;
case SnakeHead:
g.setColor(Color.GREEN);
g.fillRect(x, y, TILE_SIZE, TILE_SIZE);
g.setColor(Color.BLACK);
Graphics2D g2d = (Graphics2D) g;
g2d.setStroke(new BasicStroke(STROKE_SIZE));
switch (game.getDirection()) {
case North: {
int baseY = y + EYE_SMALL_INSET;
g.drawLine(x + EYE_LARGE_INSET, baseY, x + EYE_LARGE_INSET, baseY + EYE_LENGTH);
g.drawLine(x + TILE_SIZE - EYE_LARGE_INSET, baseY, x + TILE_SIZE - EYE_LARGE_INSET,
baseY + EYE_LENGTH);
break;
}
case South: {
int baseY = y + TILE_SIZE - EYE_SMALL_INSET;
g.drawLine(x + EYE_LARGE_INSET, baseY, x + EYE_LARGE_INSET, baseY - EYE_LENGTH);
g.drawLine(x + TILE_SIZE - EYE_LARGE_INSET, baseY, x + TILE_SIZE - EYE_LARGE_INSET,
baseY - EYE_LENGTH);
break;
}
case West: {
int baseX = x + EYE_SMALL_INSET;
g.drawLine(baseX, y + EYE_LARGE_INSET, baseX + EYE_LENGTH, y + EYE_LARGE_INSET);
g.drawLine(baseX, y + TILE_SIZE - EYE_LARGE_INSET, baseX + EYE_LENGTH,
y + TILE_SIZE - EYE_LARGE_INSET);
break;
}
case East: {
int baseX = x + TILE_SIZE - EYE_SMALL_INSET;
g.drawLine(baseX, y + EYE_LARGE_INSET, baseX - EYE_LENGTH, y + EYE_LARGE_INSET);
g.drawLine(baseX, y + TILE_SIZE - EYE_LARGE_INSET, baseX - EYE_LENGTH,
y + TILE_SIZE - EYE_LARGE_INSET);
break;
}
}
g2d.setStroke(new BasicStroke(1));
break;
}
}
}
}