-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnake.cpp
More file actions
174 lines (143 loc) · 4.88 KB
/
snake.cpp
File metadata and controls
174 lines (143 loc) · 4.88 KB
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
#include "mbed.h"
#include "globals.h"
#include "N5110.h"
#include "Joystick.h"
#include "snake.h"
#include <cstdio>
Point snake[128];
int snake_length = 3;
Point food;
int dx = 1, dy = 0;
int snakeScale = 3; //must be 1 2 or 4
int refreshPeriod = 200; //ms
void draw_snake() {
lcd.clear();
for (int i = 0; i < snake_length; i++) { //draw snake body
lcd.drawRect(snake[i].x, snake[i].y, snakeScale, snakeScale, FILL_BLACK);
}
lcd.drawRect(food.x, food.y, snakeScale, snakeScale, FILL_TRANSPARENT); //draw food
lcd.drawRect(0, 0, WIDTH, HEIGHT, FILL_TRANSPARENT);
lcd.refresh();
}
void spawn_food() {
food.x = rand() % (WIDTH/snakeScale) * snakeScale;
food.y = rand() % (HEIGHT/snakeScale) * snakeScale;
}
void update_snake() {
printf(" player X: %d Y: %d \n", snake[0].x, snake[0].y);
Point new_head = {snake[0].x + dx, snake[0].y + dy};
printf(" player X: %d Y: %d food X: %d Y: %d\n", new_head.x, new_head.y, food.x, food.y); //for debugging
for (int i = 0; i < snake_length; i++) { //check collison with snake body
if (snake[i].x == new_head.x && snake[i].y == new_head.y) {
printf("Game Over 1!\n\n");
game_over();
}
}
for (int i = snake_length; i > 0; i--) {
snake[i] = snake[i - 1];
}
snake[0] = new_head;
if (new_head.x < 0 || new_head.x >= WIDTH || new_head.y < 0 || new_head.y >= HEIGHT) { //check collison with border
printf("Game Over 2!\n");
for (int i = 0 ; i< snake_length ; i++) {
printf("i: %d X: %d Y: %d \n", i, snake[i].x, snake[i].y);
}
game_over();
}
if (new_head.x == food.x && new_head.y == food.y) { // if food has been eaten
if (stopMusic) {
stopMusic = !stopMusic;
tone(buzzer, 500, 100);
stopMusic = !stopMusic;
}
else {tone(buzzer, 500, 100);}
snake_length++;
spawn_food();
}
}
void read_input() {
Direction dir = joystick.get_direction();
if (dir == N && dy != snakeScale) { dx = 0; dy = -snakeScale; }
if (dir == S && dy != -snakeScale) { dx = 0; dy = snakeScale; }
if (dir == W && dx != snakeScale) { dx = -snakeScale; dy = 0; }
if (dir == E && dx != -snakeScale) { dx = snakeScale; dy = 0; }
}
void playSnake() {
snakeMenu();
init_snake();
while (1) {
read_input();
update_snake();
draw_snake();
if (g_buttonA_flag) { // check if menu button pressed
ThisThread::sleep_for(500ms);
g_buttonA_flag = 0;
printf(" exiting snake \n");
break;
}
thread_sleep_for(refreshPeriod);
}
}
void game_over() {
lcd.clear();
lcd.drawRect(0, 0, 84, 48, FILL_TRANSPARENT); // Draw screen border
char buffer[32];
sprintf(buffer, "food eaten: %d", snake_length - 3);
lcd.printString(" GAME OVER", 1, 2);
lcd.printString(buffer, 1, 3);
lcd.refresh();
if (stopMusic) {
stopMusic = !stopMusic;
tone(buzzer, 1000, 300);
tone(buzzer, 500, 200);
tone(buzzer, 300, 300);
stopMusic = !stopMusic;}
else {
tone(buzzer, 1000, 300);
tone(buzzer, 500, 200);
tone(buzzer, 300, 300);}
ThisThread::sleep_for(5000ms);
snakeMenu();
init_snake();
ThisThread::sleep_for(500ms);
}
void init_snake() {
//init snake position
int startX = (WIDTH / 2 / snakeScale) * snakeScale; //ensures snake and food on the same grid
int startY = (HEIGHT / 2 / snakeScale) * snakeScale;
snake[0] = {startX, startY}; //start head in middle
snake[1] = {startX - snakeScale, startY};
snake[2] = {startX - 2 * snakeScale, startY};
snake_length = 3;
dx = snakeScale;
dy = 0;
spawn_food();
}
void snakeMenu() {
char scaleBuffer[32];
char refreshBuffer[32];
g_joystick_flag = false;
while (g_joystick_flag == false) {
lcd.clear();
lcd.drawRect(0, 0, 84, 48, FILL_TRANSPARENT); // Draw screen border
lcd.printString("press joystick", 2, 1);
lcd.printString(" to start", 1, 2);
sprintf(scaleBuffer, "SCALE: %d", snakeScale);
lcd.printString(scaleBuffer, 2, 3);
if (joystick.get_direction() == N && snakeScale < 10) {
snakeScale++;
} else if (joystick.get_direction() == S && snakeScale > 1) {
snakeScale--;
}
sprintf(refreshBuffer, "REFRESH: %d", refreshPeriod);
lcd.printString(refreshBuffer, 2, 4);
if (joystick.get_direction() == E && refreshPeriod < 1000) {
refreshPeriod += 50;
} else if (joystick.get_direction() == W && refreshPeriod > 50) {
refreshPeriod -= 50;
}
lcd.refresh();
ThisThread::sleep_for(400ms);
};
g_joystick_flag = false;
}