-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.pde
328 lines (271 loc) · 8.97 KB
/
snake.pde
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
// Luke Dickerson
// Sept. 1st, 2017
////////////// VARIABLES ////////////////////////////
// Player Variables
PlayerSegment head, tail;
ArrayList<PlayerSegment> player;
String playerDirection = "UP";
boolean dead = true;
// Food Variables
Food food;
int nextRainbowColor;
// Map Variables
int gridWidth = 20; // number of squares wide
int gridHeight = 20; // number of squares tall
int squareWidth, squareHeight;
// Miscellaneous Variables
boolean paused = false, allowedToChangeDirection = true;
int timer;
int startingFramerate = 400;
int framerate = startingFramerate; // in milliseconds
boolean won = false;
int winTime;
int winDisplayDuration = 2500; // milliseconds
/////////////////////////////////////////////////////
void setup() {
size(400, 400);
reset();
squareWidth = width / gridWidth;
squareHeight = height / gridHeight;
timer = 0;
food = new Food();
}
void draw() {
if (!dead) {
if (!paused) {
if (millis() - timer >= framerate) {
allowedToChangeDirection = true;
background(0);
movePlayer();
dead = hitWall() || hitSelf();
if (!dead) {
if (beatGame()) {
dead = true;
won = true;
winTime = millis();
} else {
drawPlayer();
eatFood();
food.drawFood();
}
} else {
reset();
}
timer = millis();
if (player.size() < 35) {
framerate = startingFramerate - 10*player.size();
} else { framerate = startingFramerate - 350; }
}
} else {
textSize(10);
fill(255);
text("Paused", 10, 30);
}
} else {
background(0);
textSize(17);
fill(255);
if (won) {
if (millis() - winTime < winDisplayDuration) {
text("YOU WON!",20, 40);
} else {
won = false;
}
} else {
text("Press Spacebar to Play\n\nMove with Arrow Keys\nPause/Resume with Spacebar",
20, 40);
}
}
}
boolean beatGame() {
return player.size() == width * height;
}
void drawPlayer() {
for (PlayerSegment ps : player) {
ps.drawSegment();
}
}
class PlayerSegment {
int x, y;
PlayerSegment ahead; // the segment ahead of this one (head's ahead = null)
int m = 1;
color c;
PlayerSegment(int psx, int psy, PlayerSegment ah, color pc) {
x = psx;
y = psy;
ahead = ah;
c = pc;
}
void drawSegment() {
fill(c);
rect(x + m, y + m, squareWidth - 2*m, squareHeight - 2*m);
}
void moveSegment(int nx, int ny) { x = nx; y = ny; }
PlayerSegment getAhead() { return ahead; }
void setAhead(PlayerSegment newAhead) { ahead = newAhead; }
int getX() { return x; }
int getY() { return y; }
color getColor() { return c; }
}
// puts the last segment of the player at the front of the head of the player
void movePlayer() {
int newHeadX = head.getX();
int newHeadY = head.getY();
if (playerDirection == "UP") { newHeadY -= squareHeight; }
else if (playerDirection == "DOWN") { newHeadY += squareHeight; }
else if (playerDirection == "LEFT") { newHeadX -= squareWidth; }
else if (playerDirection == "RIGHT") { newHeadX += squareWidth; }
tail.moveSegment(newHeadX, newHeadY);
if (player.size() > 1) {
PlayerSegment newTail = tail.getAhead();
tail.setAhead(null);
head.setAhead(tail);
head = tail;
tail = newTail;
}
}
boolean hitWall() {
return head.getX() < 0 || head.getX() >= width ||
head.getY() < 0 || head.getY() >= height;
}
boolean hitSelf() {
for (PlayerSegment ps : player) {
if (ps != head) {
if (ps.getX() == head.getX() && ps.getY() == head.getY()) {
return true;
}
}
}
return false;
}
void reset() {
nextRainbowColor = 0;
head = new PlayerSegment(width / 2, height / 2, null, rainbowColor());
tail = head;
player = new ArrayList<PlayerSegment>();
player.add(head);
}
boolean locationOccupied(int x, int y) {
for (PlayerSegment ps : player) {
if (ps.getX() == x && ps.getY() == y) {
return true;
}
}
return false;
}
class Food {
int x, y, m;
color c;
Food() {
x = 0;
y = 0;
m = 2;
newPosition();
}
void newPosition() {
int oldX = x, oldY = y;
int newX, newY, rx, ry;
boolean squareOccupied;
// change position
while (x == oldX && y == oldY) {
rx = (int)random(width); ry = (int)random(height);
newX = rx - rx % squareWidth; newY = ry - ry % squareHeight;
squareOccupied = false;
for (PlayerSegment ps : player) {
if (newX == ps.getX() && newY == ps.getY()) {
squareOccupied = true;
break;
}
}
if (!squareOccupied) {
x = newX;
y = newY;
}
}
// change color
c = rainbowColor();
}
void drawFood() {
fill(c);
ellipse(x + squareWidth/2, y + squareHeight/2, squareWidth - 2*m, squareHeight - 2*m);
}
int getX() { return x; }
int getY() { return y; }
color getColor() {return c; }
}
void eatFood() {
if (head.getX() == food.getX() && head.getY() == food.getY()) {
// add a playerSegment to the player's tail
int tx = tail.getX(), ty = tail.getY();
int ntx = 0, nty = 0;
if (player.size() > 1) {
int ahx = tail.getAhead().getX(), ahy = tail.getAhead().getY();
// if tail's ahead is above the tail, put the new tail under the current tail
if (tx == ahx && ty > ahy) { ntx = tx; nty = ty + squareHeight; }
// if tail's ahead is below the tail, put the new tail above the current tail
else if (tx == ahx && ty < ahy) { ntx = tx; nty = ty - squareHeight; }
// if tail's ahead is to the left of the tail, put the new tail to the right of the current tail
else if (tx < ahx && ty == ahy) { ntx = tx + squareWidth; nty = ty; }
// if tail's ahead is to the right of the tail, put the new tail to the left of the current tail
else if (tx > ahx && ty == ahy) { ntx = tx - squareWidth; nty = ty; }
// if the location of the newTail is occupied
if (locationOccupied(ntx, nty)) {
// find a position that is unoccupied, and put it there instead
if (!locationOccupied(tx, ty + squareHeight)) { ntx = tx; nty = ty + squareHeight; }
else if (!locationOccupied(tx, ty - squareHeight)) { ntx = tx; nty = ty - squareHeight; }
else if (!locationOccupied(tx + squareWidth, ty)) { ntx = tx + squareWidth; nty = ty; }
else if (!locationOccupied(tx - squareWidth, ty)) { ntx = tx - squareWidth; nty = ty; }
}
} else {
if (playerDirection == "UP") { ntx = tx; nty = ty + squareHeight; }
else if (playerDirection == "DOWN") { ntx = tx; nty = ty - squareHeight; }
else if (playerDirection == "LEFT") { ntx = tx + squareWidth; nty = ty; }
else if (playerDirection == "RIGHT") { ntx = tx - squareWidth; nty = ty; }
}
PlayerSegment newTail = new PlayerSegment(ntx, nty, tail, food.getColor());
player.add(newTail);
tail = newTail;
food.newPosition();
}
}
color randomColor() {
int numColors = 6, rc = (int)random(numColors);
switch (rc) {
case 0: return color(255, 0, 0 ); // red
case 1: return color(255, 127, 0 ); // orange
case 2: return color(255, 255, 0 ); // yellow
case 3: return color(0, 255, 0 ); // green
case 4: return color(0, 0, 255); // blue
case 5: return color(148, 0, 211); // violet
default: return color(255, 255, 255); // white
}
}
color rainbowColor() {
nextRainbowColor++;
if (nextRainbowColor > 5) { nextRainbowColor = 0; }
switch (nextRainbowColor) {
case 0: return color(255, 0, 0 ); // red
case 1: return color(255, 127, 0 ); // orange
case 2: return color(255, 255, 0 ); // yellow
case 3: return color(0, 255, 0 ); // green
case 4: return color(0, 0, 255); // blue
case 5: return color(148, 0, 211); // violet
default: return color(255, 255, 255); // white
}
}
void keyPressed() {
if (key == CODED) {
if (keyCode == UP && playerDirection != "DOWN" && allowedToChangeDirection) { playerDirection = "UP"; allowedToChangeDirection = false; }
else if (keyCode == DOWN && playerDirection != "UP" && allowedToChangeDirection) { playerDirection = "DOWN"; allowedToChangeDirection = false; }
else if (keyCode == LEFT && playerDirection != "RIGHT" && allowedToChangeDirection) { playerDirection = "LEFT"; allowedToChangeDirection = false; }
else if (keyCode == RIGHT && playerDirection != "LEFT" && allowedToChangeDirection) { playerDirection = "RIGHT"; allowedToChangeDirection = false; }
} else {
if (key == ' ') {
if (dead) {
dead = false;
} else {
paused = !paused;
}
}
}
}