#include #include #include #include "world.h" #include "game.h" #define NUM_MICE 5 // Start is called once at the beginning void* init_game() { struct game* st = calloc(1, sizeof(struct game)); // Initialize mice at random positions for (int i = 0; i < NUM_MICE; i++) { st->mousex[i] = rand() % 20 + 1; st->mousey[i] = rand() % 10 + 1; } st->catx = 0; st->caty = 0; st->catx_position = 15; st->caty_position = 15; return st; } // Step is called in a loop once per interval int game_event(struct event* event, void* game) { struct game* state = game; int width = event->width; int height = event->height; if (event->type == EVENT_ESC) { return 1; } // Handle keyboard input if (event->type == EVENT_KEY) { if (event->key == KEY_UP) { state->catx = 0; state->caty = -1; } else if (event->key == KEY_DOWN) { state->catx = 0; state->caty = 1; } else if (event->key == KEY_LEFT) { state->catx = -1; state->caty = 0; } else if (event->key == KEY_RIGHT) { state->catx = 1; state->caty = 0; } } if (event->type == EVENT_TIMEOUT) { int cx = state->catx_position + state->catx; int cy = state->caty_position + state->caty; // Keep cat inside bounds if (cx >= 1 && cx < width - 1) state->catx_position = cx; if (cy >= 1 && cy < height - 1) state->caty_position = cy; // Move mice randomly for (int i = 0; i < NUM_MICE; i++) { int m = rand() % 4; int mx = state->mousex[i]; int my = state->mousey[i]; if (m == 0 && my > 1) state->mousey[i]--; else if (m == 1 && my < height - 2) state->mousey[i]++; else if (m == 2 && mx > 1) state->mousex[i]--; else if (m == 3 && mx < width - 2) state->mousex[i]++; } } // Check if cat caught any mouse for (int i = 0; i < NUM_MICE; i++) { if (state->catx_position == state->mousex[i] && state->caty_position == state->mousey[i]) { state->mousex[i] = -1; // remove mouse from play state->mousey[i] = -1; } } // Draw world clear_screen(); set_color_cell('c', state->catx_position, state->caty_position, COLOR_YELLOW, COLOR_RED); set_color_cell('-', state->catx_position - 1, state->caty_position, COLOR_YELLOW, COLOR_GREEN); int mice_left = 0; for (int i = 0; i < NUM_MICE; i++) { if (state->mousex[i] >= 0) { set_cell('m', state->mousex[i], state->mousey[i]); mice_left++; } } sprintf(state->message, "Mysky zive: %d", mice_left); set_message(state->message, 1, 0); // If all mice are gone, show win message if (mice_left == 0) { set_message("Vsetky mysky chytene! Gratulujem!", width / 2 - 10, height / 2); } return 0; }