diff --git a/src/game.c b/src/game.c index d8878d14342da06a703963368ce5a80b6ad6c08a..9212bdc495df3ea32a6e3c15df3747772e7f3f36 100644 --- a/src/game.c +++ b/src/game.c @@ -29,3 +29,17 @@ void ResetGame(Snake *snake, Food *food, int *score, bool *gameOver) { *gameOver = false; } +void TogglePause (GameState *state) { + if (*state == PLAYING) { + *state = PAUSED; + } else if (*state == PAUSED) { + *state = PLAYING; + } +} + + +void HandlePauseInput(GameState * state) { + if (IsKeyPressed(KEY_ESCAPE) || IsKeyPressed(KEY_P)) { + TogglePause(state); + } +} \ No newline at end of file diff --git a/src/game.h b/src/game.h index 239812c97eb112f5879edf47d4e99e8321a2280d..4b728380a84e6b54c839b7189874daaae8eba9a4 100644 --- a/src/game.h +++ b/src/game.h @@ -6,7 +6,7 @@ #include "obstacle.h" // Added for obstacle support // Game state enum (added but doesn't conflict with existing bool gameOver) -typedef enum { MENU, PLAYING, GAME_OVER } GameState; +typedef enum { MENU, PLAYING, PAUSED, GAME_OVER } GameState; // Your existing functions (unchanged) void HandleInput(Snake *snake); @@ -14,5 +14,7 @@ void ResetGame(Snake *snake, Food *food, int *score, bool *gameOver); // New version that supports obstacles (doesn't replace the old one) void ResetGameWithObstacles(Snake *snake, Food *food, Obstacle obstacles[], int *score, int *level, GameState *state); +void TogglePause(GameState *state); +void HandlePauseInput(GameState *state); #endif \ No newline at end of file diff --git a/src/main.c b/src/main.c index 41dd8a48fc1b2961ec70e9678c0b49c59af7506d..f97a4e0d6e44a9bb0e4c1ede8e89f13da354317d 100644 --- a/src/main.c +++ b/src/main.c @@ -76,6 +76,7 @@ int main(void) { case PLAYING: HandleInput(&snake); + HandlePauseInput(&gameState); UpdateSnake(&snake); // Food collision @@ -100,6 +101,9 @@ int main(void) { flashFrames = 2; } break; + case PAUSED: + HandlePauseInput(&gameState); + break; case GAME_OVER: if (IsKeyPressed(KEY_ENTER)) { @@ -142,6 +146,29 @@ int main(void) { DrawText(TextFormat("Level: %d", level), 10, 40, 20, DARKGRAY); break; + case PAUSED: + for (int i = 0; i < snake.length; i++){ + DrawRectangle(snake.segments[i].x * CELL_SIZE, + snake.segments[i].y * CELL_SIZE, + CELL_SIZE, CELL_SIZE, GREEN); + } + + DrawRectangle(food.position.x * CELL_SIZE, + food.position.y * CELL_SIZE, + CELL_SIZE, CELL_SIZE, BLUE); + DrawObstacles(obstacles, MAX_OBSTACLES) ; + + + //DRAW PAUSE OVERLAY + DrawRectangle(0,0, SCREEN_WIDTH, SCREEN_HEIGHT, (Color){0,0,0,128}); + DrawText("Paused", SCREEN_WIDTH/2 -80, SCREEN_HEIGHT/2 -40, 50, WHITE); + DrawText("Press ESC or P to resume", SCREEN_WIDTH/2 -150, SCREEN_HEIGHT/2 +20, 20, WHITE); + DrawText(TextFormat("Level: %d", level), 10, 10, 20, WHITE); + DrawText(TextFormat("Score: %d", score), 10, 40, 20, WHITE); + break; + + + case GAME_OVER: DrawText("GAME OVER!", SCREEN_WIDTH/2 - 100, SCREEN_HEIGHT/2 - 40, 40, RED); DrawText(TextFormat("Score: %d", score), SCREEN_WIDTH/2 - 60, SCREEN_HEIGHT/2 + 20, 30, DARKGRAY);