-
Notifications
You must be signed in to change notification settings - Fork 0
/
nielk.ino
326 lines (273 loc) · 6.82 KB
/
nielk.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
#include <Arduboy2.h>
#include "bitmaps.h"
Arduboy2 arduboy;
int const grid_size = 4;
int grid_width = 0;
int grid_height = 0;
int const max_length = 64;
int snake_x [max_length] = {};
int snake_y [max_length] = {};
int snake_length = 3;
int head = snake_length-1;
int snake_dir_x = 1;
int snake_dir_y = 0;
int prev_dir_x = 1;
int prev_dir_y = 0;
int const r = grid_size/2;
int apple_x = 0;
int apple_y = 0;
bool paused = false;
bool flipped = false;
bool apple_flipped = false;
bool snake_flipped[max_length] = {};
int score = 0;
int room = 0;
int game_timer = 30;
int menu_item = 0;
String menu_items[4] = {"PLAY","CONTROLS","ABOUT"};
void setup() {
arduboy.boot();
arduboy.blank();
arduboy.setFrameRate(30);
arduboy.initRandomSeed();
arduboy.display();
Serial.begin(9600);
grid_width = WIDTH/grid_size-1;
grid_height = HEIGHT/grid_size-3;
reset_snake();
randomize_apple();
}
// Also checks for self-collision
void render_snake() {
int head_x = snake_x[head];
int head_y = snake_y[head];
int current_x = head_x;
int current_y = head_y;
for (int i = head; i > head-snake_length; i--) {
int index = i;
if (index < 0)
index += max_length;
current_x = snake_x[index];
current_y = snake_y[index];
if (current_x == head_x && current_y == head_y && index!= head) room = 2;
if (snake_flipped[index]){
arduboy.drawRect(current_x*grid_size, current_y*grid_size,
grid_size, grid_size);
} else {
arduboy.fillRect(current_x*grid_size, current_y*grid_size,
grid_size, grid_size);
}
}
}
void render_apple() {
if (apple_flipped){
arduboy.drawRect(apple_x*grid_size, apple_y*grid_size,
grid_size, grid_size);
} else {
arduboy.fillRect(apple_x*grid_size, apple_y*grid_size,
grid_size, grid_size);
}
}
void render_pause() {
arduboy.fillRect(44,24,41,16,BLACK);
arduboy.drawBitmap(44,24,paused_icon,41,16,1);
}
void render_2_digit_number(int x, int y, int n) {
arduboy.drawBitmap(x+5,y,numbers[n%10],8,4,1);
if (n > 9) {
arduboy.drawBitmap(x,y,numbers[(n-n%10)/10],8,4,1);
}
}
void render_bottom_bar() {
arduboy.drawBitmap(0,56,bottom_bar,128,9,1);
render_2_digit_number(29, 57, score);
render_2_digit_number(119,57, game_timer);
}
void render_menu() {
arduboy.drawBitmap(0,0,menu_background,128,64,1);
int pos = 17;
for (int i = 0; i < 3;i++) {
int w = 5*menu_items[i].length();
if(i==menu_item) {
arduboy.drawRect(pos-2,43,w+3,10);
}
pos += w + 5;
}
}
void render_game_over() {
arduboy.fillRect(32,16,64,32,0);
arduboy.drawBitmap(32,16,game_over,64,32,1);
}
void render_controls() {
arduboy.drawBitmap(0,0,controls_background,128,64,1);
}
void render_about() {
arduboy.drawBitmap(0,0,about_background,128,64,1);
}
void handle_game_input() {
arduboy.pollButtons();
switch (room) {
case 0:
if (arduboy.justPressed(UP_BUTTON))
menu_item = 2-menu_item;
if (arduboy.justPressed(DOWN_BUTTON))
menu_item = 2-menu_item;
if (arduboy.justPressed(LEFT_BUTTON))
menu_item -= 1;
if (arduboy.justPressed(RIGHT_BUTTON))
menu_item += 1;
menu_item = (menu_item+3) %3;
if (arduboy.justPressed(A_BUTTON)) {
switch(menu_item) {
case 0:
room = 1;
break;
case 1:
room = 3;
break;
case 2:
room = 4;
break;
}
}
break;
case 1:
if (!paused) {
if (arduboy.justPressed(UP_BUTTON) && prev_dir_y == 0) {
snake_dir_x = 0;
snake_dir_y = -1+2*flipped;
}
if (arduboy.justPressed(DOWN_BUTTON) && prev_dir_y == 0) {
snake_dir_x = 0;
snake_dir_y = 1-2*flipped;
}
if (arduboy.justPressed(LEFT_BUTTON) && prev_dir_x == 0) {
snake_dir_x = -1+2*flipped;
snake_dir_y = 0;
}
if (arduboy.justPressed(RIGHT_BUTTON) && prev_dir_x == 0) {
snake_dir_x = 1-2*flipped;
snake_dir_y = 0;
}
}
if (arduboy.justPressed(A_BUTTON)) {
paused = !paused;
}
break;
case 2:
if (arduboy.justPressed(A_BUTTON)) {
room = 0;
score = 0;
flipped = false;
reset_snake();
randomize_apple();
}
if (arduboy.justPressed(B_BUTTON)) {
arduboy.clear();
render_snake();
render_apple();
render_bottom_bar();
Serial.write(arduboy.getBuffer(), 128 * 64 / 8);
}
break;
default:
if (arduboy.justPressed(B_BUTTON)) {
room = 0;
}
break;
}
}
void update_snake() {
head += 1;
int prev = head - 1;
if (head > max_length - 1)
head = 0;
int next_x = snake_x[prev]+snake_dir_x;
int next_y = snake_y[prev]+snake_dir_y;
prev_dir_x = snake_dir_x;
prev_dir_y = snake_dir_y;
if (next_y<0){
next_y=grid_height;
next_x=grid_width-next_x;
flipped = !flipped;
}
else if (next_y>grid_height){
next_y=0;
next_x=grid_width-next_x;
flipped = !flipped;
}
if (next_x<0)
next_x=grid_width;
else if (next_x>grid_width)
next_x=0;
snake_x[head] = next_x;
snake_y[head] = next_y;
snake_flipped[head] = flipped;
if (apple_flipped == flipped && next_x == apple_x && next_y == apple_y){
snake_length+=1;
randomize_apple();
score+=1;
}
}
void reset_snake() {
snake_length = 3;
head = snake_length-1;
snake_dir_x = 1;
snake_dir_y = 0;
prev_dir_x = 1;
prev_dir_y = 0;
snake_x[0] = grid_width/2;
snake_x[1] = grid_width/2+1;
snake_x[2] = grid_width/2+2;
snake_y[0] = grid_height/2;
snake_y[1] = grid_height/2;
snake_y[2] = grid_height/2;
snake_flipped[0] = false;
snake_flipped[1] = false;
snake_flipped[2] = false;
}
void randomize_apple() {
apple_x = random(1,grid_width);
apple_y = random(1,grid_height);
apple_flipped = random(0,2);
game_timer = max(30-score,10);
}
void loop() {
if (!arduboy.nextFrame()) return;
handle_game_input();
switch (room) {
case 0:
arduboy.clear();
render_menu();
break;
case 1:
if (paused) {
render_pause();
} else {
if (arduboy.everyXFrames(30)){ game_timer--;}
if (game_timer == 0){
render_bottom_bar();
room = 2;}
if (arduboy.everyXFrames(4)) {
update_snake();
arduboy.clear();
render_snake();
render_apple();
render_bottom_bar();
}
}
break;
case 2:
render_game_over();
break;
case 3:
arduboy.clear();
render_controls();
break;
case 4:
arduboy.clear();
render_about();
break;
}
arduboy.display();
}