pvjc24/a2/snake.c

49 lines
1.2 KiB
C
Raw Normal View History

2024-04-18 13:43:15 +00:00
#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;
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) {
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-18 13:43:15 +00:00
// 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;
2024-04-11 12:06:05 +00:00
}
2024-04-18 13:43:15 +00:00
if(result == END_FOOD) {
generate_food(state);
}
// TODO: Pridajte kód pre vykreslenie hry a spracovanie vstupu od hráča
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
}