diff --git a/emsdk b/emsdk new file mode 160000 index 0000000000000000000000000000000000000000..24fc909c0da13ef641d5ae75e89b5a97f25e37aa --- /dev/null +++ b/emsdk @@ -0,0 +1 @@ +Subproject commit 24fc909c0da13ef641d5ae75e89b5a97f25e37aa diff --git a/src/main.c b/src/main.c index 77132442cebf9d165114399cc8a93ec5a5fbffd7..9d1d2c1785be36af6ded4501678855256b84c39e 100644 --- a/src/main.c +++ b/src/main.c @@ -4,82 +4,123 @@ #include "game.h" #include <stdlib.h> #include <time.h> +#include <stdio.h> + +typedef enum { MENU, PLAYING, GAME_OVER } GameState; + +// Modified FlashScreen to work with main loop +void HandleFlashEffect(int *flashFrames, Color *flashColor) { + if (*flashFrames > 0) { + BeginDrawing(); + ClearBackground(*flashColor); + EndDrawing(); + (*flashFrames)--; + } +} int main(void) { - InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Simple Snake Game"); - SetTargetFPS(10); // Snake speed + InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Snake Game"); + SetTargetFPS(10); - srand(time(NULL)); + // Game state management + GameState gameState = MENU; + bool startGame = false; + int flashFrames = 0; + Color flashColor = BLANK; + // Game components + srand(time(NULL)); Snake snake; Food food; - - InitSnake(&snake); //center + InitSnake(&snake); SpawnFood(&food); - int score = 0; - bool gameOver = false; while (!WindowShouldClose()) { + // Handle flash effect first + if (flashFrames > 0) { + HandleFlashEffect(&flashFrames, &flashColor); + continue; // Skip other logic during flash + } + + // State machine + switch (gameState) { + case MENU: + if (IsKeyPressed(KEY_ENTER)) { + gameState = PLAYING; + flashColor = GREEN; + flashFrames = 5; // Short green flash when starting + } + break; - if (!gameOver) { - HandleInput(&snake); - UpdateSnake(&snake); + case PLAYING: + HandleInput(&snake); + UpdateSnake(&snake); - // Check if food eaten - if (snake.segments[0].x == food.position.x && - snake.segments[0].y == food.position.y) { - score += 1; - if (snake.length < MAX_CELLS) // check and new food appears + // Food collision + if (snake.segments[0].x == food.position.x && snake.segments[0].y == food.position.y) { + score++; snake.length++; - SpawnFood(&food); - } + SpawnFood(&food); + flashColor = YELLOW; + flashFrames = 5; + } - // Check for collision - if (CheckCollision(&snake)) - gameOver = true; + // Game over check + if (CheckCollision(&snake)) { + gameState = GAME_OVER; + flashColor = RED; + flashFrames = 10; + } + break; - } else { // Game Over state: press Enter to restart - if (IsKeyPressed(KEY_ENTER)) { - ResetGame(&snake, &food, &score, &gameOver); - } + case GAME_OVER: + if (IsKeyPressed(KEY_ENTER)) { + ResetGame(&snake, &food, &score, &gameState); + gameState = PLAYING; + flashColor = GREEN; + flashFrames = 5; + } + break; } - // Rendering + // Drawing BeginDrawing(); - ClearBackground(RAYWHITE); - - // Draw Snake - 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); - } - - // Draw Food - DrawRectangle(food.position.x * CELL_SIZE, - food.position.y * CELL_SIZE, - CELL_SIZE, CELL_SIZE, BLUE); - - // Draw Score - DrawText(TextFormat("Score: %d", score), - 10, 10, 20, DARKGRAY); - - // Game Over Message - if (gameOver) { - DrawText("GAME OVER!", SCREEN_WIDTH/2 - MeasureText("GAME OVER!",40)/2, - SCREEN_HEIGHT/2 - 40, 40, GREEN); - DrawText(TextFormat("Final Score: %d", score), - SCREEN_WIDTH/2 - MeasureText("Final Score: XXX",30)/2, - SCREEN_HEIGHT/2 + 10 ,30,DARKGRAY); - DrawText("Press ENTER to Restart", - SCREEN_WIDTH/2 - MeasureText("Press ENTER to Restart",20)/2, - SCREEN_HEIGHT/2 + 50 ,20,LIGHTGRAY); - } + ClearBackground(RAYWHITE); + + switch (gameState) { + case MENU: + DrawText("SNAKE GAME", SCREEN_WIDTH/2 - 100, SCREEN_HEIGHT/2 - 50, 40, GREEN); + DrawText("Press ENTER to Start", SCREEN_WIDTH/2 - 120, SCREEN_HEIGHT/2, 20, DARKGRAY); + break; + + case PLAYING: + // Draw snake + 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); + } + + // Draw food + DrawRectangle(food.position.x * CELL_SIZE, + food.position.y * CELL_SIZE, + CELL_SIZE, CELL_SIZE, BLUE); + + // Draw score + DrawText(TextFormat("Scoreee: %d", score), 10, 10, 20, DARKGRAY); + break; + + case GAME_OVER: + DrawText("GAME OVER!", SCREEN_WIDTH/2 - 100, SCREEN_HEIGHT/2 - 40, 40, RED); + DrawText(TextFormat("Scoreee: %d", score), SCREEN_WIDTH/2 - 60, SCREEN_HEIGHT/2 + 20, 30, DARKGRAY); + DrawText("Press ENTER to Restaeeert", SCREEN_WIDTH/2 - 140, SCREEN_HEIGHT/2 + 70, 20, GRAY); + break; + } + EndDrawing(); } CloseWindow(); - return 0; -} +} \ No newline at end of file