I am trying to do a snake game in XNA but I have some little problems...
I explain :
The Snake has a direction (Enum Type : Haut(=up), Bas(=down), Droite(=right), Gauche(=left)) When the player press a direction key this funcion is used :
public void SetDirection(Direction newDirection) // Teste les directions impossibles => impossible de faire un demi tour
{
if (_direction == Direction.Haut && newDirection == Direction.Bas)
{
return;
}
else if (_direction == Direction.Bas && newDirection == Direction.Haut)
{
return;
}
else if (_direction == Direction.Gauche && newDirection == Direction.Droite)
{
return;
}
else if (_direction == Direction.Droite && newDirection == Direction.Gauche)
{
return;
}
_direction = newDirection;
}
In theory, the snake can't make a U-turn. But it does not work properly (I will try to explain why) :
The snake moves each 100ms. while these 100ms, the snake is waiting for a direction. BUT if the snake goes up for example, and if the user presses {Droite(=right), Bas(=down)} or {Gauche(=left), Bas(=down)} while these 100ms, the snake makes a U-turn, and die because he cuts himself.
As a consequence, the user loose the game...
I can't see how to solve this problem.
If I am not clear or if you need any other information, please let me know.
Thanks in advance for your help
PS : I am sorry for the grammar faults but I don't speak perfectly english... :)
PPS : Here is the call to setDirection :
case GameState.Infini:
if (newKeyboardState.IsKeyDown(Keys.Escape))
{
CurrentGameState = GameState.Pause;
}
else if (newKeyboardState.IsKeyDown(Keys.Up))
{
_snake.SetDirection(Direction.Haut);
}
else if (newKeyboardState.IsKeyDown(Keys.Down))
{
_snake.SetDirection(Direction.Bas);
}
else if (newKeyboardState.IsKeyDown(Keys.Left))
{
_snake.SetDirection(Direction.Gauche);
}
else if (newKeyboardState.IsKeyDown(Keys.Right))
{
_snake.SetDirection(Direction.Droite);
}
SetDirection
? The issue could be coming from there. \$\endgroup\$