summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Schiffer <mschiffer@universe-factory.net>2013-03-15 14:59:12 +0100
committerMatthias Schiffer <mschiffer@universe-factory.net>2013-03-15 14:59:12 +0100
commit529c34efef580066b41156556bf3fe7668ddf97a (patch)
tree9a8a55ae8e7166b859db6b8e83350681b4bc4b7e
parentb0dda6fda27cc894673b3d96f92660b17d822eea (diff)
downloadsnake-529c34efef580066b41156556bf3fe7668ddf97a.tar
snake-529c34efef580066b41156556bf3fe7668ddf97a.zip
Reverse play field, prevent death by going backwards
-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())