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 <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:
|
||||||
|
sx = 0;
|
||||||
|
sy = 1;
|
||||||
|
break;
|
||||||
|
case KEY_LEFT:
|
||||||
|
sx = -1;
|
||||||
|
sy = 0;
|
||||||
|
break;
|
||||||
|
case KEY_RIGHT:
|
||||||
|
sx = 1;
|
||||||
|
sy = 0;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (is_snake_part) {
|
|
||||||
printf("x");
|
|
||||||
} else if (is_food) {
|
|
||||||
printf("*");
|
|
||||||
} else {
|
|
||||||
printf(" ");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printf("\n");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
// Function to handle collision detection and game logic
|
||||||
struct state game_state;
|
int handle_collision(struct state* state) {
|
||||||
initialize_world(&game_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) {
|
||||||
while (1) {
|
return END_WALL; // End game if the head hits the wall
|
||||||
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;
|
|
||||||
|
// 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);
|
// If none of the above conditions are met, continue the game
|
||||||
return EXIT_SUCCESS;
|
return END_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user