2024-04-11 12:39:01 +00:00
|
|
|
#include <stdio.h>
|
2024-04-11 12:06:05 +00:00
|
|
|
#include <stdlib.h>
|
2024-04-11 12:39:01 +00:00
|
|
|
#include <conio.h>
|
2024-04-11 12:06:05 +00:00
|
|
|
|
2024-04-11 12:39:01 +00:00
|
|
|
#define WIDTH 20
|
|
|
|
#define HEIGHT 20
|
|
|
|
#define FOOD_COUNT 3
|
|
|
|
|
|
|
|
enum EndGame { END_WALL, END_SNAKE, END_FOOD, END_CONTINUE };
|
|
|
|
|
|
|
|
struct snake {
|
|
|
|
int x, y;
|
|
|
|
struct snake* next;
|
|
|
|
};
|
2024-04-11 12:09:32 +00:00
|
|
|
|
|
|
|
struct state {
|
|
|
|
int width, height;
|
2024-04-11 12:39:01 +00:00
|
|
|
int sx, sy;
|
|
|
|
struct snake* snake;
|
|
|
|
int foodx[FOOD_COUNT];
|
|
|
|
int foody[FOOD_COUNT];
|
2024-04-11 12:09:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2024-04-11 12:39:01 +00:00
|
|
|
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);
|
2024-04-11 12:09:32 +00:00
|
|
|
|
2024-04-11 12:39:01 +00:00
|
|
|
int main() {
|
|
|
|
struct state game_state;
|
|
|
|
initialize(&game_state);
|
|
|
|
game_loop(&game_state);
|
|
|
|
cleanup(&game_state);
|
2024-04-11 12:09:32 +00:00
|
|
|
return 0;
|
2024-04-11 12:06:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 12:39:01 +00:00
|
|
|
void initialize(struct state* state) {
|
|
|
|
state->width = WIDTH;
|
|
|
|
state->height = HEIGHT;
|
|
|
|
state->sx = 1;
|
|
|
|
state->sy = 0;
|
|
|
|
state->snake = add_snake(NULL, 5, 5);
|
|
|
|
|
|
|
|
for (int i = 0; i < FOOD_COUNT; i++) {
|
|
|
|
state->foodx[i] = rand() % state->width;
|
|
|
|
state->foody[i] = rand() % state->height;
|
2024-04-11 12:06:05 +00:00
|
|
|
}
|
2024-04-11 12:09:32 +00:00
|
|
|
}
|
2024-04-11 12:06:05 +00:00
|
|
|
|
2024-04-11 12:39:01 +00:00
|
|
|
void render(struct state* state) {
|
|
|
|
system("cls");
|
|
|
|
|
|
|
|
for (int y = 0; y < state->height; y++) {
|
|
|
|
for (int x = 0; x < state->width; x++) {
|
|
|
|
if (is_snake(state->snake, x, y)) {
|
|
|
|
printf("* ");
|
|
|
|
} else {
|
|
|
|
int food = 0;
|
|
|
|
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");
|
2024-04-11 12:06:05 +00:00
|
|
|
}
|
2024-04-11 12:39:01 +00:00
|
|
|
}
|
2024-04-11 12:06:05 +00:00
|
|
|
|
2024-04-11 12:39:01 +00:00
|
|
|
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;
|
|
|
|
}
|
2024-04-11 12:06:05 +00:00
|
|
|
}
|
2024-04-11 12:39:01 +00:00
|
|
|
}
|
2024-04-11 12:06:05 +00:00
|
|
|
|
2024-04-11 12:39:01 +00:00
|
|
|
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;
|
2024-04-11 12:06:05 +00:00
|
|
|
}
|
2024-04-11 12:39:01 +00:00
|
|
|
|
2024-04-11 12:06:05 +00:00
|
|
|
}
|
2024-04-11 12:39:01 +00:00
|
|
|
}
|
2024-04-11 12:06:05 +00:00
|
|
|
|
2024-04-11 12:39:01 +00:00
|
|
|
void cleanup(struct state* state) {
|
|
|
|
free_snake(state->snake);
|
2024-04-11 12:06:05 +00:00
|
|
|
}
|