fix change direction into itself #7

This commit is contained in:
Hanse-14 2023-12-20 16:57:29 +01:00
parent 1a9a348793
commit 1f4abeaa00

View File

@ -1,6 +1,7 @@
#include "snake.h" #include "snake.h"
#include "game.h" //for access to global 'game' struct #include "game.h" //for access to global 'game' struct
bool snakeSetDirectionIsAllowed = true;
void snakeInit() void snakeInit()
{ {
@ -44,6 +45,8 @@ void snakeGrow()
void snakeMove() void snakeMove()
{ {
int i = game.snake.length - 1; // counter for snake moving int i = game.snake.length - 1; // counter for snake moving
snakeSetDirectionIsAllowed = true; // direction can be changed now
// update head position automatically // update head position automatically
snakeUpdateHeadPos(); snakeUpdateHeadPos();
@ -63,6 +66,12 @@ void snakeMove()
void snakeSetDir(snakeDirection_t dir) 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 // check, if snake should be move in opposite direction -> new direction stays old direction
switch(dir) switch(dir)
{ {
@ -92,6 +101,7 @@ void snakeSetDir(snakeDirection_t dir)
} }
game.snake.direction = dir; game.snake.direction = dir;
snakeSetDirectionIsAllowed = false; // direction cannot updated until next snakeMove
return; return;
} }