#include #include #include #include "world.h" #define MOUSE_COUNT 5 struct game { int catx, caty; int mousex[MOUSE_COUNT]; int mousey[MOUSE_COUNT]; int alive[MOUSE_COUNT]; int score; }; void* init_game() { srand(time(NULL)); struct game* g = calloc(1, sizeof(struct game)); g->catx = 10; g->caty = 10; for (int i = 0; i < MOUSE_COUNT; i++) { g->mousex[i] = rand() % 40; g->mousey[i] = rand() % 20; g->alive[i] = 1; } g->score = 0; return g; } int game_event(struct event* e, void* p) { struct game* g = (struct game*)p; int W = 40; int H = 20; if (e->type == EVENT_KEY) { if (e->key == KEY_UP && g->caty > 0) g->caty--; if (e->key == KEY_DOWN && g->caty < H - 1) g->caty++; if (e->key == KEY_LEFT && g->catx > 0) g->catx--; if (e->key == KEY_RIGHT && g->catx < W - 1) g->catx++; } for (int i = 0; i < MOUSE_COUNT; i++) { if (g->alive[i]) { if (g->catx == g->mousex[i] && g->caty == g->mousey[i]) { g->alive[i] = 0; g->score++; } } } for (int i = 0; i < MOUSE_COUNT; i++) { if (!g->alive[i]) continue; int m = rand() % 4; if (m == 0 && g->mousey[i] > 0) g->mousey[i]--; if (m == 1 && g->mousey[i] < H - 1) g->mousey[i]++; if (m == 2 && g->mousex[i] > 0) g->mousex[i]--; if (m == 3 && g->mousex[i] < W - 1) g->mousex[i]++; } clear(); set_cell('C', g->catx, g->caty); for (int i = 0; i < MOUSE_COUNT; i++) { if (g->alive[i]) { set_cell('m', g->mousex[i], g->mousey[i]); } } if (g->score == MOUSE_COUNT) { return 1; } return 0; }