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 "game.h" //for access to global 'game' struct
bool snakeSetDirectionIsAllowed = true;
void snakeInit()
{
@ -45,6 +46,8 @@ 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;
}