-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake.c
475 lines (402 loc) · 11.1 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include "matelight.h"
#define MODE_GAME 0
#define MODE_DEAD 1
#define MOVE_NONE 0
#define MOVE_UP 1
#define MOVE_LEFT 2
#define MOVE_RIGHT 3
#define MOVE_DOWN 4
#define SNAKE_START 3
#define SNAKE_FOOD 2
#define SNAKE_SUPERFOOD 6
#define OBJ_EMPTY 0
#define OBJ_WALL 1
#define OBJ_SNAKE 2
#define OBJ_SNAKEHEAD 3
#define OBJ_FOOD 4
#define OBJ_SUPERFOOD 5
#define OBJ_POISON 6
#define WANTED_FOOD 3
#define WANTED_POISON 2
#define SUPERFOOD_FREQ 5
#define ROTATE_TICKS 25
#define COLOR_EMPTY COLOR_BLACK
#define COLOR_WALL COLOR_WHITE
#define COLOR_SNAKE COLOR_LIGHT_GRAY
#define COLOR_SNAKEHEAD COLOR_ORANGE
#define COLOR_FOOD COLOR_GREEN
#define COLOR_SUPERFOOD COLOR_MAGENTA
#define COLOR_POISON COLOR_RED
static int game_mode = MODE_DEAD;
static bool game_pause = false;
static int game_speed = 0;
static int game_level = 0;
static int tick_count = 0;
static int move = MOVE_NONE;
static int diry;
static int dirx;
static int numfood;
static int numpoison;
static int rotatecnt;
static int snakelen;
static int wantedlen;
static unsigned char grid[MAX_GRID_SIZE];
static unsigned char snake[MAX_GRID_SIZE * 2];
static unsigned char objs[MAX_GRID_SIZE * 2];
static void grid_set(int y, int x, unsigned char type)
{
grid[(y * grid_width) + x] = type;
}
static void add_object(unsigned char type)
{
int ry, rx;
int yloop, xloop;
int objy, objx;
if (type == OBJ_SNAKEHEAD) {
ry = grid_height / 2;
rx = grid_width / 2;
} else {
ry = rand() % grid_height;
rx = rand() % grid_width;
}
for (yloop = 0; yloop < grid_height; yloop++) {
for (xloop = 0; xloop < grid_width; xloop++) {
objy = (ry + yloop) % grid_height;
objx = (rx + xloop) % grid_width;
if (grid[(objy * grid_width) + objx] == OBJ_EMPTY) {
grid_set(objy, objx, type);
if (type == OBJ_FOOD || type == OBJ_SUPERFOOD) {
objs[((numfood + numpoison) * 2) + 0] = objy;
objs[((numfood + numpoison) * 2) + 1] = objx;
numfood++;
} else if (type == OBJ_POISON) {
objs[((numfood + numpoison) * 2) + 0] = objy;
objs[((numfood + numpoison) * 2) + 1] = objx;
numpoison++;
} else if (type == OBJ_SNAKEHEAD) {
snake[0] = objy;
snake[1] = objx;
snakelen = 1;
}
return;
}
}
}
}
static void remove_obj(int objy, int objx)
{
int i, j;
unsigned char type = grid[(objy * grid_width) + objx];
for (i = 0; i < (numfood + numpoison); i++) {
if (objs[(i * 2) + 0] == objy && objs[(i * 2) + 1] == objx) {
for (j = i; j < ((numfood + numpoison) - 1); j++) {
objs[(j * 2) + 0] = objs[(j * 2) + 2];
objs[(j * 2) + 1] = objs[(j * 2) + 3];
}
break;
}
}
if (type == OBJ_FOOD || type == OBJ_SUPERFOOD) {
numfood--;
} else if (type == OBJ_POISON) {
numpoison--;
}
grid_set(objy, objx, OBJ_EMPTY);
}
static void rotate_objects(void)
{
int objy, objx;
if ((numfood + numpoison) <= 0) return;
objy = objs[0];
objx = objs[1];
remove_obj(objy, objx);
}
static void setup_game(bool start)
{
int y, x;
game_mode = start ? MODE_GAME : MODE_DEAD;
game_pause = false;
game_speed = 0;
tick_count = 0;
move = MOVE_NONE;
diry = 0;
dirx = 1;
numfood = 0;
numpoison = 0;
rotatecnt = 0;
snakelen = 0;
wantedlen = SNAKE_START;
memset(grid, '\0', sizeof(grid));
memset(snake, '\0', sizeof(snake));
memset(objs, '\0', sizeof(objs));
if (game_level) {
for (x = 0; x < grid_width; x++) {
grid[(0 * grid_width) + x] = OBJ_WALL;
grid[((grid_height - 1) * grid_width) + x] = OBJ_WALL;
}
for (y = 1; y < (grid_height - 1); y++) {
grid[(y * grid_width) + 0] = OBJ_WALL;
grid[(y * grid_width) + (grid_width - 1)] = OBJ_WALL;
}
}
add_object(OBJ_SNAKEHEAD);
}
static void activate(bool start)
{
setup_game(start);
}
static void deactivate(void)
{
setup_game(false);
}
static void update_dir(void)
{
switch (move) {
case MOVE_UP:
if (diry == 0) {
diry = -1;
dirx = 0;
}
break;
case MOVE_LEFT:
if (dirx == 0) {
diry = 0;
dirx = -1;
}
break;
case MOVE_RIGHT:
if (dirx == 0) {
diry = 0;
dirx = 1;
}
break;
case MOVE_DOWN:
if (diry == 0) {
diry = 1;
dirx = 0;
}
break;
}
move = MOVE_NONE;
}
static void move_snake(void)
{
int heady = snake[((snakelen - 1) * 2) + 0];
int headx = snake[((snakelen - 1) * 2) + 1];
unsigned char type;
heady = heady + diry;
if (heady < 0) heady = grid_height - 1;
else if (heady >= grid_height) heady = 0;
headx = headx + dirx;
if (headx < 0) headx = grid_width - 1;
else if (headx >= grid_width) headx = 0;
type = grid[(heady * grid_width) + headx];
switch (type) {
case OBJ_WALL:
case OBJ_SNAKE:
case OBJ_SNAKEHEAD:
case OBJ_POISON:
game_mode = MODE_DEAD;
break;
case OBJ_FOOD:
case OBJ_SUPERFOOD:
/* eat food */
remove_obj(heady, headx);
wantedlen += (type == OBJ_SUPERFOOD ? SNAKE_SUPERFOOD : SNAKE_FOOD);
/* fall through */
case OBJ_EMPTY:
default:
/* move snake */
if (snakelen < wantedlen) {
snakelen++;
} else {
int taily = snake[0];
int tailx = snake[1];
int i;
grid_set(taily, tailx, OBJ_EMPTY);
if (snakelen >= 2) {
//__builtin_memmove(snake, snake + 2, snakelen - 1);
for (i = 0; i < (snakelen - 1); i++) {
snake[(i * 2) + 0] = snake[(i * 2) + 2];
snake[(i * 2) + 1] = snake[(i * 2) + 3];
}
}
}
snake[((snakelen - 1) * 2) + 0] = heady;
snake[((snakelen - 1) * 2) + 1] = headx;
grid_set(heady, headx, OBJ_SNAKEHEAD);
if (snakelen >= 2) {
int bodyy = snake[((snakelen - 2) * 2) + 0];
int bodyx = snake[((snakelen - 2) * 2) + 1];
grid_set(bodyy, bodyx, OBJ_SNAKE);
}
break;
}
}
static void tick(void)
{
if (game_mode != MODE_GAME) return;
if (game_pause) return;
tick_count++;
if (game_speed && (tick_count % 4) != 0) return;
update_dir();
/* move_snake */
if (diry != 0 || dirx != 0) {
move_snake();
}
if (game_mode != MODE_GAME) return;
rotatecnt++;
if (rotatecnt >= ROTATE_TICKS) {
rotate_objects();
rotatecnt = 0;
}
/* add food */
while (numfood < WANTED_FOOD) {
add_object((rand() % SUPERFOOD_FREQ) == 0 ? OBJ_SUPERFOOD : OBJ_FOOD);
}
/* add poison */
while (numpoison < WANTED_POISON) {
add_object(OBJ_POISON);
}
}
static void draw_block(char *screen, int y, int x, unsigned int color, int fade)
{
unsigned char r = (color >> 16) & 0xff;
unsigned char g = (color >> 8) & 0xff;
unsigned char b = color & 0xff;
if (fade) {
int pos = (int)(time_val * 1000.0) % 1000;
if (pos > 500) pos = 500 - (pos - 500);
pos *= 2;
r = ((int)r*pos) / 1000;
g = ((int)g*pos) / 1000;
b = ((int)b*pos) / 1000;
}
set_pixel(screen, y, x, COLOR_RGB(r, g, b));
}
static void draw_obj(char *screen, int y, int x)
{
unsigned char type;
int color;
type = grid[(y * grid_width) + x];
color = COLOR_EMPTY;
switch (type) {
case OBJ_WALL:
color = COLOR_WALL;
break;
case OBJ_SNAKE:
color = COLOR_SNAKE;
break;
case OBJ_SNAKEHEAD:
color = COLOR_SNAKEHEAD;
break;
case OBJ_FOOD:
color = COLOR_FOOD;
break;
case OBJ_SUPERFOOD:
color = COLOR_SUPERFOOD;
break;
case OBJ_POISON:
color = COLOR_POISON;
break;
}
switch (type) {
/* clear all */
default:
draw_block(screen, y, x, color, 0);
break;
/* box with 1px border */
case OBJ_WALL:
case OBJ_SNAKE:
draw_block(screen, y, x, color, 0);
break;
case OBJ_SNAKEHEAD:
draw_block(screen, y, x, color, 0);
break;
/* circle */
case OBJ_FOOD:
case OBJ_SUPERFOOD:
case OBJ_POISON:
draw_block(screen, y, x, color, 1);
break;
}
}
static void input(int player, int key_idx, bool key_val, int key_state)
{
(void)player;
(void)key_state;
switch (game_mode) {
case MODE_GAME:
if (key_idx == KEYPAD_LEFT && key_val) {
move = MOVE_LEFT;
} else if (key_idx == KEYPAD_RIGHT && key_val) {
move = MOVE_RIGHT;
} else if (key_idx == KEYPAD_UP && key_val) {
move = MOVE_UP;
} else if (key_idx == KEYPAD_DOWN && key_val) {
move = MOVE_DOWN;
}
if (key_idx == KEYPAD_START && key_val) {
if (! game_pause) {
game_pause = true;
} else {
game_pause = false;
}
}
if (key_idx == KEYPAD_B && key_val) {
game_speed = ! game_speed;
}
if (key_idx == KEYPAD_A && key_val) {
game_level = ! game_level;
setup_game(true);
}
break;
case MODE_DEAD:
switch (key_idx) {
case KEYPAD_SELECT:
case KEYPAD_START:
case KEYPAD_B:
case KEYPAD_A:
if (key_val) {
setup_game(true);
}
break;
default:
break;
}
}
}
static void render(bool *display, char *screen)
{
int y, x;
if (game_mode == MODE_GAME) {
*display = true;
for (y = 0; y < grid_height; y++) {
for (x = 0; x < grid_width; x++) {
draw_obj(screen, y, x);
}
}
} else {
*display = false;
}
}
static bool idle(void)
{
return game_mode != MODE_GAME;
}
const struct game snake_game = {
"snake",
true,
false,
0.1,
NULL,
activate,
deactivate,
input,
tick,
render,
idle,
};