#include #include #include #include "world.h" // Game state structure struct state { int width; int height; int sx; int sy; struct snake* snake; int foodx[5]; int foody[5]; }; // Initialize game state void* init_game() { struct state* st = calloc(1, sizeof(struct state)); return st; } // Initialize game state void init_snake(struct event* world, struct state* st) { int cy = world->height / 2; int cx = world->width / 2 - 5; for (int i = 0; i < 5; i++) { st->snake = add_snake(st->snake, cx + i, cy); } int h = world->height; int w = world->width; for (int i = 0; i < 5; i++) { st->foodx[i] = rand() % w; st->foody[i] = rand() % h; } st->sx = 1; st->sy = 0; } // Handle game events int world_event(struct event* w, void* game) { struct state* st = game; if (w->type == EVENT_START) { init_snake(w, st); } else if (w->type == EVENT_KEY) { int key = w->key; if (key == KEY_RIGHT) { st->sx = 1; st->sy = 0; } else if (key == KEY_LEFT) { st->sx = -1; st->sy = 0; } else if (key == KEY_DOWN) { st->sx = 0; st->sy = 1; } else if (key == KEY_UP) { st->sx = 0; st->sy = -1; } } else if (w->type == EVENT_ESC) { return 1; } else if (w->type == EVENT_TIMEOUT) { clear_screen(); st->width = w->width; st->height = w->height; int r = step_state(st); char ms[200]; sprintf(ms, "r %d\n", r); set_message(ms, 9, 9); struct snake* sn = st->snake; while (sn != NULL) { set_cell('x', sn->x, sn->y); sn = sn->next; } for (int i = 0; i < FOOD_COUNT; i++) { if (st->foodx[i] >= 0 && st->foody[i] >= 0) { set_cell('*', st->foodx[i], st->foody[i]); } } if (r) { char message[] = "Koniec"; set_message(message, 10, 10); } } return 0; } int main(int argc, char** argv) { start_world(init_game, world_event, free); return 0; }