-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_2.ino
518 lines (390 loc) · 8.64 KB
/
snake_2.ino
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
#include <Wire.h>
#include "Adafruit_LEDBackpack.h"
#include "Adafruit_GFX.h"
#include <EEPROM.h>
// CONFIGURATION
// FPS to begin moving at
int beginRate = 2;
// # of points between levels
int levelInterval = 3;
// Milliseconds to wait before moving food
int moveFoodTimeout = 300;
// Screen size
int boardWidth = 8;
int boardHeight = 8;
// Snake & Food colors
uint16_t foodColor = LED_YELLOW;
uint16_t snakeColor = LED_GREEN;
// Initial position of the snake
int snakeInitialX = 0;
int snakeInitialY = 0;
// // //
// Joystick inputs
int joystickXPin = 1;
int joystickYPin = 0;
// EEPROM addresses for high score and
// next random seed
int seedAddr = 0;
int scoreAddr = 10;
// Game is paused
// Used for debugging
boolean paused = false;
// Convenience for snake direciton
enum direction {
UP,
DOWN,
LEFT,
RIGHT
};
// X, Y coordinate pair
struct point {
int x;
int y;
};
// Linked list to keep track of tail
// plist short for "point list"
struct plist {
struct point coord;
struct plist* next;
};
// Find plist length
int plistLength (struct plist* list) {
if (!list) {
return 0;
}
int counter = 1;
struct plist item = *list;
while (item.next) {
counter++;
item = *(item.next);
}
return counter;
}
// Remove the last element from plist
void plistPop (struct plist* list) {
if (!list) {
return;
}
struct plist* item = list;
struct plist* last = item;
while (item->next) {
last = item;
item = item->next;
}
// Deallocate memory for the node
free(item);
last->next = NULL;
}
// Add element to beginning of plist
struct plist* plistPush(struct plist* list, struct point item) {
// Create a new item
struct plist* newItem;
// Allocate memory for the new item
newItem = (struct plist*)malloc(sizeof(struct plist));
// Fill the item with shit
newItem->coord = item;
newItem->next = list;
return newItem;
}
// checks if a given point is in a given plist
boolean pointInPlist (struct point item, struct plist* list) {
if (!list) {
return false;
}
struct plist* cur = list;
boolean i = true;
do {
if (pointsAreEqual(item, cur->coord)) {
return true;
}
if (cur->next) {
cur = cur->next;
} else {
i = false;
}
} while (i);
return false;
}
boolean pointsAreEqual (struct point pA, struct point pB) {
return pA.x == pB.x && pA.y == pB.y;
}
int timeout = -1;
long lastMillis;
float currentRate;
int interval;
struct point snakeHead;
struct plist* snakeTail;
int tailLength;
direction direction;
enum direction nextDirection;
int score;
struct point food;
Adafruit_BicolorMatrix matrix = Adafruit_BicolorMatrix();
Adafruit_7segment scoreboard = Adafruit_7segment();
// Seed the random number generator
// Otherwise each time the program runs it will
// use the same sequence of numbers for the food location.
// Saves a random number in EEPROM to seed with next time
void seed () {
// Even if there's nothing at this location,
// the default value is 255 so we'll be good anyway.
int seed = EEPROM.read(seedAddr);
randomSeed(seed);
seed = seed + random();
if (seed > 2147483646) {
seed = random();
}
EEPROM.write(seedAddr, seed);
}
// Set up, or reset the game
void initializeGame () {
// Put the food somewhere
moveFood();
score = 0;
direction = RIGHT;
tailLength = 0;
snakeHead.x = snakeInitialX;
snakeHead.y = snakeInitialY;
// To do: clear out the entire linked list
// this leads to a memory leak.
snakeTail = NULL;
currentRate = beginRate;
interval = 1000 / currentRate;
}
void setup () {
Serial.begin(9600);
matrix.begin(0x72);
scoreboard.begin(0x71);
// Lower this value to save battery
// Raise it to look awesomer
matrix.setBrightness(10);
seed();
initializeGame();
}
void loop () {
if (paused) {
return;
}
// Routines that need to run every loop,
// regardless of whether it's a frame
checkDirection();
checkTimeout();
// Game frame
if (millis() - lastMillis > interval) {
lastMillis = millis();
changeDirection();
moveSnake();
if (snakeTouchingSnake()) {
endGame();
}
if (snakeTouchingFood()) {
earnPoint();
moveFood();
}
updateDisplay();
}
}
// Checks to see if a direction change was requested
// Doesn't necessarily change it
void checkDirection () {
struct point joystick = readJoystick();
// Maybe adjust the thresholds?
if (joystick.x > 1000) {
nextDirection = LEFT;
} else if (joystick.x < 100) {
nextDirection = RIGHT;
}
if (joystick.y > 1000) {
nextDirection = DOWN;
} else if (joystick.y < 100) {
nextDirection = UP;
}
}
// The snake shouldn't be able to flip 180 degrees
// in one go. Checks if a direction change is valid,
// And changes it if so
void changeDirection () {
if (nextDirection == LEFT && direction != RIGHT) {
direction = LEFT;
}
if (nextDirection == RIGHT && direction != LEFT) {
direction = RIGHT;
}
if (nextDirection == UP && direction != DOWN) {
direction = UP;
}
if (nextDirection == DOWN && direction != UP) {
direction = DOWN;
}
}
// Tail must update first
// Basically, it moves to where the head is now,
// then the head moves.
void moveSnake () {
moveSnakeTail();
moveSnakeHead();
}
void moveSnakeHead() {
snakeHead = alterPointByDirection(snakeHead);
}
void moveSnakeTail () {
if (!tailLength) {
return;
}
// Add the current snake head to the beginning of the plist
snakeTail = plistPush(snakeTail, snakeHead);
// Remove any pixels in the tail after the amount
// required by the level.
while (plistLength(snakeTail) > tailLength) {
plistPop(snakeTail);
}
}
// Moves a given point one pixel in the current direction
struct point alterPointByDirection (struct point item) {
switch (direction) {
case UP:
item.y--;
break;
case DOWN:
item.y++;
break;
case LEFT:
item.x--;
break;
case RIGHT:
item.x++;
break;
}
if (item.y > (boardHeight - 1)) {
item.y = 0;
}
if (item.y < 0) {
item.y = boardHeight - 1;
}
if (item.x > (boardWidth - 1)) {
item.x = 0;
}
if (item.x < 0) {
item.x = boardWidth - 1;
}
return item;
}
boolean snakeTouchingSnake() {
return pointInPlist(snakeHead, snakeTail);
}
boolean snakeTouchingFood() {
return pointsAreEqual(snakeHead, food);
}
// The food moves slightly after the snake eats it
// If it moves either right away or on the next frame
// it's jarring and looks wrong
// So it moves a fraction of a second after to give
// a nice little hopping effect
void moveFood () {
timeout = moveFoodTimeout;
}
void checkTimeout () {
if (timeout == 0) {
boolean pointInSnake = false;
do {
food.x = random(0, 7);
food.y = random(0, 7);
// Don't want to move the food to a point
//currently occupied by part of the snake
if (pointInPlist(food, snakeTail)) {
pointInSnake = true;
} else if (pointsAreEqual(food, snakeHead)) {
pointInSnake = true;
} else {
pointInSnake = false;
}
} while (pointInSnake);
updateDisplay();
}
if (timeout >= 0) {
timeout--;
}
}
void earnPoint () {
score++;
if (score % levelInterval == 0) {
currentRate += .5;
interval = 1000 / currentRate;
tailLength++;
}
}
void updateDisplay () {
matrix.clear();
drawFood();
drawSnake();
matrix.writeDisplay();
scoreboard.print(score);
scoreboard.writeDisplay();
}
void drawFood () {
matrix.drawPixel(food.x, food.y, foodColor);
}
void drawSnake () {
matrix.drawPixel(snakeHead.x, snakeHead.y, snakeColor);
if (snakeTail) {
struct plist tail = *snakeTail;
boolean i = true;
do {
matrix.drawPixel(tail.coord.x, tail.coord.y, snakeColor);
if (tail.next) {
tail = *(tail.next);
} else {
i = false;
}
} while(i);
}
}
void endGame () {
// interrupt();
// return;
for (int i = 0; i < boardHeight; i++) {
matrix.drawLine(0, i, 7, i, LED_RED);
matrix.writeDisplay();
delay(100);
}
matrix.blinkRate(1);
delay(2000);
matrix.blinkRate(0);
if (highScore()) {
saveHighScore();
displayCongrats();
}
initializeGame();
}
boolean highScore () {
int hScore = EEPROM.read(scoreAddr);
if (hScore && hScore != 255) {
return score > hScore;
} else {
return true;
}
}
void saveHighScore () {
EEPROM.write(scoreAddr, score);
}
void displayCongrats () {
matrix.setTextWrap(false); // we dont want text to wrap so it scrolls nicely
matrix.setTextSize(1);
matrix.setTextColor(LED_GREEN);
for (int8_t x=7; x>=-100; x--) {
matrix.clear();
matrix.setCursor(x,0);
matrix.print("You rock! High Score!");
matrix.writeDisplay();
delay(50);
}
}
struct point readJoystick () {
struct point joyval;
joyval.x = analogRead(joystickXPin);
joyval.y = analogRead(joystickYPin);
return joyval;
}
void interrupt () {
paused = true;
}