pvjc26/a5/game.c
2026-05-09 11:48:11 +00:00

126 lines
2.1 KiB
C

#include <stdlib.h>
#include <time.h>
#include <string.h>
#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;
}