Reverse play field, prevent death by going backwards

This commit is contained in:
Matthias Schiffer 2013-03-15 14:59:12 +01:00
parent b0dda6fda2
commit 529c34efef

35
snake.c
View file

@ -20,10 +20,11 @@ typedef enum _dir_t {
} dir_t; } dir_t;
static volatile uint8_t dir; static volatile uint8_t kbdir;
static volatile bool start = false; static volatile bool start;
static volatile uint16_t lfsr = 0xace1; static volatile uint16_t lfsr = 0xace1;
static uint8_t dir;
static point_t tail = {7, 4}; static point_t tail = {7, 4};
#define HISTORY_MAX 128 #define HISTORY_MAX 128
@ -88,7 +89,7 @@ static void reset(void) {
rand_q(); rand_q();
dir = EAST; kbdir = dir = EAST;
tail = (point_t){7, 4}; tail = (point_t){7, 4};
history_len = 0; history_len = 0;
history_pos = 0; history_pos = 0;
@ -98,6 +99,10 @@ static void reset(void) {
move(EAST, true); move(EAST, true);
} }
static void set_led(uint8_t x, uint8_t y, uint8_t val) {
LedSignSet(13-x, 8-y, val);
}
void kbd_handle(uint16_t code, bool make) { void kbd_handle(uint16_t code, bool make) {
if (!make) if (!make)
return; return;
@ -108,19 +113,19 @@ void kbd_handle(uint16_t code, bool make) {
break; break;
case KBD_CODE_UP: case KBD_CODE_UP:
dir = NORTH; kbdir = NORTH;
break; break;
case KBD_CODE_LEFT: case KBD_CODE_LEFT:
dir = WEST; kbdir = WEST;
break; break;
case KBD_CODE_DOWN: case KBD_CODE_DOWN:
dir = SOUTH; kbdir = SOUTH;
break; break;
case KBD_CODE_RIGHT: case KBD_CODE_RIGHT:
dir = EAST; kbdir = EAST;
} }
} }
@ -155,24 +160,27 @@ static bool point_used(const point_t *p, bool head) {
} }
static bool step(void) { static bool step(void) {
if (kbdir != rev(dir))
dir = kbdir;
uint8_t d = dir; uint8_t d = dir;
rand_mix(d); rand_mix(d);
LedSignSet(tail.x, tail.y, 0); set_led(tail.x, tail.y, 0);
int i; int i;
point_t pt = tail; point_t pt = tail;
LedSignSet(pt.x, pt.y, 0); set_led(pt.x, pt.y, 0);
for (i = 0; i < history_len; i++) { for (i = 0; i < history_len; i++) {
go(&pt, get_dir(i)); go(&pt, get_dir(i));
LedSignSet(pt.x, pt.y, 2); set_led(pt.x, pt.y, 2);
} }
go(&pt, d); go(&pt, d);
LedSignSet(pt.x, pt.y, 3); set_led(pt.x, pt.y, 3);
if (point_used(&pt, true)) if (point_used(&pt, true))
return false; return false;
@ -189,7 +197,7 @@ static bool step(void) {
if (point_used(&pt, false)) if (point_used(&pt, false))
return false; return false;
LedSignSet(q.x, q.y, 7); set_led(q.x, q.y, 7);
return true; return true;
} }
@ -203,9 +211,8 @@ int main() {
kbd_send(KBD_CMD_RESET); kbd_send(KBD_CMD_RESET);
while(true) { while(true) {
while (!start) {}
start = false; start = false;
while (!start) {}
reset(); reset();
while (step()) while (step())