This commit is contained in:
Macko 2024-04-15 22:31:53 +02:00
parent 2ae393fdd1
commit 79cb8d5f9e

View File

@ -1,118 +1,57 @@
#include <stdio.h> struct snake* add_snake(struct snake* old_head, int x, int y) {
#include <stdlib.h> struct snake* new_head = (struct snake*) malloc(sizeof(struct snake));
#include <conio.h> new_head->x = x;
new_head->y = y;
new_head->next = old_head;
return new_head;
}
#define WIDTH 20 int is_snake(struct snake* snake, int x, int y) {
#define HEIGHT 20 while (snake != NULL) {
#define FOOD_COUNT 3 if (snake->x == x && snake->y == y) {
return 1;
enum EndGame { END_WALL, END_SNAKE, END_FOOD, END_CONTINUE }; }
snake = snake->next;
struct snake { }
int x, y;
struct snake* next;
};
struct state {
int width, height;
int sx, sy;
struct snake* snake;
int foodx[FOOD_COUNT];
int foody[FOOD_COUNT];
};
void initialize(struct state* state);
void render(struct state* state);
void handle_input(struct state* state);
void game_loop(struct state* state);
void cleanup(struct state* state);
int main() {
struct state game_state;
initialize(&game_state);
game_loop(&game_state);
cleanup(&game_state);
return 0; return 0;
} }
void initialize(struct state* state) { void free_snake(struct snake* snake) {
state->width = WIDTH; while (snake != NULL) {
state->height = HEIGHT; struct snake* next = snake->next;
state->sx = 1; free(snake);
state->sy = 0; snake = next;
state->snake = add_snake(NULL, 5, 5); }
}
int step_state(struct state* state) {
int new_x = state->snake->x + state->sx;
int new_y = state->snake->y + state->sy;
if (new_x < 0 || new_x >= state->width || new_y < 0 || new_y >= state->height) {
return END_WALL;
}
if (is_snake(state->snake->next, new_x, new_y)) {
return END_SNAKE;
}
for (int i = 0; i < FOOD_COUNT; i++) { for (int i = 0; i < FOOD_COUNT; i++) {
state->foodx[i] = rand() % state->width; if (state->foodx[i] == new_x && state->foody[i] == new_y) {
state->foody[i] = rand() % state->height; state->foodx[i] = -1;
state->foody[i] = -1;
state->snake = add_snake(state->snake, new_x, new_y);
return END_FOOD;
} }
} }
void render(struct state* state) { state->snake = add_snake(state->snake, new_x, new_y);
system("cls"); struct snake* tail = state->snake;
while (tail->next->next != NULL) {
for (int y = 0; y < state->height; y++) { tail = tail->next;
for (int x = 0; x < state->width; x++) { }
if (is_snake(state->snake, x, y)) { free(tail->next);
printf("* "); tail->next = NULL;
} else {
int food = 0; return END_CONTINUE;
for (int i = 0; i < FOOD_COUNT; i++) {
if (state->foodx[i] == x && state->foody[i] == y) {
printf("o ");
food = 1;
break;
}
}
if (!food) printf(". ");
}
}
printf("\n");
}
}
void handle_input(struct state* state) {
if (_kbhit()) {
char key = _getch();
switch (key) {
case 'w':
state->sx = 0; state->sy = -1;
break;
case 's':
state->sx = 0; state->sy = 1;
break;
case 'a':
state->sx = -1; state->sy = 0;
break;
case 'd':
state->sx = 1; state->sy = 0;
break;
case 'q':
exit(0);
break;
default:
break;
}
}
}
void game_loop(struct state* state) {
while (1) {
render(state);
handle_input(state);
int result = step_state(state);
if (result == END_WALL || result == END_SNAKE) {
printf("Game Over!\n");
break;
} else if (result == END_FOOD) {
printf("You ate all the food! You win!\n");
break;
}
}
}
void cleanup(struct state* state) {
free_snake(state->snake);
} }