Added boosts system which change the snake speed

This commit is contained in:
2025-05-27 14:58:57 +02:00
parent 9c053fe61c
commit 9a754e94be
3 changed files with 64 additions and 6 deletions

51
game.c
View File

@@ -7,7 +7,7 @@
#include "grid.h"
#include "snake.h"
#define DIFFICULTY 6
#define BASE_SPEED 6
void print_help()
{
@@ -35,6 +35,12 @@ int main(int argc, char *argv[])
int current_level = 1;
int global_score = 0;
int previous_score = 0;
int speed = BASE_SPEED;
int boosts = 0;
int boost_spawn_timer = 0;
int boost_spawn_interval = 0;
int boost_duration = 0;
const int boost_duration_frames = 5 * 24;
Grid *g;
@@ -152,9 +158,27 @@ int main(int argc, char *argv[])
char stats[256];
MLV_clear_window(MLV_COLOR_BLACK);
loop_count = (loop_count + 1) % DIFFICULTY;
loop_count = (loop_count + 1) % speed;
if (loop_count == 0)
{
if (boost_spawn_timer >= boost_spawn_interval)
{
int x, y;
do
{
x = rand() % g->nbc;
y = rand() % g->nbl;
} while (g->grid[y][x] != EMPTY);
g->grid[y][x] = BOOST;
boost_spawn_timer = 0;
boost_spawn_interval = 50 + rand() % BASE_SPEED;
}
else
{
boost_spawn_timer++;
}
result = move_snake(snake, g);
if (result == WALL || result == SNAKE)
@@ -173,6 +197,14 @@ int main(int argc, char *argv[])
global_score = previous_score;
break;
}
else if (result == BOOST)
{
boosts++;
boost_duration = boost_duration_frames;
speed = BASE_SPEED - boosts;
speed = (BASE_SPEED - boosts) > 1 ? (BASE_SPEED - boosts) : 1;
printf("Boost collected! Speed increased. Boost duration: %d frames\n", boost_duration);
}
else if (result == FRUIT)
{
Position *tail = snake->segments_list;
@@ -203,11 +235,22 @@ int main(int argc, char *argv[])
}
}
if (boost_duration > 0)
{
boost_duration--;
if (boost_duration == 0)
{
boosts--;
speed = (BASE_SPEED - boosts) > 1 ? (BASE_SPEED - boosts) : 1;
printf("Boost expired. Speed reset. Active boosts: %d\n", boosts);
}
}
draw_grid(g);
snprintf(stats, sizeof(stats),
"Level: %d | Fruits Left: %d/%d | Score: %d | Snake Size: %d",
current_level, nb_fruit, total_fruits, global_score, snake->size);
"Level: %d | Fruits Left: %d/%d | Score: %d | Snake Size: %d | Boosts: %d",
current_level, nb_fruit, total_fruits, global_score, snake->size, boosts);
MLV_draw_text(10, height - 20, stats, MLV_COLOR_WHITE);
MLV_actualise_window();

17
grid.c
View File

@@ -69,11 +69,12 @@ void draw_grid(Grid *g)
int window_width = MLV_get_window_width();
int window_height = MLV_get_window_height();
int cell_size = compute_size(g, window_width, window_height);
MLV_Image *image_wall, *image_fruit, *image_snake, *image_empty;
MLV_Image *image_wall, *image_fruit, *image_snake, *image_snake_head, *image_boost, *image_empty;
image_wall = MLV_load_image("./assets/wall.png");
image_fruit = MLV_load_image("./assets/fruit.png");
image_snake = MLV_load_image("./assets/snake.png");
image_snake_head = MLV_load_image("./assets/snake_head.png");
image_boost = MLV_load_image("./assets/boost.png");
image_empty = MLV_load_image("./assets/grass.png");
if (image_wall != NULL)
{
@@ -91,6 +92,10 @@ void draw_grid(Grid *g)
{
MLV_resize_image_with_proportions(image_snake_head, cell_size, cell_size);
}
if (image_boost != NULL)
{
MLV_resize_image_with_proportions(image_boost, cell_size, cell_size);
}
if (image_empty != NULL)
{
MLV_resize_image_with_proportions(image_empty, cell_size, cell_size);
@@ -158,6 +163,16 @@ void draw_grid(Grid *g)
MLV_draw_image(image_snake_head, x, y);
}
break;
case BOOST:
if (image_boost == NULL)
{
MLV_draw_filled_rectangle(x, y, cell_size, cell_size, MLV_COLOR_YELLOW);
}
else
{
MLV_draw_image(image_boost, x, y);
}
break;
default:
MLV_draw_filled_rectangle(x, y, cell_size, cell_size, MLV_COLOR_BLACK);
break;

2
grid.h
View File

@@ -21,7 +21,7 @@ typedef enum
FRUIT = 'f',
SNAKE = 's',
SNAKEHEAD = 'S',
SNAKETAIL = 't'
BOOST = 'b'
} Element;
Grid *allocate_grid(int n, int m);