-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.java
More file actions
281 lines (252 loc) · 9.08 KB
/
Snake.java
File metadata and controls
281 lines (252 loc) · 9.08 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
/*Possible Changes:
* add controls menu
* speed up each time food is eaten
* add highscore
*/
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Random;
import java.util.Scanner;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
@SuppressWarnings("serial")
public class Snake extends JFrame {
public enum Direction {
UP, DOWN, LEFT, RIGHT
}
private int height = 32, width = 32,
delay = 100, delayDecay = 1,
initLength = 5, addLength = 3,
foodCount = 1, iconSize = 8;
private final static String START_PROMPT = "Press any key to start!";
private final static String SETTINGS_ERROR = "Problem with the file settings.txt\nMake sure it contains:\nGrid Height\nGrid Width\nGame Delay\nInital Snake Length\nAdded Snake Length\nFood Count";
private final static String SCORE_PREFIX = "Score: ";
private final static String PLAY_AGAIN = "Would you like to play again?";
private final static String WIN_MESSAGE = "You Won Snake!\nNow go outside and live a little.";
private ImageIcon fieldIcon, snakeIcon, foodIcon;
private JPanel base;
private JLabel[][] field;
private JLabel scoreBoard;
private LinkedList<int[]> snake;
private Direction dir, lastDir;
private Timer timer;
private Random rng;
private int score, removeWait;
private boolean finished;
public static void main(String args[]) {
System.setProperty("java.util.Arrays.useLegacyMergeSort", "true");
JFrame snake = new Snake("Snake - Eat to Win");
snake.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
snake.setVisible(true);
}
public Snake(String s) {
super(s);
setLayout(new BorderLayout());
addKeyListener(new DirectionListener());
setFocusable(true);
fieldIcon = createIcon(Color.WHITE);
snakeIcon = createIcon(Color.BLACK);
foodIcon = createIcon(Color.RED);
rng = new Random();
scoreBoard = new JLabel();
add(scoreBoard, BorderLayout.NORTH);
timer = new Timer(delay, gameLoop);
setup();
}
private ImageIcon createIcon(Color color) {
BufferedImage img = new BufferedImage(iconSize, iconSize, BufferedImage.TYPE_INT_RGB);
Graphics2D g = img.createGraphics();
g.setPaint(color);
g.fillRect(0,0,iconSize,iconSize);
return new ImageIcon(img);
}
private void setup() {
readSettings();
initGUI();
initSnake();
}
private void readSettings() {
try {
Scanner scan = new Scanner(new File("settings.txt"));
height = Integer.parseInt(scan.nextLine().trim());
width = Integer.parseInt(scan.nextLine().trim());
delay = Integer.parseInt(scan.nextLine().trim());
initLength = Integer.parseInt(scan.nextLine().trim());
addLength = Integer.parseInt(scan.nextLine().trim());
foodCount = Integer.parseInt(scan.nextLine().trim());
scan.close();
} catch (Exception e) {
System.out.println(SETTINGS_ERROR);
}
}
private void initGUI() {
base = new JPanel(new GridLayout(height, width));
field = new JLabel[height][width];
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
base.add(field[i][j] = new JLabel(fieldIcon));
scoreBoard.setText(START_PROMPT);
add(base, BorderLayout.CENTER);
pack();
}
private void initSnake() {
snake = new LinkedList<int[]>();
for (int i = 0; i < initLength; i++) {
add(new int[] { height / 2, width / 2 - initLength / 2 + i });
}
for (int i = 0; i < foodCount; i++)
addFood(i);
lastDir = dir = Direction.RIGHT;
removeWait = score = 0;
finished = false;
timer.setDelay(delay);
}
private void add(int[] pos) {
snake.add(pos);
field[pos[0]][pos[1]].setIcon(snakeIcon);
}
private void remove() {
if (removeWait > 0)
removeWait--;
else {
int tail[] = snake.remove();
field[tail[0]][tail[1]].setIcon(fieldIcon);
}
}
private void addFood() {
addFood(foodCount);
}
private void addFood(int food) {
if (food == foodCount)
scoreBoard.setText("Score: " + ++score);
int whiteCount = height * width - snake.size() - food;
if (whiteCount <= 0)
return;
int foodIndex = rng.nextInt(whiteCount) + 1;
whiteCount = 0;
for (JLabel[] i : field)
for (JLabel j : i) {
whiteCount += j.getIcon() == fieldIcon ? 1 : 0;
if (whiteCount == foodIndex) {
j.setIcon(foodIcon);
return;
}
}
}
public boolean isFood(int[] pos) {
return field[pos[0]][pos[1]].getIcon() == foodIcon;
}
public boolean isSnake(int[] pos) { // Straightforward unless it's the tail
// and it will be removed
return (!(pos[0] == snake.getFirst()[0] && pos[1] == snake.getFirst()[1]) || removeWait > 0)
&& field[pos[0]][pos[1]].getIcon() == snakeIcon;
}
ActionListener gameLoop = new ActionListener() {
public void actionPerformed(ActionEvent e) {
int[] head = Arrays.copyOf(snake.getLast(), 2);
switch (dir) {
case UP:
head[0] -= 1;
break;
case DOWN:
head[0] += 1;
break;
case LEFT:
head[1] -= 1;
break;
case RIGHT:
head[1] += 1;
break;
}
lastDir = dir;
head[0] = (head[0] + height) % height;
head[1] = (head[1] + width) % width;
if (isSnake(head)) {
timer.stop();
finished = true;
if (snake.size() == height * width)
if(JOptionPane.showConfirmDialog(Snake.this, WIN_MESSAGE, "Game Over", JOptionPane.OK_CANCEL_OPTION) == JOptionPane.OK_OPTION)
System.exit(0);
switch (JOptionPane.showConfirmDialog(Snake.this, PLAY_AGAIN)) {
case JOptionPane.YES_OPTION:
setup();
break;
case JOptionPane.NO_OPTION:
System.exit(0);
break;
}
} else {
if (isFood(head)) {
removeWait += addLength;
addFood();
}
remove();
add(head);
}
}
};
private class DirectionListener implements KeyListener {
@SuppressWarnings("fallthrough")
public void keyPressed(KeyEvent e) {
boolean skip = false;
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
case KeyEvent.VK_NUMPAD8:
case KeyEvent.VK_W:
if (lastDir != Direction.DOWN)
dir = Direction.UP;
break;
case KeyEvent.VK_DOWN:
case KeyEvent.VK_NUMPAD2:
case KeyEvent.VK_S:
if (lastDir != Direction.UP)
dir = Direction.DOWN;
break;
case KeyEvent.VK_LEFT:
case KeyEvent.VK_NUMPAD4:
case KeyEvent.VK_A:
if (lastDir != Direction.RIGHT)
dir = Direction.LEFT;
break;
case KeyEvent.VK_RIGHT:
case KeyEvent.VK_NUMPAD6:
case KeyEvent.VK_D:
if (lastDir != Direction.LEFT)
dir = Direction.RIGHT;
break;
case KeyEvent.VK_R:
setup();
// Intentional fallthrough
case KeyEvent.VK_P:
timer.stop();
skip = true;
break;
case KeyEvent.VK_ESCAPE:
System.exit(0);
break;
}
if (!(timer.isRunning() || finished || skip)) {
timer.start();
scoreBoard.setText(SCORE_PREFIX + score);
}
}
public void keyTyped(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}
}