This commit is contained in:
Tančáková 2024-04-08 15:42:12 +02:00
parent 26ef46eebe
commit 5c64e5e1b0

View File

@ -1,86 +1,66 @@
#include "snake.h" #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h> #include <time.h>
#include <stdio.h> #include "snake.h"
#define WIDTH 20 // Global variables to store the direction of the snake
#define HEIGHT 10 int sx = 0; // Horizontal speed
#define INITIAL_SNAKE_LENGTH 5 int sy = 0; // Vertical speed
#define FOOD_COUNT 20
void initialize_world(struct state *state) { // Function to move the snake based on the direction
// Initialize snake in the middle of the world void move_snake(struct snake* head) {
int initial_x = WIDTH / 2; // Update the position of the head based on the current speed
int initial_y = HEIGHT / 2; head->x += sx;
state->snake = add_snake(NULL, initial_x, initial_y); head->y += sy;
if (state->snake == NULL) {
fprintf(stderr, "Failed to initialize snake.\n");
exit(EXIT_FAILURE);
}
// Initialize food
srand(time(NULL));
for (int i = 0; i < FOOD_COUNT; i++) {
state->foodx[i] = rand() % WIDTH;
state->foody[i] = rand() % HEIGHT;
}
// Initialize state parameters
state->sx = 1;
state->sy = 0;
state->width = WIDTH;
state->height = HEIGHT;
} }
void print_world(struct state *state) { // Function to update the direction of the snake based on user input
for (int y = 0; y < HEIGHT; y++) { void update_direction(int direction) {
for (int x = 0; x < WIDTH; x++) { switch(direction) {
int is_snake_part = is_snake(state->snake, x, y); case KEY_UP:
int is_food = 0; sx = 0;
for (int i = 0; i < FOOD_COUNT; i++) { sy = -1;
if (state->foodx[i] == x && state->foody[i] == y) { break;
is_food = 1; case KEY_DOWN:
break; sx = 0;
} sy = 1;
} break;
if (is_snake_part) { case KEY_LEFT:
printf("x"); sx = -1;
} else if (is_food) { sy = 0;
printf("*"); break;
} else { case KEY_RIGHT:
printf(" "); sx = 1;
} sy = 0;
} break;
printf("\n"); default:
}
}
int main() {
struct state game_state;
initialize_world(&game_state);
while (1) {
system("clear");
print_world(&game_state);
usleep(200000);
// Move snake
int end_game_reason = step_state(&game_state);
if (end_game_reason != END_CONTINUE) {
if (end_game_reason == END_SNAKE) {
printf("Snake hit itself! Game over.\n");
} else if (end_game_reason == END_WALL) {
printf("Snake hit the wall! Game over.\n");
} else if (end_game_reason == END_FOOD) {
printf("All food eaten! You win!\n");
} else {
printf("Game over due to unknown reason.\n");
}
break; break;
}
} }
}
free_snake(game_state.snake);
return EXIT_SUCCESS; // Function to handle collision detection and game logic
int handle_collision(struct state* state) {
// Check if the new head position is within the bounds of the game area
if (state->snake->x < 0 || state->snake->x >= state->width || state->snake->y < 0 || state->snake->y >= state->height) {
return END_WALL; // End game if the head hits the wall
}
// Check if the new head position overlaps with the snake's body
if (is_snake(state->snake, state->snake->x, state->snake->y)) {
return END_SNAKE; // End game if the head collides with the snake's body
}
// Check if the new head position overlaps with any food item
for (int i = 0; i < FOOD_COUNT; i++) {
if (state->foodx[i] == state->snake->x && state->foody[i] == state->snake->y) {
// Mark the food item as eaten
state->foodx[i] = -1;
state->foody[i] = -1;
return END_CONTINUE; // Continue game if the snake eats food
}
}
// If none of the above conditions are met, continue the game
return END_CONTINUE;
} }