From b75fa3ccba12c90e8ea6bb204e75d5b23c89e8c8 Mon Sep 17 00:00:00 2001 From: mk570rp Date: Sat, 9 May 2026 11:48:11 +0000 Subject: [PATCH] a5 --- a5/game.c | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ a5/game.h | 10 +++++ 2 files changed, 135 insertions(+) create mode 100644 a5/game.c create mode 100644 a5/game.h diff --git a/a5/game.c b/a5/game.c new file mode 100644 index 0000000..ab9288c --- /dev/null +++ b/a5/game.c @@ -0,0 +1,125 @@ +#include +#include +#include + +#include "game.h" + +#define MOUSE_COUNT 5 + +struct game { + int catx; + int caty; + + int mousex[MOUSE_COUNT]; + int mousey[MOUSE_COUNT]; + + int alive[MOUSE_COUNT]; + + int score; + + char message[100]; +}; + +void* init_game() { + + srand(time(NULL)); + + struct game* game = calloc(1, sizeof(struct game)); + + game->catx = 10; + game->caty = 10; + + for (int i = 0; i < MOUSE_COUNT; i++) { + game->mousex[i] = rand() % 20; + game->mousey[i] = rand() % 20; + game->alive[i] = 1; + } + + return game; +} + +int world_event(struct event* event, void* g) { + + struct game* game = (struct game*)g; + + if (event->type == EVENT_KEY) { + + if (event->key == KEY_UP) { + game->caty--; + } + + if (event->key == KEY_DOWN) { + game->caty++; + } + + if (event->key == KEY_LEFT) { + game->catx--; + } + + if (event->key == KEY_RIGHT) { + game->catx++; + } + } + + for (int i = 0; i < MOUSE_COUNT; i++) { + + if (game->alive[i] == 1) { + + if (game->catx == game->mousex[i] && + game->caty == game->mousey[i]) { + + game->alive[i] = 0; + game->score++; + } + } + } + + + for (int i = 0; i < MOUSE_COUNT; i++) { + + if (game->alive[i] == 1) { + + int m = rand() % 4; + + if (m == 0) { + game->mousex[i]++; + } + + if (m == 1) { + game->mousex[i]--; + } + + if (m == 2) { + game->mousey[i]++; + } + + if (m == 3) { + game->mousey[i]--; + } + } + } + + clear(); + + set_cell('c', game->catx, game->caty); + + // vykreslenie myšiek + for (int i = 0; i < MOUSE_COUNT; i++) { + + if (game->alive[i] == 1) { + set_cell('m', game->mousex[i], game->mousey[i]); + } + } + + // koniec hry + if (game->score == MOUSE_COUNT) { + + strcpy(game->message, "Koniec hry"); + + set_str(game->message, 1, 1); + + return 1; + } + + return 0; +} diff --git a/a5/game.h b/a5/game.h new file mode 100644 index 0000000..a9ee9de --- /dev/null +++ b/a5/game.h @@ -0,0 +1,10 @@ +#ifndef GAME_H +#define GAME_H + +#include "world.h" + +void* init_game(); + +int world_event(struct event* event, void* g); + +#endif