funguje
This commit is contained in:
parent
26ef46eebe
commit
5c64e5e1b0
120
a2/snake.c
120
a2/snake.c
@ -1,86 +1,66 @@
|
||||
#include "snake.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include "snake.h"
|
||||
|
||||
#define WIDTH 20
|
||||
#define HEIGHT 10
|
||||
#define INITIAL_SNAKE_LENGTH 5
|
||||
#define FOOD_COUNT 20
|
||||
// Global variables to store the direction of the snake
|
||||
int sx = 0; // Horizontal speed
|
||||
int sy = 0; // Vertical speed
|
||||
|
||||
void initialize_world(struct state *state) {
|
||||
// Initialize snake in the middle of the world
|
||||
int initial_x = WIDTH / 2;
|
||||
int initial_y = HEIGHT / 2;
|
||||
state->snake = add_snake(NULL, initial_x, initial_y);
|
||||
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;
|
||||
// Function to move the snake based on the direction
|
||||
void move_snake(struct snake* head) {
|
||||
// Update the position of the head based on the current speed
|
||||
head->x += sx;
|
||||
head->y += sy;
|
||||
}
|
||||
|
||||
void print_world(struct state *state) {
|
||||
for (int y = 0; y < HEIGHT; y++) {
|
||||
for (int x = 0; x < WIDTH; x++) {
|
||||
int is_snake_part = is_snake(state->snake, x, y);
|
||||
int is_food = 0;
|
||||
for (int i = 0; i < FOOD_COUNT; i++) {
|
||||
if (state->foodx[i] == x && state->foody[i] == y) {
|
||||
is_food = 1;
|
||||
// Function to update the direction of the snake based on user input
|
||||
void update_direction(int direction) {
|
||||
switch(direction) {
|
||||
case KEY_UP:
|
||||
sx = 0;
|
||||
sy = -1;
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
sx = 0;
|
||||
sy = 1;
|
||||
break;
|
||||
case KEY_LEFT:
|
||||
sx = -1;
|
||||
sy = 0;
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
sx = 1;
|
||||
sy = 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (is_snake_part) {
|
||||
printf("x");
|
||||
} else if (is_food) {
|
||||
printf("*");
|
||||
} else {
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
}
|
||||
|
||||
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");
|
||||
// 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
|
||||
}
|
||||
break;
|
||||
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
||||
free_snake(game_state.snake);
|
||||
return EXIT_SUCCESS;
|
||||
// If none of the above conditions are met, continue the game
|
||||
return END_CONTINUE;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user