forked from technezio/snake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.ino
89 lines (77 loc) · 1.38 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
/*
* Simple remake of the classic Snake game, using Arduino and
* an LED matrix, optimized for MAX7219 driver.
* By GLGPrograms
* GPL-3.0
*/
#include <ledMatrix.h>
#include "snake_class.h"
ledMatrix myMatrix(8,10,12);
Snake mySnake;
boolean eaten = true;
char val;
byte x;
byte y;
void setup()
{
Serial.begin(9600);
myMatrix.setBrightness(2);
randomSeed( analogRead(A0) );
}
void loop()
{
val = '0';
if( Serial.available() > 0 )
{
val = Serial.read();
// Flush the buffer
while( Serial.available() )
Serial.read();
}
switch(val)
{
case 'w':
mySnake.changeDir(UP);
break;
case 'a':
mySnake.changeDir(LEFT);
break;
case 's':
mySnake.changeDir(DOWN);
break;
case 'd':
mySnake.changeDir(RIGHT);
break;
}
mySnake.move();
// If apple has been eaten, draw new one
if( eaten )
{
mySnake.newApple();
eaten = false;
}
// else check if the snake eats it
else if ( mySnake.eat() )
{
eaten = true;
mySnake.append();
}
snakePrint(mySnake);
delay(500);
}
void snakePrint(const Snake& s)
{
Snake::Dot *tmp = s.head;
// Clear matrix
myMatrix.flush();
// Print snake
for (byte i = 0; i < s.lenght; i++)
{
myMatrix.dot(tmp -> x, tmp -> y);
tmp = tmp -> next;
}
// Print apple
myMatrix.dot(s.apple[0], s.apple[1]);
// Actually display matrix content
myMatrix.load();
}