From 1f4abeaa00b931b827148e65c3381a8270e75940 Mon Sep 17 00:00:00 2001 From: Hanse-14 Date: Wed, 20 Dec 2023 16:57:29 +0100 Subject: [PATCH] fix change direction into itself #7 --- src/snake.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/snake.c b/src/snake.c index 6a9ffc0..2a22497 100644 --- a/src/snake.c +++ b/src/snake.c @@ -1,6 +1,7 @@ #include "snake.h" #include "game.h" //for access to global 'game' struct +bool snakeSetDirectionIsAllowed = true; void snakeInit() { @@ -44,6 +45,8 @@ void snakeGrow() void snakeMove() { int i = game.snake.length - 1; // counter for snake moving + + snakeSetDirectionIsAllowed = true; // direction can be changed now // update head position automatically snakeUpdateHeadPos(); @@ -63,6 +66,12 @@ void snakeMove() void snakeSetDir(snakeDirection_t dir) { + // if direction mustn changed + if(!snakeSetDirectionIsAllowed) + { + return; + } + // check, if snake should be move in opposite direction -> new direction stays old direction switch(dir) { @@ -92,6 +101,7 @@ void snakeSetDir(snakeDirection_t dir) } game.snake.direction = dir; + snakeSetDirectionIsAllowed = false; // direction cannot updated until next snakeMove return; }