Browse Source

Games: Snake: Fixes fruit spawn position

If the initial fruit position collides with the snake's body a new
position for the fruit is generated but without adding the grid offset.
pull/623/head
Joseph-Eugene Winzer 6 years ago
parent
commit
b4c02d94a0
1 changed files with 6 additions and 6 deletions
  1. +6
    -6
      games/snake.c

+ 6
- 6
games/snake.c View File

@ -203,7 +203,7 @@ void UpdateGame(void)
if ((snake[0].position.x == snake[i].position.x) && (snake[0].position.y == snake[i].position.y)) gameOver = true;
}
// l">TODO: review logic: fruit.position calculation
// fruit.position calculation
if (!fruit.active)
{
fruit.active = true;
@ -211,11 +211,11 @@ void UpdateGame(void)
for (int i = 0; i < counterTail; i++)
{
while ((fruit.position.x == snake[i].position.x) && (fruit.position.y == snake[i].position.y))
{
fruit.position = (Vector2){ GetRandomValue(0, (screenWidth/SQUARE_SIZE) - 1)*SQUARE_SIZE, GetRandomValue(0, (screenHeight/SQUARE_SIZE) - 1)*SQUARE_SIZE };
i = 0;
}
while ((fruit.position.x == snake[i].position.x) && (fruit.position.y == snake[i].position.y))
{
fruit.position = (Vector2){ GetRandomValue(0, (screenWidth/SQUARE_SIZE) - 1)*SQUARE_SIZE + offset.x/2, GetRandomValue(0, (screenHeight/SQUARE_SIZE) - 1)*SQUARE_SIZE + offset.y/2 };
i = 0;
}
}
}

Loading…
Cancel
Save