forked from slawa19/IKEA-LED-Table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnakeGame.ino
168 lines (149 loc) · 4.01 KB
/
snakeGame.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
/* LedTable
*
* Written by: Klaas De Craemer, Ing. David Hrbaty
*
* Snake game
*/
void snakeInit(){
//Snake start position and direction & initialise variables
curLength = 3;
xs[0]=3; xs[1]=2; xs[2]=1;
ys[0]=FIELD_HEIGHT/2; ys[1]=FIELD_HEIGHT/2; ys[2]=FIELD_HEIGHT/2;
dir = DIR_RIGHT;
score = 0;
//Generate random apple position
ax = random(FIELD_WIDTH-1);
ay = random(FIELD_HEIGHT-1);
snakeGameOver = false;
}
void runSnake(){
snakeInit();
unsigned long prevUpdateTime = 0;
boolean snakeRunning = true;
while(snakeRunning){
//Check self-collision with snake
int i=curLength-1;
while (i>=2){
if (collide(xs[0], xs[i], ys[0], ys[i], SNAKEWIDTH, SNAKEWIDTH, SNAKEWIDTH, SNAKEWIDTH)){
Serial.println("Selfcollision");
die();
}
i = i-1;
}
if (snakeGameOver){
snakeRunning = false;
break;
}
//Check collision of snake head with apple
if (collide(xs[0], ax, ys[0], ay, SNAKEWIDTH, SNAKEWIDTH, SNAKEWIDTH, SNAKEWIDTH)){
//Increase score and snake length;
score = score +1;
curLength = curLength +1;
//Add snake segment with temporary position of new segments
xs[curLength-1] = 255;
ys[curLength-1] = 255;
//Generate new apple position
ax = random(FIELD_WIDTH-1);
ay = random(FIELD_HEIGHT-1);
//Generate apple color
acolor = colorLib[random(1)];
int i=curLength-1;
for(int i=0; i<curLength; i++) {
if (collide(ax, xs[i], ay, ys[i], SNAKEWIDTH, SNAKEWIDTH, SNAKEWIDTH, SNAKEWIDTH)){
ax = random(FIELD_WIDTH-1);
ay = random(FIELD_HEIGHT-1);
i=0;
}
}
}
//Shift snake position array by one
i = curLength-1;
while (i>=1){
xs[i] = xs[i-1];
ys[i] = ys[i-1];
i = i-1;
}
//Determine new position of head of snake
if (dir == DIR_RIGHT){
xs[0] = xs[0] + SNAKEWIDTH;
}
else if (dir == DIR_LEFT){
xs[0] = xs[0] - SNAKEWIDTH;
}
else if (dir == DIR_UP){
ys[0] = ys[0] - SNAKEWIDTH;
}
else {//DOWN
ys[0] = ys[0] + SNAKEWIDTH;
}
//Check if outside playing field
if ((xs[0]<0) || (xs[0]>=FIELD_WIDTH) || (ys[0]<0) || (ys[0]>=FIELD_HEIGHT)){
if (xs[0]<0) {xs[0] =FIELD_WIDTH -1;}
else if (xs[0]>=FIELD_WIDTH) {xs[0] = 0;}
else if (ys[0]<0) {ys[0] =FIELD_HEIGHT -1;}
else if (ys[0]>=FIELD_HEIGHT) {ys[0] = 0;}
}
clearTablePixels();
//Draw apple
setTablePixel(ax,ay,YELLOW);
//Draw snake
for (int i=0; i<curLength; i++){
setTablePixel(xs[i], ys[i], WHITE);
}
showPixels();
//Check buttons and set snake movement direction while we are waiting to draw the next move
unsigned long curTime;
boolean dirChanged = false;
do{
readInput();
if (curControl == BTN_EXIT){
snakeRunning = false;
break;
}
if (curControl != BTN_NONE && !dirChanged){//Can only change direction once per loop
dirChanged = true;
setDirection();
}
curTime = millis();
}
while ((curTime - prevUpdateTime) <250);//Once enough time has passed, proceed. The lower this number, the faster the game is
prevUpdateTime = curTime;
}
fadeOut();
char buf[4];
int len = sprintf(buf, "%i", score);
scrollTextBlocked(buf,len,WHITE);
}
/* Set direction from current button state */
void setDirection(){
switch(curControl){
case BTN_LEFT:
dir = DIR_LEFT;
break;
case BTN_RIGHT:
dir = DIR_RIGHT;
break;
case BTN_DOWN:
dir = DIR_DOWN;
break;
case BTN_UP:
dir = DIR_UP;
break;
case BTN_START:
break;
}
}
/* Ending, show score */
void die(){
snakeGameOver = true;
Serial.println("die");
}
/* Collision detection function */
boolean collide(int x1, int x2, int y1, int y2, int w1, int w2, int h1, int h2){
if ((x1+w1>x2) && (x1<x2+w2) && (y1+h1>y2) && (y1<y2+h2)){
return true;
}
else {
return false;
}
}