From 9028e3082e0ed32b1fb6ac86e967e12c1f7a6f95 Mon Sep 17 00:00:00 2001 From: daniel-Jones Date: Mon, 2 Apr 2018 10:18:36 +0930 Subject: Added checking to x directions as well to prevent colliding with self --- snake.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'snake.c') diff --git a/snake.c b/snake.c index 6b94287..ca7ed6b 100644 --- a/snake.c +++ b/snake.c @@ -110,12 +110,16 @@ int main(void) snake->direction = UP; break; case 'a': - case 'h': - snake->direction = LEFT; + case 'h': + /* prevent changing direction into yourself */ + if (snake->direction != RIGHT) + snake->direction = LEFT; break; case 'd': case 'l': - snake->direction = RIGHT; + /* prevent changing direction into yourself */ + if (snake->direction != LEFT) + snake->direction = RIGHT; break; case 'q': /* q quits the game */ snake->state = 0; -- cgit v1.2.3