-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeGame.java
More file actions
238 lines (202 loc) · 5.2 KB
/
SnakeGame.java
File metadata and controls
238 lines (202 loc) · 5.2 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
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JPanel;
public class SnakeGame extends JPanel implements Runnable, KeyListener {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 500, HEIGHT = 500;
private Thread thread;
protected boolean running;
private boolean success = false;
private boolean right = true, left = false, up = false, down = false;
private BodyPart b;
private ArrayList<BodyPart> snake;
private Apple apple;
private ArrayList<Apple> apples;
private ArrayList<Integer> keyPresses;
private long timePressed;
private long timeReleased;
private ArrayList<Long> timePressedLeft;
private ArrayList<Long> timePressedRight;
private ArrayList<Long> timePressedUp;
private ArrayList<Long> timePressedDown;
private long startTime;
private long endTime;
private int xCoor = 10, yCoor = 10, size = 15;
private int ticks = 0;
Biometrics biometrics;
public SnakeGame() {
setFocusable(true);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
addKeyListener(this);
snake = new ArrayList<BodyPart>();
apples = new ArrayList<Apple>();
keyPresses = new ArrayList<Integer>();
timePressedLeft = new ArrayList<Long>();
timePressedRight = new ArrayList<Long>();
timePressedUp = new ArrayList<Long>();
timePressedDown = new ArrayList<Long>();
start();
}
public void start() {
running = true;
thread = new Thread(this);
thread.start();
startTime = System.nanoTime();
}
public Biometrics stop() {
endTime = System.nanoTime();
biometrics = new Biometrics(keyPresses, timePressedLeft, timePressedRight, timePressedUp, timePressedDown, startTime, endTime, success);
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
return biometrics;
}
public void tick() {
if(snake.size() == 0) {
b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);
}
ticks++;
if(ticks > 250000) {
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
if(right) {
xCoor++;
}
if(left) {
xCoor--;
}
if(up) {
yCoor--;
}
if(down) {
yCoor++;
}
ticks = 0;
b = new BodyPart(xCoor, yCoor, 10);
snake.add(b);
if(snake.size() > size) {
snake.remove(0);
}
}
if(apples.size() == 0) {
int xCoor = 30;
int yCoor = 30;
apple = new Apple(xCoor, yCoor, 10);
apples.add(apple);
}
for(int i = 0; i < apples.size(); i++) {
if(xCoor == apples.get(i).getxCoor() && yCoor == apples.get(i).getyCoor()) {
size++;
apples.remove(i);
success = true;
System.out.println("You got the apple!");
stop();
}
}
//collision with snake body part
for(int i = 0; i < snake.size(); i++) {
if(xCoor == snake.get(i).getxCoor() && yCoor == snake.get(i).getyCoor()) {
if(i != snake.size() - 1) {
System.out.println("Game Over");
success = false;
stop();
}
}
}
//collision with border
if(xCoor < 0 || xCoor > 49 || yCoor < 0 || yCoor > 49) {
System.out.println("Game Over");
success = false;
stop();
}
}
//getter method
public Biometrics getBiometrics() {
return biometrics;
}
public void paint(Graphics g) {
g.clearRect(0, 0, WIDTH, HEIGHT);
g.setColor(Color.BLACK);
g.fillRect(0, 0, WIDTH, HEIGHT);
for(int i = 0; i < WIDTH/10; i++) {
g.drawLine(i*10, 0, i*10, HEIGHT);
}
for(int i = 0; i < HEIGHT/10; i++) {
g.drawLine(0, i*10, HEIGHT, i*10);
}
for(int i = 0; i < snake.size(); i++) {
snake.get(i).draw(g);
}
for(int i = 0; i < apples.size(); i++) {
apples.get(i).draw(g);
}
}
@Override
public void run() {
while(running) {
tick();
repaint();
}
}
@Override
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
keyPresses.add(key);
timePressed = System.nanoTime();
if(key == KeyEvent.VK_RIGHT && !left) {
right = true;
up = false;
down = false;
}
if(key == KeyEvent.VK_LEFT && !right) {
left = true;
up = false;
down = false;
}
if(key == KeyEvent.VK_UP && !down) {
up = true;
left = false;
right = false;
}
if(key == KeyEvent.VK_DOWN && !up) {
down = true;
left = false;
right = false;
}
}
@Override
public void keyReleased(KeyEvent arg0) {
int key = arg0.getKeyCode();
if(key == KeyEvent.VK_RIGHT && !left) {
timeReleased = System.nanoTime();
timePressedRight.add(timeReleased - timePressed);
}
if(key == KeyEvent.VK_LEFT && !right) {
timeReleased = System.nanoTime();
timePressedLeft.add(timeReleased - timePressed);
}
if(key == KeyEvent.VK_UP && !down) {
timeReleased = System.nanoTime();
timePressedUp.add(timeReleased - timePressed);
}
if(key == KeyEvent.VK_DOWN && !up) {
timeReleased = System.nanoTime();
timePressedDown.add(timeReleased - timePressed);
}
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
}