-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSnake.ino
384 lines (354 loc) · 7 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
#include <LiquidCrystal.h>
#include <Keypad.h>
//upper snake in 8x5
byte up[] = {
B11111,
B11111,
B11111,
B11111,
B00000,
B00000,
B00000,
B00000
};
//lower snake in 8x5
byte down[] = {
B00000,
B00000,
B00000,
B00000,
B11111,
B11111,
B11111,
B11111
};
//full snake in 8x5
byte full[] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111
};
//upper food in 8x5
byte food_up[] = {
B00000,
B00011,
B11111,
B01010,
B00000,
B00000,
B00000,
B00000
};
//lower food in 8x5
byte food_down[] = {
B00000,
B00000,
B00000,
B00000,
B00000,
B00011,
B11111,
B01010
};
//snake above food in 8x5
byte snake_up_food_down[] = {
B11111,
B11111,
B11111,
B11111,
B00000,
B00011,
B11111,
B01010
};
//snake below food in 8x5
byte snake_down_food_up[] = {
B00000,
B00011,
B11111,
B01010,
B11111,
B11111,
B11111,
B11111
};
//keypad init
const int ROW_NUM = 4; //four rows
const int COLUMN_NUM = 3; //three columns (ignoring the 4th)
char keys[ROW_NUM][COLUMN_NUM] = {
{'1','2','3'},
{'4','5','6'},
{'7','8','9'},
{'*','0','#'}
};
byte pin_rows[ROW_NUM] = {8, 7, 6, 5};
byte pin_column[COLUMN_NUM] = {4, 3, 2};
Keypad keypad = Keypad(makeKeymap(keys), pin_rows, pin_column, ROW_NUM, COLUMN_NUM);
//LCD init
LiquidCrystal lcd(A4, A5, 9, 10, 11, A3);
//pins
const int pin_piezo = A0;
//game matrix (score excluded)
const int X = 14;
const int Y = 4;
int matrix[X][Y] = {{0}};
//max snake length (and win condition)
const int MAX_LENGTH = 42;
//snake position
int snakeX[MAX_LENGTH] = {0};
int snakeY[MAX_LENGTH] = {0};
int moveX = 0; //X movement in this moment
int moveY = 0; //Y movement in this moment
//state variables
char key;
int counter = 0;
int score = 0;
bool failure = false;
//displaying the game matrix
//empty == 0
//snake == 1
//food == 2
void display(){
for(int j=0; j<Y; j+=2){
for(int i=0; i<X; i++){
if(j == 0){
lcd.setCursor(i,0);
}
else{
lcd.setCursor(i,1);
}
if(matrix[i][j] == 0 && matrix[i][j+1] == 0){
lcd.print(' ');
}
else if(matrix[i][j] == 1 && matrix[i][j+1] == 0){
lcd.write(byte(0));
}
else if(matrix[i][j] == 0 && matrix[i][j+1] == 1){
lcd.write(byte(1));
}
else if(matrix[i][j] == 1 && matrix[i][j+1] == 1){
lcd.write(byte(2));
}
else if(matrix[i][j] == 2 && matrix[i][j+1] == 0){
lcd.write(byte(3));
}
else if(matrix[i][j] == 0 && matrix[i][j+1] == 2){
lcd.write(byte(4));
}
else if(matrix[i][j] == 1 && matrix[i][j+1] == 2){
lcd.write(byte(5));
}
else if(matrix[i][j] == 2 && matrix[i][j+1] == 1){
lcd.write(byte(6));
}
}
}
}
//displaying the score ("SC" + 2 digits)
void display_score(){
char tmp;
lcd.setCursor(14,0);
lcd.print('S');
lcd.setCursor(15,0);
lcd.print('C');
lcd.setCursor(14,1);
tmp = '0' + ((score%100)/10);
lcd.print(tmp);
lcd.setCursor(15,1);
tmp = '0' + ((score%10));
lcd.print(tmp);
}
//generating 1 food in random place
void random_food(){
int randX = 0;
int randY = 0;
//making sure that new food does not spawn on the snake
do{
randX = random(0, X);
randY = random(0, Y);
}while(matrix[randX][randY] != 0);
matrix[randX][randY] = 2;
}
//resetting the game
void game_reset(){
//cleaning up
moveX = 0;
moveY = 0;
score = 0;
failure = false;
for(int j=0; j<Y; j++){
for(int i=0; i<X; i++){
matrix[i][j] = 0;
}
}
for(int i=0; i<MAX_LENGTH; i++){
snakeX[i] = -1;
snakeY[i] = -1;
}
//initial snake position
snakeX[0] = 1;
snakeY[0] = 1;
matrix[snakeX[0]][snakeY[0]] = 1;
//initial movement is staying still
moveX = 0;
moveY = 0;
//first food
random_food();
tone(pin_piezo, 156, 100);
delay(250);
tone(pin_piezo, 311, 100);
}
void make_move(){
int nextX = snakeX[0] + moveX;
int nextY = snakeY[0] + moveY;
int tmpX, tmpY;
if(nextX < 0 || nextX >= X || nextY < 0 || nextY >= Y || (matrix[nextX][nextY] == 1 && (moveX != 0 || moveY != 0))){
//failure conditions fulfilled
failure = true;
}
else{
if(matrix[nextX][nextY] == 2){
//eating
tone(pin_piezo, 622, 30);
score++;
for(int i=0; i<=score; i++){
matrix[nextX][nextY] = 1;
tmpX = snakeX[i];
tmpY = snakeY[i];
snakeX[i] = nextX;
snakeY[i] = nextY;
nextX = tmpX;
nextY = tmpY;
}
//1 new food
random_food();
}
else{
//regular movement
for(int i=0; i<=score; i++){
matrix[snakeX[i]][snakeY[i]] = 0;
matrix[nextX][nextY] = 1;
tmpX = snakeX[i];
tmpY = snakeY[i];
snakeX[i] = nextX;
snakeY[i] = nextY;
nextX = tmpX;
nextY = tmpY;
}
}
}
}
void setup() {
//debug serial output
Serial.begin(9600);
//piezo init
pinMode(pin_piezo, OUTPUT);
//lcd init
pinMode(A3, OUTPUT);
pinMode(A4, OUTPUT);
pinMode(A5, OUTPUT);
lcd.begin(16, 2);
lcd.setCursor(0,0);
//loading graphics 8x5 for LCD
lcd.createChar(0, up);
lcd.createChar(1, down);
lcd.createChar(2, full);
lcd.createChar(3, food_up);
lcd.createChar(4, food_down);
lcd.createChar(5, snake_up_food_down);
lcd.createChar(6, snake_down_food_up);
//(re)setting the game
game_reset();
}
//main logic loop
void loop() {
display_score();
display();
counter = 0;
//waiting for input for 0.5s, if nothing then auto move
while( (key = keypad.getKey()) == NO_KEY && counter < 500 ){
delay(1);
counter++;
}
if(key == NO_KEY){
key = 'M';
}
Serial.println(key); //DEBUG / MOVE LOG
if(score >= MAX_LENGTH){
//WIN
tone(pin_piezo, 392, 200);
delay(300);
tone(pin_piezo, 330, 200);
delay(300);
tone(pin_piezo, 392, 200);
delay(300);
tone(pin_piezo, 330, 200);
delay(300);
tone(pin_piezo, 392, 200);
while(key != '0'){
key = keypad.waitForKey();
};
delay(300);
game_reset();
}
else if(key == '0' || failure == true){
//DEFEAT
tone(pin_piezo, 233, 100);
delay(200);
tone(pin_piezo, 440, 100);
delay(200);
tone(pin_piezo, 233, 100);
delay(200);
tone(pin_piezo, 440, 100);
delay(200);
tone(pin_piezo, 233, 100);
while(key != '0'){
key = keypad.waitForKey();
};
delay(300);
game_reset();
}
else if(key == '2'){
//moving up
tone(pin_piezo, 311, 30);
moveX = 0;
moveY = -1;
make_move();
}
else if(key == '6'){
//moving right
tone(pin_piezo, 311, 30);
moveX = 1;
moveY = 0;
make_move();
}
else if(key == '8'){
//moving down
tone(pin_piezo, 311, 30);
moveX = 0;
moveY = 1;
make_move();
}
else if(key == '4'){
//moving left
tone(pin_piezo, 311, 30);
moveX = -1;
moveY = 0;
make_move();
}
else if(key == 'M'){
//auto move (nothing pressed)
tone(pin_piezo, 156, 30);
make_move();
}
else{
//wrong key
tone(pin_piezo, 156, 30);
}
delay(30);
}