From b4c02d94a0cfae773f0955e33978aaaba419f0ea Mon Sep 17 00:00:00 2001 From: Joseph-Eugene Winzer Date: Mon, 13 Aug 2018 23:52:18 +0200 Subject: [PATCH 1/2] 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. --- games/snake.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/games/snake.c b/games/snake.c index 40d66da86..a4b2d69f4 100644 --- a/games/snake.c +++ b/games/snake.c @@ -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; } - // 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; + } } } From ab7acd6e3435b3443c8346ab495f05699a0e9750 Mon Sep 17 00:00:00 2001 From: Joseph-Eugene Winzer Date: Mon, 13 Aug 2018 23:58:42 +0200 Subject: [PATCH 2/2] Games: Snake: Fixes snake head collision with fruit CheckCollisionRecs() returns true on edge-collision what means that the snake eats the fruit when colliding with it but also when sliding by one unit above or below the fruit. --- games/snake.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/games/snake.c b/games/snake.c index a4b2d69f4..23d1bd1d0 100644 --- a/games/snake.c +++ b/games/snake.c @@ -220,8 +220,8 @@ void UpdateGame(void) } // collision - if (CheckCollisionRecs((Rectangle){(int)snake[0].position.x, (int)snake[0].position.y, (int)snake[0].size.x, (int)snake[0].size.y}, - (Rectangle){(int)fruit.position.x, (int)fruit.position.y, (int)fruit.size.x, (int)fruit.size.y})) + if ((snake[0].position.x < (fruit.position.x + fruit.size.x) && (snake[0].position.x + snake[0].size.x) > fruit.position.x) && + (snake[0].position.y < (fruit.position.y + fruit.size.y) && (snake[0].position.y + snake[0].size.y) > fruit.position.y)) { snake[counterTail].position = snakePosition[counterTail - 1]; counterTail += 1;