Added using snake size and snake growth

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

37
game.c
View File

@@ -31,6 +31,7 @@ int main(int argc, char *argv[])
char *input_file = NULL;
int width = 640, height = 480;
MLV_Keyboard_button touche = MLV_KEYBOARD_NONE;
int initial_size = 0;
Grid *g;
@@ -43,10 +44,12 @@ int main(int argc, char *argv[])
add_segment(snake, 1, 5);
add_segment(snake, 1, 4);
add_segment(snake, 1, 3);
add_segment(snake, 1, 2);
add_segment(snake, 1, 1);
add_segment(snake, 1, 0);
snake->dir = BOTTOM;
add_segment(snake, 1, 2);
add_segment(snake, 1, 1);
add_segment(snake, 1, 0);
snake->dir = BOTTOM;
initial_size += snake->size;
while ((opt = getopt_long(argc, argv, "hi:", long_options, &option_index)) != -1)
{
@@ -144,18 +147,32 @@ int main(int argc, char *argv[])
if (result == WALL || result == SNAKE)
{
MLV_draw_text(
width / 2 - 75, height / 2,
"Game Over! You hit something.",
MLV_COLOR_RED);
if (result == WALL)
{
MLV_draw_text(width / 2 - 75, height / 2, "Game Over! You hit a wall.", 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_wait_seconds(3);
break;
}
else if (result == FRUIT)
{
nb_fruit--;
if (nb_fruit == 0)
Position *tail = snake->segments_list;
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(
width / 2 - 75, height / 2,