-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnake.java
More file actions
281 lines (248 loc) · 8.11 KB
/
Copy pathSnake.java
File metadata and controls
281 lines (248 loc) · 8.11 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
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferStrategy;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
public class Snake extends JFrame implements KeyListener {
private int windowWidth = 800;
private int windowHeight = 600;
private ArrayList<Point> snake;
private ArrayList<Point> obstacles;
private Point snakePosition;
private Point food;
private int dx;
private int dy;
private boolean snakeIsAlive = true;
private boolean inMenu = true;
private int points;
private Random randomGenerator = new Random();
public static void main(String[] args) {
new Snake();
}
// handles the main window of the game and sets the start
public Snake() {
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(windowWidth, windowHeight);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setTitle("Ultimate Snake Game By Team Auril");
this.setVisible(true);
this.createBufferStrategy(2);
this.addKeyListener(this);
snakePosition = randomPoint(15);
food = randomPoint(15);
initGame();
while (true) {
long start = System.currentTimeMillis();
gameLoop();
while (System.currentTimeMillis() - start < 70 - points) {
//waiting
//the higher the level, the faster the snake
}
}
}
private Point randomPoint (int deviation) {
int randomX = deviation + randomGenerator.nextInt(windowWidth / 10 - 2 * deviation);
int randomY = deviation + randomGenerator.nextInt(windowHeight / 10 - 2 * deviation);
return new Point (randomX, randomY);
}
private void initGame() {
snake = new ArrayList<Point>();
snake.add(snakePosition);
growSnake(2);
obstacles = new ArrayList<Point>();
dx = 0;
dy = 0;
// if (snakeIsAlive) {
// points = 0;
// }
}
//main loop in the game - handles movement and new object placement
private void gameLoop() {
// move the snake
moveSnake(dx, dy);
// food check
if(snake.get(0).equals(food)) {
generateFood();
generateObstacles(2);
growSnake(2);
points++;
}
// obstacle check
for (Point p : obstacles) {
if(snake.get(0).equals(p)) {
snakeIsAlive = false;
initGame();
}
}
// check if the snake has hit itself
for (int n = 3; n < snake.size(); n++) {
if(snake.get(0).equals(snake.get(n))) {
snakeIsAlive = false;
initGame();
}
}
// our snake can move through walls
if (snake.get(0).x < 0) {
snake.get(0).x = windowWidth / 10;
}
else if (snake.get(0).x >= windowWidth / 10) {
snake.get(0).x = 0;
}
else if (snake.get(0).y < 2) {
snake.get(0).y = windowHeight / 10;
}
else if (snake.get(0).y >= windowHeight / 10) {
snake.get(0).y = 2;
}
drawFrame();
}
private void drawFrame() {
BufferStrategy buffer = this.getBufferStrategy();
Graphics g = null;
try {
g = buffer.getDrawGraphics();
// cleaning the buffer
g.setColor(Color.BLACK);
g.fillRect(0, 0, windowWidth, windowHeight);
// drawing
if (inMenu) {
drawMenu(g);
}
else {
if (snakeIsAlive) {
drawObstacles(g);
drawFood(g);
drawSnake(g);
drawScore(g);
}
else {
gameOver(g);
}
}
} finally {
g.dispose();
}
// Showing the contents of the buffer
buffer.show();
Toolkit.getDefaultToolkit().sync();
}
private void drawSnake (Graphics g) {
try {
Image skin = ImageIO.read(getClass().getResource("/SnakeSkin.jpg"));
Image head = ImageIO.read(getClass().getResource("/SnakeHead.png"));
g.drawImage(head, snake.get(0).x*10, snake.get(0).y*10, 10, 10, null);
for (int i = 1; i < snake.size(); i++) {
g.drawImage(skin, snake.get(i).x*10, snake.get(i).y*10, 10, 10, null);
}
} catch (IOException e) {
e.printStackTrace();
}
/*g.setColor(Color.GREEN);
for (Point p : snake) {
g.fillRect(p.x*10, p.y*10, 10, 10);
}*/
}
private void moveSnake(int dx, int dy) {
for (int n = snake.size() - 1; n > 0; n--) {
snake.get(n).setLocation(snake.get(n-1));
}
snake.get(0).x += dx;
snake.get(0).y += dy;
}
private void growSnake (int count) {
for (int i = 0; i < count; i++) {
snake.add(new Point(snake.get(snake.size() - 1)));
}
}
private void generateFood() {
food = randomPoint(8);
}
private void drawFood(Graphics g) {
try {
Image meal = ImageIO.read(getClass().getResource("/Strawberry.png"));
g.drawImage(meal, food.x*10, food.y*10, 10, 10, null);
} catch (IOException e) {
e.printStackTrace();
}
/*g.setColor(Color.RED);
g.fillRect(food.x*10, food.y*10, 10, 10);*/
}
private void generateObstacles (int count) {
for (int i = 0; i < count; i++) {
obstacles.add(randomPoint(4));
}
}
private void drawObstacles(Graphics g) {
g.setColor(Color.WHITE);
for (Point p : obstacles) {
g.fillRect(p.x*10, p.y*10, 10, 10);
}
}
private void drawScore (Graphics g) {
g.setColor(Color.LIGHT_GRAY);
g.drawString("Total score: " + points, 713, 40);
}
private void gameOver(Graphics g) {
String gmOver = "GAME OVER!";
String score = "Score: " + points;
String next = "Press [enter] to continue";
int gmOverFontSize = 70;
int scoreFontSize = 40;
g.setColor(Color.GREEN);
g.setFont(new Font("Tahoma", Font.PLAIN, gmOverFontSize));
g.drawString(gmOver, centralTxt(gmOver, gmOverFontSize, g), 250);
g.setFont(new Font("Tahoma", Font.PLAIN, scoreFontSize));
g.drawString(score, centralTxt(score, scoreFontSize, g), 300);
g.drawString(next, centralTxt(next, scoreFontSize, g), 360);
}
private int centralTxt (String txt, int txtSize, Graphics g) {
FontMetrics fm = g.getFontMetrics();
int strWidth = fm.stringWidth(txt);
int middle = (windowWidth / 2) - (strWidth / 2);
return middle;
}
private void drawMenu(Graphics g) {
g.setColor(Color.red);
int menuFontSize = 50;
String enter = "Press [enter] to start the game";
String game = "Ultimate Snake Game";
String team = "By Team Auril";
g.setFont(new Font("Tahoma", Font.PLAIN, menuFontSize));
g.drawString(game, centralTxt(game, menuFontSize, g), 200);
g.drawString(team, centralTxt(team, menuFontSize, g), 300);
g.drawString(enter, centralTxt(enter, menuFontSize, g), 400);
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if (key == KeyEvent.VK_ENTER) {
points = 0;
snakeIsAlive = true;
inMenu = false;
}
else if (key == KeyEvent.VK_ESCAPE) {
System.exit(0);
}
else if (key == 37) {
dy = 0;
if (dx != 1) dx = -1;
} else if (key == 38) {
dx = 0;
if (dy != 1) dy = -1;
} else if (key == 39) {
dy = 0;
if (dx != -1) dx = 1;
} else if (key == 40) {
dx = 0;
if (dy != -1) dy = 1;
}
}
@Override
public void keyReleased(KeyEvent e) {}
@Override
public void keyTyped(KeyEvent e) {}
}