summary refs log tree commit diff homepage
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2021-01-29 02:17:14 -0500
committerJune McEnroe <june@causal.agency>2021-01-29 02:17:14 -0500
commit97389401cb0a346be6fc08170138b6e19d26940e (patch)
treeb75dad74a29f6fc87a0befde3dec6f6ad7803069
parentAdd snake game (diff)
downloadplay-97389401cb0a346be6fc08170138b6e19d26940e.tar.gz
play-97389401cb0a346be6fc08170138b6e19d26940e.zip
Don't let the snake snap its own neck
Diffstat (limited to '')
-rw-r--r--snake.c13
1 files changed, 9 insertions, 4 deletions
diff --git a/snake.c b/snake.c
index 3eb28f5..4b36ff9 100644
--- a/snake.c
+++ b/snake.c
@@ -175,14 +175,19 @@ static void input(void) {
 		ch = getch();
 		nodelay(stdscr, true);
 	}
+	int dy = head.dy;
+	int dx = head.dx;
 	switch (ch) {
-		break; case 'h': case KEY_LEFT:  head.dy =  0; head.dx = -1;
-		break; case 'j': case KEY_DOWN:  head.dy = +1; head.dx =  0;
-		break; case 'k': case KEY_UP:    head.dy = -1; head.dx =  0;
-		break; case 'l': case KEY_RIGHT: head.dy =  0; head.dx = +1;
+		break; case 'h': case KEY_LEFT:  dy =  0; dx = -1;
+		break; case 'j': case KEY_DOWN:  dy = +1; dx =  0;
+		break; case 'k': case KEY_UP:    dy = -1; dx =  0;
+		break; case 'l': case KEY_RIGHT: dy =  0; dx = +1;
 		break; case 'q': over = "You are satisfied.";
 		break; case ERR: exit(EXIT_FAILURE);
 	}
+	if (dy == -head.dy && dx == -head.dx) return;
+	head.dy = dy;
+	head.dx = dx;
 }
 
 uint playSnake(void) {