This commit is contained in:
Jakub Frankovič 2026-05-08 13:28:24 +02:00
parent cd81b53a59
commit 076d06a307
3 changed files with 26 additions and 14 deletions

16
du4/program.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
#include <unistd.h>
#include "game.h"
int main() {
printf("Starting Snake Game...\n");
printf("Use arrow keys to move the snake.\n");
printf("Press 'q' to quit.\n");
printf("Eat all food (*) to win!\n\n");
sleep(2);
run_game();
printf("Thanks for playing!\n");
return 0;
}

View File

@ -64,6 +64,12 @@ int step_state(struct state* state) {
return END_WALL;
}
// Check if new position is on snake's body
if (is_snake(state->snake, new_x, new_y)) {
return END_SNAKE;
}
// Find food at new position
int food_index = -1;
int items_left = 0;
@ -76,14 +82,9 @@ int step_state(struct state* state) {
}
if (food_index >= 0) {
// Food found - mark as eaten and grow snake
state->foodx[food_index] = -1;
state->foody[food_index] = -1;
if (is_snake(state->snake, new_x, new_y)) {
state->snake = add_snake(state->snake, new_x, new_y);
return END_SNAKE;
}
state->snake = add_snake(state->snake, new_x, new_y);
if (items_left == 0) {
@ -91,14 +92,9 @@ int step_state(struct state* state) {
}
return END_CONTINUE;
} else {
// No food - normal movement (remove tail and add head)
state->snake = remove_snake(state->snake);
if (is_snake(state->snake, new_x, new_y)) {
state->snake = add_snake(state->snake, new_x, new_y);
return END_SNAKE;
}
state->snake = add_snake(state->snake, new_x, new_y);
return END_CONTINUE;
}
}
}

View File

@ -2,7 +2,7 @@
#define snake_h_INCLUDED
// Number of food items on the plane
#define FOOD_COUNT 5
#define FOOD_COUNT 20
/**
* One part of the snake;