This repository has been archived by the owner on Nov 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snake_sp.c
76 lines (69 loc) · 1.72 KB
/
snake_sp.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
#include "snake_shared.c"
void ram (void) {
uint8_t key = BTN_RIGHT, button;
lcdClear();
while (1) {
switch (key) {
case BTN_ENTER:
// exit
return;
case BTN_RIGHT:
if (direction != DIRECTION_LEFT)
direction = DIRECTION_RIGHT;
break;
case BTN_UP:
if (direction != DIRECTION_DOWN)
direction = DIRECTION_UP;
break;
case BTN_LEFT:
if (direction != DIRECTION_RIGHT)
direction = DIRECTION_LEFT;
break;
case BTN_DOWN:
if (direction != DIRECTION_UP)
direction = DIRECTION_DOWN;
break;
//Default: No keystroke received. Assuming last keystroke.
}
point newendpoint = snake.endpoint;
shiftPoint (&newendpoint, direction);
bool resetBacon = false;
if (newendpoint.x == bacon.x && newendpoint.y == bacon.y) {
growBuf (&snake, direction);
resetBacon = true;
} else {
setGamePixel(snake.startpoint.x, snake.startpoint.y, 0);
shiftBuf (&snake, direction);
}
if (getGamePixel(snake.endpoint.x, snake.endpoint.y))
break;
setGamePixel(snake.endpoint.x, snake.endpoint.y, 1);
while (resetBacon) {
bacon.x = getRandom() % GAME_WIDTH;
bacon.y = getRandom() % GAME_HEIGHT;
if (!getGamePixel(bacon.x, bacon.y))
resetBacon = false;
}
drawFood (bacon.x, bacon.y);
lcdRefresh();
key = BTN_NONE;
for (i=0; i < ( TIME_PER_MOVE / 50 ); ++i) {
delayms(50);
if ((button = getInputRaw()) != BTN_NONE)
key = button;
}
}
// Game ended, show results
lcdClear();
lcdNl();
lcdPrintln("Game Over");
lcdPrintln("Your score:");
lcdPrintInt(getLength(&snake) - INITIAL_LENGTH + 1);
lcdNl();
lcdPrintln("Press any key");
lcdPrintln("to continue.");
lcdRefresh();
delayms(500);
while(getInputRaw() == BTN_NONE)
delayms(25);
}