pvjc24/a2/snake.c
2024-04-18 15:43:15 +02:00

49 lines
1.2 KiB
C

#include <stdio.h>
#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;
}
}
// 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) {
state->foodx[i] = rand() % state->width;
state->foody[i] = rand() % state->height;
}
}
}
// Hlavná slučka hry
void game_loop(struct state* state) {
while(1) {
int result = step_state(state);
if(result == END_WALL || result == END_SNAKE) {
break;
}
if(result == END_FOOD) {
generate_food(state);
}
// TODO: Pridajte kód pre vykreslenie hry a spracovanie vstupu od hráča
}
}
int main() {
srand(time(NULL));
struct state state;
init_state(&state, 20, 20);
game_loop(&state);
free_snake(state.snake);
return 0;
}