-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathBoard.java
More file actions
259 lines (194 loc) · 7.5 KB
/
Board.java
File metadata and controls
259 lines (194 loc) · 7.5 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
package com.zetcode;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;
public class Board extends JPanel implements ActionListener {
private final int BOARD_WIDTH = 300;
private final int BOARD_HEIGHT = 300;
private final int DOT_SIZE = 10; //A 'dot' is a single segmentation of the snake's body.
private final int MAX_LENGTH = 900;
private final int RAND_POS = 29; //The value assigned here is the multiplier for the random value generated later.
private final int DELAY = 140; //The delay between 'ticks' of the game (refreshes of the game board)
private final int[] dotXPos = new int[MAX_LENGTH]; //Holds the x position of each dot on the snake.
private final int[] dotYPos = new int[MAX_LENGTH]; //Holds the y position of each dot on the snake.
//Property variables
private int currentSnakeLength;
private int appleXPos, appleYPos;
//Movement variables
private boolean leftDirection = false;
private boolean rightDirection = true;
private boolean upDirection = false;
private boolean downDirection = false;
//Game-management variables
private boolean gameRunning = true;
private Timer timer;
private Image body, head, apple;
public Board() {
initBoard();
}
private void initBoard() {
addKeyListener(new InputHandler());
setBackground(Color.black);
setFocusable(true);
setPreferredSize(new Dimension(BOARD_WIDTH, BOARD_HEIGHT));
loadImages();
initGame();
}
private void loadImages() {
body = new ImageIcon("src/resources/dot.png").getImage();
apple = new ImageIcon("src/resources/apple.png").getImage();
head = new ImageIcon("src/resources/head.png").getImage();
}
private void initGame() {
currentSnakeLength = 3;
for (int z = 0; z < currentSnakeLength; z++) {
dotXPos[z] = 50 - z * 10;
dotYPos[z] = 50;
}
locateApple();
timer = new Timer(DELAY, this);
timer.start();
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
private void doDrawing(Graphics g) {
if (gameRunning) {
g.drawImage(apple, appleXPos, appleYPos, this);
for (int z = 0; z < currentSnakeLength; z++) {
if (z == 0) {
g.drawImage(head, dotXPos[z], dotYPos[z], this);
} else {
g.drawImage(body, dotXPos[z], dotYPos[z], this);
}
}
Toolkit.getDefaultToolkit().sync();
} else {
gameOver(g);
}
}
private void gameOver(Graphics g) {
String gameOverMessage = "Game Over";
String restartMessage = "Press 'R' to Restart";
Font endGameFont = new Font("Helvetica", Font.BOLD, 28);
FontMetrics fontMetric = getFontMetrics(endGameFont);
int gameOverTextXPos = (BOARD_WIDTH - fontMetric.stringWidth(gameOverMessage)) / 2;
int gameOverTextYPos = (BOARD_HEIGHT / 2) - 30;
g.setColor(Color.white);
g.setFont(endGameFont);
g.drawString(gameOverMessage, gameOverTextXPos, gameOverTextYPos);
g.drawString(restartMessage, gameOverTextXPos - 50, gameOverTextYPos + 60);
}
private void checkApple() {
//If the snake's head is at the same x and y pos of the apple, add to the length, and move the apple.
if ((dotXPos[0] == appleXPos) && (dotYPos[0] == appleYPos)) {
currentSnakeLength++;
locateApple();
}
}
private void move() {
//Moves every piece of the snake's body to the x and y pos of the body-part in-front of it, creating the effect
//of the snake moving forward. This must be done *before* the snake's head is moved.
for (int z = currentSnakeLength; z > 0; z--) {
dotXPos[z] = dotXPos[(z - 1)];
dotYPos[z] = dotYPos[(z - 1)];
}
//Moves the head according to the enabled direction.
if (leftDirection) {
dotXPos[0] -= DOT_SIZE;
}
if (rightDirection) {
dotXPos[0] += DOT_SIZE;
}
if (upDirection) {
dotYPos[0] -= DOT_SIZE;
}
if (downDirection) {
dotYPos[0] += DOT_SIZE;
}
}
private void checkCollision() {
/*
Loops through every part of the snake's body and for each piece checks to see if that piece is touching the
head. If it is, ends the game. Removed a check that was initially here which ignored collisions with early parts
of the body. With that check in place, it was super easy to pass straight through the body anytime you got trapped.
*/
for (int z = currentSnakeLength; z > 0; z--) {
if ((dotXPos[0] == dotXPos[z]) && (dotYPos[0] == dotYPos[z])) {
gameRunning = false;
}
}
//All of these checks are to ensure the snake is within the bounds of the game board.
if (dotYPos[0] >= BOARD_HEIGHT) {
gameRunning = false;
}
if (dotYPos[0] < 0) {
gameRunning = false;
}
if (dotXPos[0] >= BOARD_WIDTH) {
gameRunning = false;
}
if (dotXPos[0] < 0) {
gameRunning = false;
}
if (!gameRunning) {
timer.stop();
}
}
//Randomizes the x and y position of the apple.
private void locateApple() {
int r = (int) (Math.random() * RAND_POS);
appleXPos = ((r * DOT_SIZE));
r = (int) (Math.random() * RAND_POS);
appleYPos = ((r * DOT_SIZE));
}
//This is run every 'tick' of the game-loop. First everything is updated and refreshed, put in it's proper place,
//and then the screen is updated to reflect these changes.
@Override
public void actionPerformed(ActionEvent e) {
if (gameRunning) {
checkApple();
checkCollision();
move();
}
repaint();
}
//Class responsible for the handling of user-input and movement of the snake
private class InputHandler extends KeyAdapter {
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
//Added support for the 'w' 'a' 's' 'd' keys here
if ( ( (key == KeyEvent.VK_LEFT) || (key == KeyEvent.VK_A) ) && (!rightDirection)) {
leftDirection = true;
upDirection = false;
downDirection = false;
}
if ( ( (key == KeyEvent.VK_RIGHT) || (key == KeyEvent.VK_D) ) && (!leftDirection)) {
rightDirection = true;
upDirection = false;
downDirection = false;
}
if ( ( (key == KeyEvent.VK_UP) || (key == KeyEvent.VK_W) ) && (!downDirection)) {
upDirection = true;
rightDirection = false;
leftDirection = false;
}
if ( ( (key == KeyEvent.VK_DOWN) || (key == KeyEvent.VK_S) ) && (!upDirection)) {
downDirection = true;
rightDirection = false;
leftDirection = false;
}
if(key == KeyEvent.VK_R && !gameRunning){
Snake.restartGame();
}
}
}
}