-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathSnake.ino
641 lines (519 loc) · 22.1 KB
/
Snake.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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
* Copyright (c) 2018 Ondrej Telka. (https://ondrej.xyz/)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "LedControl.h" // LedControl library is used for controlling a LED matrix. Find it using Library Manager or download zip here: https://github.com/wayoda/LedControl
// --------------------------------------------------------------- //
// ------------------------- user config ------------------------- //
// --------------------------------------------------------------- //
// there are defined all the pins
struct Pin {
static const short joystickX = A2; // joystick X axis pin
static const short joystickY = A3; // joystick Y axis pin
static const short joystickVCC = 15; // virtual VCC for the joystick (Analog 1) (to make the joystick connectable right next to the arduino nano)
static const short joystickGND = 14; // virtual GND for the joystick (Analog 0) (to make the joystick connectable right next to the arduino nano)
static const short potentiometer = A7; // potentiometer for snake speed control
static const short CLK = 10; // clock for LED matrix
static const short CS = 11; // chip-select for LED matrix
static const short DIN = 12; // data-in for LED matrix
};
// LED matrix brightness: between 0(darkest) and 15(brightest)
const short intensity = 8;
// lower = faster message scrolling
const short messageSpeed = 5;
// initial snake length (1...63, recommended 3)
const short initialSnakeLength = 3;
void setup() {
Serial.begin(115200); // set the same baud rate on your Serial Monitor
initialize(); // initialize pins & LED matrix
calibrateJoystick(); // calibrate the joystick home (do not touch it)
showSnakeMessage(); // scrolls the 'snake' message around the matrix
}
void loop() {
generateFood(); // if there is no food, generate one
scanJoystick(); // watches joystick movements & blinks with food
calculateSnake(); // calculates snake parameters
handleGameStates();
// uncomment this if you want the current game board to be printed to the serial (slows down the game a bit)
// dumpGameBoard();
}
// --------------------------------------------------------------- //
// -------------------- supporting variables --------------------- //
// --------------------------------------------------------------- //
LedControl matrix(Pin::DIN, Pin::CLK, Pin::CS, 1);
struct Point {
int row = 0, col = 0;
Point(int row = 0, int col = 0): row(row), col(col) {}
};
struct Coordinate {
int x = 0, y = 0;
Coordinate(int x = 0, int y = 0): x(x), y(y) {}
};
bool win = false;
bool gameOver = false;
// primary snake head coordinates (snake head), it will be randomly generated
Point snake;
// food is not anywhere yet
Point food(-1, -1);
// construct with default values in case the user turns off the calibration
Coordinate joystickHome(500, 500);
// snake parameters
int snakeLength = initialSnakeLength; // choosed by the user in the config section
int snakeSpeed = 1; // will be set according to potentiometer value, cannot be 0
int snakeDirection = 0; // if it is 0, the snake does not move
// direction constants
const short up = 1;
const short right = 2;
const short down = 3; // 'down - 2' must be 'up'
const short left = 4; // 'left - 2' must be 'right'
// threshold where movement of the joystick will be accepted
const int joystickThreshold = 160;
// artificial logarithmity (steepness) of the potentiometer (-1 = linear, 1 = natural, bigger = steeper (recommended 0...1))
const float logarithmity = 0.4;
// snake body segments storage
int gameboard[8][8] = {};
// --------------------------------------------------------------- //
// -------------------------- functions -------------------------- //
// --------------------------------------------------------------- //
// if there is no food, generate one, also check for victory
void generateFood() {
if (food.row == -1 || food.col == -1) {
// self-explanatory
if (snakeLength >= 64) {
win = true;
return; // prevent the food generator from running, in this case it would run forever, because it will not be able to find a pixel without a snake
}
// generate food until it is in the right position
do {
food.col = random(8);
food.row = random(8);
} while (gameboard[food.row][food.col] > 0);
}
}
// watches joystick movements & blinks with food
void scanJoystick() {
int previousDirection = snakeDirection; // save the last direction
long timestamp = millis();
while (millis() < timestamp + snakeSpeed) {
// calculate snake speed exponentially (10...1000ms)
float raw = mapf(analogRead(Pin::potentiometer), 0, 1023, 0, 1);
snakeSpeed = mapf(pow(raw, 3.5), 0, 1, 10, 1000); // change the speed exponentially
if (snakeSpeed == 0) snakeSpeed = 1; // safety: speed can not be 0
// determine the direction of the snake
analogRead(Pin::joystickY) < joystickHome.y - joystickThreshold ? snakeDirection = up : 0;
analogRead(Pin::joystickY) > joystickHome.y + joystickThreshold ? snakeDirection = down : 0;
analogRead(Pin::joystickX) < joystickHome.x - joystickThreshold ? snakeDirection = left : 0;
analogRead(Pin::joystickX) > joystickHome.x + joystickThreshold ? snakeDirection = right : 0;
// ignore directional change by 180 degrees (no effect for non-moving snake)
snakeDirection + 2 == previousDirection && previousDirection != 0 ? snakeDirection = previousDirection : 0;
snakeDirection - 2 == previousDirection && previousDirection != 0 ? snakeDirection = previousDirection : 0;
// intelligently blink with the food
matrix.setLed(0, food.row, food.col, millis() % 100 < 50 ? 1 : 0);
}
}
// calculate snake movement data
void calculateSnake() {
switch (snakeDirection) {
case up:
snake.row--;
fixEdge();
matrix.setLed(0, snake.row, snake.col, 1);
break;
case right:
snake.col++;
fixEdge();
matrix.setLed(0, snake.row, snake.col, 1);
break;
case down:
snake.row++;
fixEdge();
matrix.setLed(0, snake.row, snake.col, 1);
break;
case left:
snake.col--;
fixEdge();
matrix.setLed(0, snake.row, snake.col, 1);
break;
default: // if the snake is not moving, exit
return;
}
// if there is a snake body segment, this will cause the end of the game (snake must be moving)
if (gameboard[snake.row][snake.col] > 1 && snakeDirection != 0) {
gameOver = true;
return;
}
// check if the food was eaten
if (snake.row == food.row && snake.col == food.col) {
food.row = -1; // reset food
food.col = -1;
// increment snake length
snakeLength++;
// increment all the snake body segments
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (gameboard[row][col] > 0 ) {
gameboard[row][col]++;
}
}
}
}
// add new segment at the snake head location
gameboard[snake.row][snake.col] = snakeLength + 1; // will be decremented in a moment
// decrement all the snake body segments, if segment is 0, turn the corresponding led off
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
// if there is a body segment, decrement it's value
if (gameboard[row][col] > 0 ) {
gameboard[row][col]--;
}
// display the current pixel
matrix.setLed(0, row, col, gameboard[row][col] == 0 ? 0 : 1);
}
}
}
// causes the snake to appear on the other side of the screen if it gets out of the edge
void fixEdge() {
snake.col < 0 ? snake.col += 8 : 0;
snake.col > 7 ? snake.col -= 8 : 0;
snake.row < 0 ? snake.row += 8 : 0;
snake.row > 7 ? snake.row -= 8 : 0;
}
void handleGameStates() {
if (gameOver || win) {
unrollSnake();
showScoreMessage(snakeLength - initialSnakeLength);
if (gameOver) showGameOverMessage();
else if (win) showWinMessage();
// re-init the game
win = false;
gameOver = false;
snake.row = random(8);
snake.col = random(8);
food.row = -1;
food.col = -1;
snakeLength = initialSnakeLength;
snakeDirection = 0;
memset(gameboard, 0, sizeof(gameboard[0][0]) * 8 * 8);
matrix.clearDisplay(0);
}
}
void unrollSnake() {
// switch off the food LED
matrix.setLed(0, food.row, food.col, 0);
delay(800);
// flash the screen 5 times
for (int i = 0; i < 5; i++) {
// invert the screen
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
matrix.setLed(0, row, col, gameboard[row][col] == 0 ? 1 : 0);
}
}
delay(20);
// invert it back
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
matrix.setLed(0, row, col, gameboard[row][col] == 0 ? 0 : 1);
}
}
delay(50);
}
delay(600);
for (int i = 1; i <= snakeLength; i++) {
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (gameboard[row][col] == i) {
matrix.setLed(0, row, col, 0);
delay(100);
}
}
}
}
}
// calibrate the joystick home for 10 times
void calibrateJoystick() {
Coordinate values;
for (int i = 0; i < 10; i++) {
values.x += analogRead(Pin::joystickX);
values.y += analogRead(Pin::joystickY);
}
joystickHome.x = values.x / 10;
joystickHome.y = values.y / 10;
}
void initialize() {
pinMode(Pin::joystickVCC, OUTPUT);
digitalWrite(Pin::joystickVCC, HIGH);
pinMode(Pin::joystickGND, OUTPUT);
digitalWrite(Pin::joystickGND, LOW);
matrix.shutdown(0, false);
matrix.setIntensity(0, intensity);
matrix.clearDisplay(0);
randomSeed(analogRead(A5));
snake.row = random(8);
snake.col = random(8);
}
void dumpGameBoard() {
String buff = "\n\n\n";
for (int row = 0; row < 8; row++) {
for (int col = 0; col < 8; col++) {
if (gameboard[row][col] < 10) buff += " ";
if (gameboard[row][col] != 0) buff += gameboard[row][col];
else if (col == food.col && row == food.row) buff += "@";
else buff += "-";
buff += " ";
}
buff += "\n";
}
Serial.println(buff);
}
// --------------------------------------------------------------- //
// -------------------------- messages --------------------------- //
// --------------------------------------------------------------- //
const PROGMEM bool snakeMessage[8][56] = {
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1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},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{0, 0, 1, 1, 1, 1, 0, 0}
},
{
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 1, 1, 1, 0, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{0, 0, 1, 1, 1, 1, 1, 0},
{0, 0, 0, 0, 0, 1, 1, 0},
{0, 1, 1, 0, 0, 1, 1, 0},
{0, 0, 1, 1, 1, 1, 0, 0}
}
};
// scrolls the 'snake' message around the matrix
void showSnakeMessage() {
[&] {
for (int d = 0; d < sizeof(snakeMessage[0]) - 7; d++) {
for (int col = 0; col < 8; col++) {
delay(messageSpeed);
for (int row = 0; row < 8; row++) {
// this reads the byte from the PROGMEM and displays it on the screen
matrix.setLed(0, row, col, pgm_read_byte(&(snakeMessage[row][col + d])));
}
}
// if the joystick is moved, exit the message
if (analogRead(Pin::joystickY) < joystickHome.y - joystickThreshold
|| analogRead(Pin::joystickY) > joystickHome.y + joystickThreshold
|| analogRead(Pin::joystickX) < joystickHome.x - joystickThreshold
|| analogRead(Pin::joystickX) > joystickHome.x + joystickThreshold) {
return; // return the lambda function
}
}
}();
matrix.clearDisplay(0);
// wait for joystick co come back
while (analogRead(Pin::joystickY) < joystickHome.y - joystickThreshold
|| analogRead(Pin::joystickY) > joystickHome.y + joystickThreshold
|| analogRead(Pin::joystickX) < joystickHome.x - joystickThreshold
|| analogRead(Pin::joystickX) > joystickHome.x + joystickThreshold) {}
}
// scrolls the 'game over' message around the matrix
void showGameOverMessage() {
[&] {
for (int d = 0; d < sizeof(gameOverMessage[0]) - 7; d++) {
for (int col = 0; col < 8; col++) {
delay(messageSpeed);
for (int row = 0; row < 8; row++) {
// this reads the byte from the PROGMEM and displays it on the screen
matrix.setLed(0, row, col, pgm_read_byte(&(gameOverMessage[row][col + d])));
}
}
// if the joystick is moved, exit the message
if (analogRead(Pin::joystickY) < joystickHome.y - joystickThreshold
|| analogRead(Pin::joystickY) > joystickHome.y + joystickThreshold
|| analogRead(Pin::joystickX) < joystickHome.x - joystickThreshold
|| analogRead(Pin::joystickX) > joystickHome.x + joystickThreshold) {
return; // return the lambda function
}
}
}();
matrix.clearDisplay(0);
// wait for joystick co come back
while (analogRead(Pin::joystickY) < joystickHome.y - joystickThreshold
|| analogRead(Pin::joystickY) > joystickHome.y + joystickThreshold
|| analogRead(Pin::joystickX) < joystickHome.x - joystickThreshold
|| analogRead(Pin::joystickX) > joystickHome.x + joystickThreshold) {}
}
// scrolls the 'win' message around the matrix
void showWinMessage() {
// not implemented yet // TODO: implement it
}
// scrolls the 'score' message with numbers around the matrix
void showScoreMessage(int score) {
if (score < 0 || score > 99) return;
// specify score digits
int second = score % 10;
int first = (score / 10) % 10;
[&] {
for (int d = 0; d < sizeof(scoreMessage[0]) + 2 * sizeof(digits[0][0]); d++) {
for (int col = 0; col < 8; col++) {
delay(messageSpeed);
for (int row = 0; row < 8; row++) {
if (d <= sizeof(scoreMessage[0]) - 8) {
matrix.setLed(0, row, col, pgm_read_byte(&(scoreMessage[row][col + d])));
}
int c = col + d - sizeof(scoreMessage[0]) + 6; // move 6 px in front of the previous message
// if the score is < 10, shift out the first digit (zero)
if (score < 10) c += 8;
if (c >= 0 && c < 8) {
if (first > 0) matrix.setLed(0, row, col, pgm_read_byte(&(digits[first][row][c]))); // show only if score is >= 10 (see above)
} else {
c -= 8;
if (c >= 0 && c < 8) {
matrix.setLed(0, row, col, pgm_read_byte(&(digits[second][row][c]))); // show always
}
}
}
}
// if the joystick is moved, exit the message
if (analogRead(Pin::joystickY) < joystickHome.y - joystickThreshold
|| analogRead(Pin::joystickY) > joystickHome.y + joystickThreshold
|| analogRead(Pin::joystickX) < joystickHome.x - joystickThreshold
|| analogRead(Pin::joystickX) > joystickHome.x + joystickThreshold) {
return; // return the lambda function
}
}
}();
matrix.clearDisplay(0);
// // wait for joystick co come back
// while (analogRead(Pin::joystickY) < joystickHome.y - joystickThreshold
// || analogRead(Pin::joystickY) > joystickHome.y + joystickThreshold
// || analogRead(Pin::joystickX) < joystickHome.x - joystickThreshold
// || analogRead(Pin::joystickX) > joystickHome.x + joystickThreshold) {}
}
// standard map function, but with floats
float mapf(float x, float in_min, float in_max, float out_min, float out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}