-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnake_game.cpp
225 lines (193 loc) · 5.79 KB
/
snake_game.cpp
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
#include "arduino.h"
#include "snake_game.h"
#include "graphics.h"
#include "pitches.h"
enum Dir {
left,
right,
up,
down
};
struct Point {
byte x;
byte y;
Point(byte x = 0, byte y = 0): x(x), y(y) {}
Point shifted_by(Dir dir, byte dist = 1) const {
switch (dir) {
case left: return Point(x - dist, y); break;
case right: return Point(x + dist, y); break;
case up: return Point(x, y - dist); break;
case down: return Point(x, y + dist); break;
}
}
static Point rand_in_disp(const Display& disp) {
return Point(random(disp.width), random(disp.height));
}
bool operator==(const Point& p) {
return p.x == x && p.y == y;
}
};
class Player {
public:
Dir facing;
Dir last_facing;
Point points[200];
byte points_start;
byte points_len;
bool alive;
Display& disp;
byte color;
Player(int x, int y, int len, Dir facing, Display& disp, byte color):
facing(facing), last_facing(facing), alive(true), disp(disp), color(color) {
points_start = 0;
points_len = len;
Point point = Point(x, y);
for (int i = 0; i < len; i++) {
points[i] = point;
point = point.shifted_by(facing);
}
}
void handle_input(const Controller& con) {
if (con[Controller::Button::left] && last_facing != Dir::right) {
facing = Dir::left;
}
if (con[Controller::Button::right] && last_facing != Dir::left) {
facing = Dir::right;
}
if (con[Controller::Button::up] && last_facing != Dir::down) {
facing = Dir::up;
}
if (con[Controller::Button::down] && last_facing != Dir::up) {
facing = Dir::down;
}
}
Point head() {
int total_points = sizeof(points) / sizeof(*points);
int head_index = (points_start + points_len - 1) % total_points;
return points[head_index];
}
// Return whether food was eaten
bool cycle(Point food, bool wrap = false) {
int total_points = sizeof(points) / sizeof(*points);
int head_index = (points_start + points_len - 1) % total_points;
int next_head = (head_index + 1) % total_points;
Point new_point = points[head_index].shifted_by(facing);
bool ate_food = false;
if (new_point.x >= disp.width || new_point.y >= disp.height) {
if (!wrap) {
alive = false;
}
new_point.x = new_point.x == 255 ? disp.width - 1 : new_point.x % disp.width;
new_point.y = new_point.y == 255 ? disp.height - 1 : new_point.y % disp.height;
}
if (new_point == food) {
points_len++;
ate_food = true;
} else {
points_start = (points_start + 1) % total_points;
}
points[next_head] = new_point;
last_facing = facing;
return ate_food;
}
void draw(Player &other_player, bool can_overlap) {
Point other_head = other_player.head();
int total_points = sizeof(points) / sizeof(*points);
for (int i = 0; i < points_len; i++) {
Point point = points[(points_start + i) % total_points];
if (point == other_head && other_player.alive) {
other_player.alive = false;
if (point == head()) {
alive = false;
}
}
if (!can_overlap && disp.get_pixel(point.x, point.y) == color) {
alive = false;
}
disp.set_pixel(point.x, point.y, color);
}
}
};
Player *players[2];
bool SnakeGame::play() {
randomSeed(analogRead(A0));
byte palette[][3] = {
{0, 0, 0},
{4, 16, 4}, // Player 1
{4, 4, 16}, // Player 2
{16, 4, 4}, // Food
{4, 12, 12}, // Tie winner
{0, 0, 0}, // Swap color for Graphics::end_game
};
disp.palette = palette;
byte start_len = 3;
Player player1(disp.width / 2 - 1, 0, start_len, Dir::down, disp, 1);
Player player2(disp.width / 2, disp.height - 1, start_len, Dir::up, disp, 2);
players[0] = &player1;
players[1] = &player2;
player1.alive = controllers[0].is_connected();
player2.alive = controller_count > 1 && controllers[1].is_connected();
Point food = Point::rand_in_disp(disp);
unsigned long last_cycle = millis();
unsigned long last_input = millis();
int game_speed = 150;
int input_speed = 5;
int winner = -1;
while (player1.alive || player2.alive) {
unsigned long now = millis();
bool single_player = player1.alive ^ player2.alive;
if (single_player) {
winner = player1.alive ? 0 : 1;
}
disp.clear_all();
if (now > last_input + input_speed) {
bool exit_game = handle_input();
if (exit_game) {
disp.palette = NULL;
return true;
}
last_input = now;
}
bool food_was_eaten = false;
if (now > last_cycle + game_speed) {
last_cycle = now;
for (int i = 0; i < 2; i++) {
if (players[i]->alive) {
food_was_eaten = players[i]->cycle(food, !single_player) || food_was_eaten;
if (food_was_eaten && AUDIO_ENABLED) {
tone(AUDIO_PIN, i ? NOTE_F4 : NOTE_D5, 10);
}
}
}
}
if (food_was_eaten) {
game_speed *= 0.98;
}
for (int i = 0; i < 2; i++) {
if (players[i]->alive || i == winner) {
players[i]->draw(*players[1 - i], !single_player);
}
}
while (disp.get_pixel(food.x, food.y) != 0) {
food = Point::rand_in_disp(disp);
}
disp.set_pixel(food.x, food.y, 3);
disp.refresh();
delay(1);
}
// If the winner is -1, that means it was a tie.
uint8_t winner_color = winner == -1 ? 4 : players[winner]->color;
return Graphics::end_game(disp, controllers, controller_count, winner_color, palette, 5);
}
bool SnakeGame::handle_input() {
Controller::update_state(controllers, controller_count);
for (int i = 0; i < controller_count; i++) {
if (controllers[i].is_connected()) {
if (controllers[i][Controller::Button::start]) {
return true;
}
players[i]->handle_input(controllers[i]);
}
}
return false;
}