Added using snake size and snake growth

This commit is contained in:
2025-05-20 14:35:47 +02:00
parent d694422e66
commit 4798ec85a3

29
game.c
View File

@@ -31,6 +31,7 @@ int main(int argc, char *argv[])
char *input_file = NULL; char *input_file = NULL;
int width = 640, height = 480; int width = 640, height = 480;
MLV_Keyboard_button touche = MLV_KEYBOARD_NONE; MLV_Keyboard_button touche = MLV_KEYBOARD_NONE;
int initial_size = 0;
Grid *g; Grid *g;
@@ -48,6 +49,8 @@ int main(int argc, char *argv[])
add_segment(snake, 1, 0); add_segment(snake, 1, 0);
snake->dir = BOTTOM; snake->dir = BOTTOM;
initial_size += snake->size;
while ((opt = getopt_long(argc, argv, "hi:", long_options, &option_index)) != -1) while ((opt = getopt_long(argc, argv, "hi:", long_options, &option_index)) != -1)
{ {
switch (opt) switch (opt)
@@ -144,18 +147,32 @@ int main(int argc, char *argv[])
if (result == WALL || result == SNAKE) if (result == WALL || result == SNAKE)
{ {
MLV_draw_text( if (result == WALL)
width / 2 - 75, height / 2, {
"Game Over! You hit something.", MLV_draw_text(width / 2 - 75, height / 2, "Game Over! You hit a wall.", MLV_COLOR_RED);
MLV_COLOR_RED); }
else if (result == SNAKE)
{
MLV_draw_text(width / 2 - 75, height / 2, "Game Over! You hit yourself.", MLV_COLOR_RED);
}
MLV_actualise_window(); MLV_actualise_window();
MLV_wait_seconds(3); MLV_wait_seconds(3);
break; break;
} }
else if (result == FRUIT) else if (result == FRUIT)
{ {
nb_fruit--; Position *tail = snake->segments_list;
if (nb_fruit == 0) Position *prev = NULL;
while (tail->next != NULL)
{
prev = tail;
tail = tail->next;
}
int dx = tail->x - (prev ? prev->x : tail->x);
int dy = tail->y - (prev ? prev->y : tail->y);
add_segment(snake, tail->x + dx, tail->y + dy);
place_snake(g, snake);
if (snake->size == initial_size + nb_fruit)
{ {
MLV_draw_text( MLV_draw_text(
width / 2 - 75, height / 2, width / 2 - 75, height / 2,