From 3612d8e5aaad0b0f7b96eede1f52fef0baecb9c2 Mon Sep 17 00:00:00 2001 From: "Ahmet Sungur (Student)" <ahmet2.sungur@live.uwe.ac.uk@csctcloud.prxhn32zsyjupl12zde3wlfkch.cwx.internal.cloudapp.net> Date: Wed, 2 Apr 2025 17:56:46 +0100 Subject: [PATCH] final commit hopefully --- src/game.h | 4 +-- src/main.c | 73 +++++++++++++++++++++++++++++++------------------- src/obstacle.c | 22 ++++++++++----- 3 files changed, 63 insertions(+), 36 deletions(-) diff --git a/src/game.h b/src/game.h index 4b72838..cd59abc 100644 --- a/src/game.h +++ b/src/game.h @@ -8,11 +8,11 @@ // Game state enum (added but doesn't conflict with existing bool gameOver) typedef enum { MENU, PLAYING, PAUSED, GAME_OVER } GameState; -// Your existing functions (unchanged) + void HandleInput(Snake *snake); 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); diff --git a/src/main.c b/src/main.c index 805a724..df1bbd0 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,7 @@ #include "snake.h" #include "food.h" -#include "game.h" // Now contains GameState enum +#include "game.h" #include "obstacle.h" #include <stdlib.h> #include <time.h> @@ -9,7 +9,7 @@ #include "snake.h" #include "raylib.h" -// Remove the GameState enum - it's now in game.h + void HandleFlashEffect(int *flashFrames, Color *flashColor) { if (*flashFrames > 0) { @@ -20,7 +20,7 @@ void HandleFlashEffect(int *flashFrames, Color *flashColor) { } } -// Change the function name to match game.h + void ResetGameWithObstacles(Snake *snake, Food *food, Obstacle obstacles[], int *score, int *level, GameState *state) { InitSnake(snake); SpawnFood(food); @@ -63,11 +63,10 @@ int main(void) { continue; } - // State machine + switch (gameState) { case MENU: if (IsKeyPressed(KEY_ENTER)) { - // Changed to ResetGameWithObstacles ResetGameWithObstacles(&snake, &food, obstacles, &score, &gameState, &level); flashColor = PURPLE; flashFrames = 1; @@ -97,7 +96,7 @@ int main(void) { // Collision checks if (CheckCollision(&snake) || CheckObstacleCollision(&snake, obstacles, MAX_OBSTACLES)) { gameState = GAME_OVER; - flashColor = RED; + flashColor = RED ; flashFrames = 2; } break; @@ -107,22 +106,25 @@ int main(void) { case GAME_OVER: if (IsKeyPressed(KEY_ENTER)) { - // Changed to ResetGameWithObstacles ResetGameWithObstacles(&snake, &food, obstacles, &score, &gameState, &level); flashColor = GREEN; flashFrames = 5; } break; } - - // ... rest of your drawing code remains exactly the same ... BeginDrawing(); - ClearBackground(RAYWHITE); + // DRAWING GRIDS + for (int i = 0; i < SCREEN_WIDTH/CELL_SIZE; i++) { + DrawLine(i * CELL_SIZE, 0, i * CELL_SIZE, SCREEN_HEIGHT, Fade(LIGHTGRAY, 0.2f)); + } + for (int i = 0; i < SCREEN_HEIGHT/CELL_SIZE; i++) { + DrawLine(0, i * CELL_SIZE, SCREEN_WIDTH, i * CELL_SIZE, Fade(LIGHTGRAY, 0.2f)); + } 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); + DrawText("SNAKE GAME", SCREEN_WIDTH/2 - 150, SCREEN_HEIGHT/2 - 50, 40, GREEN); + DrawText("Press ENTER to Start", SCREEN_WIDTH/2 - 150, SCREEN_HEIGHT/2 -1, 30, GREEN); break; case PLAYING: @@ -149,16 +151,23 @@ int main(void) { } // Draw food - DrawRectangle(food.position.x * CELL_SIZE, - food.position.y * CELL_SIZE, - CELL_SIZE, CELL_SIZE, BLUE); + DrawCircle(food.position.x * CELL_SIZE + CELL_SIZE/2, + food.position.y * CELL_SIZE + CELL_SIZE/2, + CELL_SIZE/3, RED); + // Add details to make it look like an apple + DrawCircle(food.position.x * CELL_SIZE + CELL_SIZE/2, + food.position.y * CELL_SIZE + CELL_SIZE/2, + CELL_SIZE/3, BROWN); // Stem + DrawCircle(food.position.x * CELL_SIZE + CELL_SIZE/3, + food.position.y * CELL_SIZE + CELL_SIZE/3, + CELL_SIZE/12, GREEN); // Leaf // Draw obstacles DrawObstacles(obstacles, MAX_OBSTACLES); // Draw score and level - DrawText(TextFormat("Score: %d", score), 10, 10, 20, DARKGRAY); - DrawText(TextFormat("Level: %d", level), 10, 40, 20, DARKGRAY); + DrawText(TextFormat("Score: %d", score), 10, 10, 20, RED); + DrawText(TextFormat("Level: %d", level), 10, 40, 20, RED); break; case PAUSED: @@ -183,27 +192,35 @@ int main(void) { } } - DrawRectangle(food.position.x * CELL_SIZE, - food.position.y * CELL_SIZE, - CELL_SIZE, CELL_SIZE, BLUE); + // Replace your food drawing code with: + DrawCircle(food.position.x * CELL_SIZE + CELL_SIZE/2, + food.position.y * CELL_SIZE + CELL_SIZE/2, + CELL_SIZE/3, RED); + // Add details to make it look like an apple + DrawCircle(food.position.x * CELL_SIZE + CELL_SIZE/2, + food.position.y * CELL_SIZE + CELL_SIZE/4, + CELL_SIZE/12, BROWN); // Stem + DrawCircle(food.position.x * CELL_SIZE + CELL_SIZE/3, + food.position.y * CELL_SIZE + CELL_SIZE/3, + CELL_SIZE/12, GREEN); // Leaf 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("Paused", SCREEN_WIDTH/2 -150, 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); + DrawText(TextFormat("Level: %d", level), 10, 10, 20, RED); + DrawText(TextFormat("Score: %d", score), 10, 40, 20, RED); break; - + // TRIED TO MAKE GAME OVER MENU TEXTS LOOK BETTER :) 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); - DrawText(TextFormat("Level: %d", level), SCREEN_WIDTH/2 - 60, SCREEN_HEIGHT/2 + 60, 30, DARKGRAY); - DrawText("Press ENTER to restart", SCREEN_WIDTH/2 - 140, SCREEN_HEIGHT/2 + 110, 20, GRAY); + DrawText("GAME OVER!", SCREEN_WIDTH/2 - 150, SCREEN_HEIGHT/2 - 40, 40, RED); + DrawText(TextFormat("Score: %d", score), SCREEN_WIDTH/2 - 120, SCREEN_HEIGHT/2 + 20, 30, RED); + DrawText(TextFormat("Level: %d", level), SCREEN_WIDTH/2 - 120, SCREEN_HEIGHT/2 + 60, 30, RED); + DrawText("Press ENTER to restart", SCREEN_WIDTH/2 -180, SCREEN_HEIGHT/2 + 110, 30, RED); break; } diff --git a/src/obstacle.c b/src/obstacle.c index 9cdc035..9651a80 100644 --- a/src/obstacle.c +++ b/src/obstacle.c @@ -56,13 +56,23 @@ void SpawnObstacle(Obstacle obstacles[], int count, const Snake *snake, const Fo } void DrawObstacles(const Obstacle obstacles[], int count) { - for (int i = 0; i < count; i++) { + for (int i = 0; i < MAX_OBSTACLES; i++) { if (obstacles[i].active) { - DrawRectangle( - obstacles[i].position.x * CELL_SIZE, - obstacles[i].position.y * CELL_SIZE, - CELL_SIZE, CELL_SIZE, OBSTACLE_COLOR - ); + // Main rock shape + DrawRectangle(obstacles[i].position.x * CELL_SIZE, + obstacles[i].position.y * CELL_SIZE, + CELL_SIZE, CELL_SIZE, GRAY); + // Rock details + DrawLine(obstacles[i].position.x * CELL_SIZE + 2, + obstacles[i].position.y * CELL_SIZE + 2, + obstacles[i].position.x * CELL_SIZE + CELL_SIZE - 2, + obstacles[i].position.y * CELL_SIZE + CELL_SIZE - 2, + DARKGRAY); + DrawLine(obstacles[i].position.x * CELL_SIZE + CELL_SIZE - 2, + obstacles[i].position.y * CELL_SIZE + 2, + obstacles[i].position.x * CELL_SIZE + 2, + obstacles[i].position.y * CELL_SIZE + CELL_SIZE - 2, + DARKGRAY); } } } -- GitLab