2024-04-18 13:43:15 +00:00
|
|
|
#include <stdio.h>
|
2024-04-18 13:46:41 +00:00
|
|
|
#include <stdlib.h>
|
2024-04-18 13:43:15 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
// Inicializuje stav hry
|
|
|
|
void init_state(struct state* state, int width, int height) {
|
|
|
|
state->width = width;
|
|
|
|
state->height = height;
|
|
|
|
state->snake = add_snake(NULL, width / 2, height / 2);
|
|
|
|
state->sx = 0;
|
|
|
|
state->sy = 1;
|
|
|
|
for(int i = 0; i < FOOD_COUNT; i++) {
|
|
|
|
state->foodx[i] = rand() % width;
|
|
|
|
state->foody[i] = rand() % height;
|
2024-04-15 20:31:53 +00:00
|
|
|
}
|
2024-04-11 12:06:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-18 13:43:15 +00:00
|
|
|
// Generuje nové jedlo na náhodnej pozícii
|
|
|
|
void generate_food(struct state* state) {
|
|
|
|
for(int i = 0; i < FOOD_COUNT; i++) {
|
|
|
|
if(state->foodx[i] == -1) {
|
2024-04-18 13:46:41 +00:00
|
|
|
do {
|
|
|
|
state->foodx[i] = rand() % state->width;
|
|
|
|
state->foody[i] = rand() % state->height;
|
|
|
|
} while(is_snake(state->snake, state->foodx[i], state->foody[i]));
|
2024-04-18 13:43:15 +00:00
|
|
|
}
|
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-18 13:46:41 +00:00
|
|
|
// Vykresli stav hry
|
|
|
|
void draw_state(struct state* state) {
|
|
|
|
for(int y = 0; y < state->height; y++) {
|
|
|
|
for(int x = 0; x < state->width; x++) {
|
|
|
|
if(is_snake(state->snake, x, y)) {
|
|
|
|
printf("S");
|
|
|
|
} else {
|
|
|
|
int is_food = 0;
|
|
|
|
for(int i = 0; i < FOOD_COUNT; i++) {
|
|
|
|
if(state->foodx[i] == x && state->foody[i] == y) {
|
|
|
|
is_food = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf(is_food ? "F" : ".");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Spracuje vstup od hráča
|
|
|
|
void process_input(struct state* state) {
|
|
|
|
char input = getchar();
|
|
|
|
switch(input) {
|
|
|
|
case 'w': state->sx = 0; state->sy = -1; break;
|
|
|
|
case 'a': state->sx = -1; state->sy = 0; break;
|
|
|
|
case 's': state->sx = 0; state->sy = 1; break;
|
|
|
|
case 'd': state->sx = 1; state->sy = 0; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-18 13:43:15 +00:00
|
|
|
// Hlavná slučka hry
|
|
|
|
void game_loop(struct state* state) {
|
|
|
|
while(1) {
|
2024-04-18 13:46:41 +00:00
|
|
|
draw_state(state);
|
|
|
|
process_input(state);
|
2024-04-18 13:43:15 +00:00
|
|
|
int result = step_state(state);
|
|
|
|
if(result == END_WALL || result == END_SNAKE) {
|
2024-04-18 13:46:41 +00:00
|
|
|
printf("Game over!\n");
|
2024-04-18 13:43:15 +00:00
|
|
|
break;
|
2024-04-11 12:06:05 +00:00
|
|
|
}
|
2024-04-18 13:43:15 +00:00
|
|
|
if(result == END_FOOD) {
|
|
|
|
generate_food(state);
|
|
|
|
}
|
2024-04-11 12:06:05 +00:00
|
|
|
}
|
2024-04-18 13:43:15 +00:00
|
|
|
}
|
2024-04-11 12:06:05 +00:00
|
|
|
|
2024-04-18 13:43:15 +00:00
|
|
|
int main() {
|
|
|
|
srand(time(NULL));
|
|
|
|
struct state state;
|
|
|
|
init_state(&state, 20, 20);
|
|
|
|
game_loop(&state);
|
|
|
|
free_snake(state.snake);
|
|
|
|
return 0;
|
2024-04-11 12:06:05 +00:00
|
|
|
}
|