-
Notifications
You must be signed in to change notification settings - Fork 1
/
Snake.c
332 lines (302 loc) · 7.52 KB
/
Snake.c
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
//Snake
byte snakeP1[SIZE*SIZE];
byte snakeP2[SIZE*SIZE];
byte lengthP1;
byte lengthP2;
//Directions: 1 = up, 2 = right, 3 = down, 4 = left
byte directionP1;
byte directionP2;
byte xP1;
byte yP1;
byte xP2;
byte yP2;
byte appleX;
byte appleY;
bool gameOverSnakeFlag;
byte lastDirP1;
byte lastDirP2;
byte snakeSpeed;
// initialize snake
void startSnake() {
// Start position P1:
xP1 = 2;
yP1 = 0;
directionP1 = 3;
lengthP1 = 4;
snakeP1[0] = 3;
snakeP1[1] = 3;
snakeP1[2] = 3;
snakeP1[3] = 3;
// Start position P2:
xP2 = 11;
yP2 = 13;
directionP2 = 1;
lengthP2 = 4;
snakeP2[0] = 1;
snakeP2[1] = 1;
snakeP2[2] = 1;
snakeP2[3] = 1;
snakeSpeed = 30;
gameOverSnakeFlag = false;
drawCounter = 0;
generateApple();
snakeDraw(GREEN, YELLOW, 0);
snakeLoop();
}
// snake's own loop
void snakeLoop() {
// Input handling
reading[0] = digitalRead(DOWN);
reading[1] = digitalRead(TURN);
reading[2] = digitalRead(LEFT);
reading[3] = digitalRead(RIGHT);
for(int i = 0; i < 4; i++) {
if (reading[i] != lastButtonState[i]) {
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading[i] != buttonState[i]) {
buttonState[i] = reading[i];
if(i == 0 && buttonState[i] == HIGH) {
turnLeft(1);
} else if(i == 1 && buttonState[i] == HIGH) {
turnRight(2);
} else if(i == 2 && buttonState[i] == HIGH) {
turnRight(1);
} else if(i == 3 && buttonState[i] == HIGH) {
turnLeft(2);
}
}
}
lastButtonState[i] = reading[i];
}
// the snakes move on
if(drawCounter > 40) {
snakeMove();
lastDirP1 = directionP1;
lastDirP2 = directionP2;
// a player will loose if either he crashes into another snake, himself or a wall
if(snakeCollision(xP1, yP1, 1) > 0 || outOfBounds(1)) {
if(snakeCollision(xP2, yP2, 2) > 0 || outOfBounds(2)) {
// both lost? wtf
gameOverSnake(3);
} else {
gameOverSnake(1);
}
} else if(snakeCollision(xP2, yP2, 2) > 0 || outOfBounds(2)) {
gameOverSnake(2);
} else {
snakeDraw(GREEN, YELLOW, 0);
}
drawCounter = 0;
}
drawCounter++;
delay(snakeSpeed/2); // the speed will increase every two apples
if(!gameOverSnakeFlag) {
snakeLoop();
}
}
// hits given player a wall?
bool outOfBounds(byte player) {
if(player == 1) {
if(xP1 > 15 || xP1 < 0 || yP1 > 15 || yP1 < 0) {
return true;
}
} else if(player == 2) {
if(xP2 > 15 || xP2 < 0 || yP2 > 15 || yP2 < 0) {
return true;
}
}
return false;
}
void gameOverSnake(byte player) {
gameOverSnakeFlag = true;
switch(player) {
case 3:
for(int i = 0; i < 100; i++) {
snakeDraw((i % 2 == 0) ? YELLOW : GREEN, (i % 2 == 1) ? YELLOW : GREEN, strip_1.Color(random(256),random(256),random(256)));
delay(20);
}
break;
case 2:
for(int i = 0; i < 100; i++) {
snakeDraw(GREEN, (i % 2 == 0) ? YELLOW : RED, strip_1.Color(i*2,i*2,i*2));
delay(20);
}
break;
case 1:
for(int i = 0; i < 100; i++) {
snakeDraw((i % 2 == 0) ? GREEN : RED, YELLOW, strip_1.Color(i*2,i*2,i*2));
delay(20);
}
break;
}
}
void snakeMove() {
// Eat apples
if(snakeCollision(appleX, appleY, 0) == 1) {
lengthP1++;
snakeSpeed--; // the delay between loop()-calls decreases
generateApple();
} else if(snakeCollision(appleX, appleY, 0) == 2) {
lengthP2++;
snakeSpeed--;
generateApple();
}
// The snake moves on
switch(directionP1) {
case 1: yP1--; break;
case 2: xP1++; break;
case 3: yP1++; break;
case 4: xP1--; break;
}
for(byte i = lengthP1 - 1; i > 0; i--) {
snakeP1[i] = snakeP1[i-1];
}
snakeP1[0] = directionP1;
switch(directionP2) {
case 1: yP2--; break;
case 2: xP2++; break;
case 3: yP2++; break;
case 4: xP2--; break;
}
for(byte i = lengthP2 - 1; i > 0; i--) {
snakeP2[i] = snakeP2[i-1];
}
snakeP2[0] = directionP2;
}
void turnLeft(byte player) {
if(player == 1) {
switch(lastDirP1) { // so multiple presses on left or right don't let you turn backwards or to the other direction
case 4: directionP1 = 3; break;
case 3: directionP1 = 2; break;
case 2: directionP1 = 1; break;
case 1: directionP1 = 4; break;
}
}
if(player == 2) {
switch(lastDirP2) {
case 4: directionP2 = 3; break;
case 3: directionP2 = 2; break;
case 2: directionP2 = 1; break;
case 1: directionP2 = 4; break;
}
}
}
void turnRight(byte player) {
if(player == 1) {
switch(lastDirP1) {
case 4: directionP1 = 1; break;
case 3: directionP1 = 4; break;
case 2: directionP1 = 3; break;
case 1: directionP1 = 2; break;
}
}
if(player == 2) {
switch(lastDirP2) {
case 4: directionP2 = 1; break;
case 3: directionP2 = 4; break;
case 2: directionP2 = 3; break;
case 1: directionP2 = 2; break;
}
}
}
void generateApple() {
do {
appleX = random(16);
appleY = random(16);
} // find a place where no snake is
while(snakeCollision(appleX, appleY, 0) > 0);
}
// checks if given player at his head position x and y collides with himself or another snake
byte snakeCollision(byte x, byte y, byte player) {
byte tempX = xP1;
byte tempY = yP1;
byte i = 0;
if(player == 1) {
switch(snakeP1[i]) {
case 4: tempX++; break;
case 3: tempY--; break;
case 2: tempX--; break;
case 1: tempY++; break;
}
i = 1; // so he cannot collide with his own head
} else {
i = 0;
}
for(; i <= lengthP1; i++) { // the snake consists of an array of directions where the actual shape can be reconstructed through iteration
if(tempX == x && tempY == y) {
return 1;
}
switch(snakeP1[i]) {
case 4: tempX++; break;
case 3: tempY--; break;
case 2: tempX--; break;
case 1: tempY++; break;
}
}
// same procedure for player 2
tempX = xP2;
tempY = yP2;
i = 0;
if(player == 2) {
switch(snakeP2[i]) {
case 4: tempX++; break;
case 3: tempY--; break;
case 2: tempX--; break;
case 1: tempY++; break;
}
i = 1;
} else {
i = 0;
}
for(; i <= lengthP2; i++) {
if(tempX == x && tempY == y) {
return 2;
}
switch(snakeP2[i]) {
case 4: tempX++; break;
case 3: tempY--; break;
case 2: tempX--; break;
case 1: tempY++; break;
}
}
return 0;
}
// draws player 1 with color c1 and player 2 with c2 and given background color
void snakeDraw(uint32_t c1, uint32_t c2, uint32_t background) {
myFill(background);
// Apple
myDraw(appleX, appleY, RED);
// Player 1
byte tempX = xP1;
byte tempY = yP1;
myDraw(tempX, tempY, c1);
// iterate through snake chain
for(byte i = 0; i < lengthP1; i++) {
switch(snakeP1[i]) {
case 4: tempX++; break;
case 3: tempY--; break;
case 2: tempX--; break;
case 1: tempY++; break;
}
myDraw(tempX, tempY, c1);
}
// Player 2
tempX = xP2;
tempY = yP2;
myDraw(tempX, tempY, c2);
for(byte i = 0; i < lengthP2; i++) {
switch(snakeP2[i]) {
case 4: tempX++; break;
case 3: tempY--; break;
case 2: tempX--; break;
case 1: tempY++; break;
}
myDraw(tempX, tempY, c2);
}
myShow();
}