From 0371e7f7e86932b46a39c1139ebacfb4a2db8c0a Mon Sep 17 00:00:00 2001 From: Hanse-14 Date: Thu, 14 Dec 2023 17:16:29 +0100 Subject: [PATCH] Add: snake cannot move into opposite direction --- src/snake.c | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/snake.c b/src/snake.c index cf4d1e8..9520aa7 100644 --- a/src/snake.c +++ b/src/snake.c @@ -62,7 +62,35 @@ void snakeMove() } void snakeSetDir(snakeDirection_t dir) -{ +{ + // check, if snake should be move in opposite direction -> new direction stays old direction + switch(dir) + { + case DOWN: + if(game.snake.direction == UP) + return; + else + break; + + case UP: + if(game.snake.direction == DOWN) + return; + else + break; + + case LEFT: + if(game.snake.direction == RIGHT) + return; + else + break; + + case RIGHT: + if(game.snake.direction == LEFT) + return; + else + break; + + } game.snake.direction = dir; return; }