summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--snake.c35
1 files changed, 21 insertions, 14 deletions
diff --git a/snake.c b/snake.c
index 2b87327..22011f8 100644
--- a/snake.c
+++ b/snake.c
@@ -20,10 +20,11 @@ typedef enum _dir_t {
} dir_t;
-static volatile uint8_t dir;
-static volatile bool start = false;
+static volatile uint8_t kbdir;
+static volatile bool start;
static volatile uint16_t lfsr = 0xace1;
+static uint8_t dir;
static point_t tail = {7, 4};
#define HISTORY_MAX 128
@@ -88,7 +89,7 @@ static void reset(void) {
rand_q();
- dir = EAST;
+ kbdir = dir = EAST;
tail = (point_t){7, 4};
history_len = 0;
history_pos = 0;
@@ -98,6 +99,10 @@ static void reset(void) {
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) {
if (!make)
return;
@@ -108,19 +113,19 @@ void kbd_handle(uint16_t code, bool make) {
break;
case KBD_CODE_UP:
- dir = NORTH;
+ kbdir = NORTH;
break;
case KBD_CODE_LEFT:
- dir = WEST;
+ kbdir = WEST;
break;
case KBD_CODE_DOWN:
- dir = SOUTH;
+ kbdir = SOUTH;
break;
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) {
+ if (kbdir != rev(dir))
+ dir = kbdir;
+
uint8_t d = dir;
rand_mix(d);
- LedSignSet(tail.x, tail.y, 0);
+ set_led(tail.x, tail.y, 0);
int i;
point_t pt = tail;
- LedSignSet(pt.x, pt.y, 0);
+ set_led(pt.x, pt.y, 0);
for (i = 0; i < history_len; i++) {
go(&pt, get_dir(i));
- LedSignSet(pt.x, pt.y, 2);
+ set_led(pt.x, pt.y, 2);
}
go(&pt, d);
- LedSignSet(pt.x, pt.y, 3);
+ set_led(pt.x, pt.y, 3);
if (point_used(&pt, true))
return false;
@@ -189,7 +197,7 @@ static bool step(void) {
if (point_used(&pt, false))
return false;
- LedSignSet(q.x, q.y, 7);
+ set_led(q.x, q.y, 7);
return true;
}
@@ -203,9 +211,8 @@ int main() {
kbd_send(KBD_CMD_RESET);
while(true) {
- while (!start) {}
start = false;
-
+ while (!start) {}
reset();
while (step())