47 lines
1.1 KiB
C
47 lines
1.1 KiB
C
#include "game.h"
|
|
#include "world.h"
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <ncurses.h>
|
|
|
|
void* init_game(void) {
|
|
struct game* obj = calloc(1, sizeof(struct game));
|
|
if (!obj) return NULL;
|
|
|
|
obj->cat_x = 5;
|
|
obj->cat_y = 3;
|
|
obj->mouse_x = 20;
|
|
obj->mouse_y = 10;
|
|
obj->cat_dx = 1;
|
|
obj->cat_dy = 1;
|
|
snprintf(obj->message, sizeof(obj->message), "Лови меня, если сможешь!");
|
|
|
|
return obj;
|
|
}
|
|
|
|
int game_event(struct event* e, void* data) {
|
|
struct game* g = (struct game*)data;
|
|
|
|
if (e->type == EVENT_KEY) {
|
|
if (e->key == KEY_UP) g->cat_y--;
|
|
if (e->key == KEY_DOWN) g->cat_y++;
|
|
if (e->key == KEY_LEFT) g->cat_x--;
|
|
if (e->key == KEY_RIGHT) g->cat_x++;
|
|
}
|
|
|
|
g->mouse_x += (rand() % 3) - 1;
|
|
g->mouse_y += (rand() % 3) - 1;
|
|
|
|
if (g->cat_x == g->mouse_x && g->cat_y == g->mouse_y) {
|
|
snprintf(g->message, sizeof(g->message), "Поймал! Финита.");
|
|
}
|
|
|
|
clear_screen();
|
|
set_cell('C', g->cat_x, g->cat_y);
|
|
set_cell('M', g->mouse_x, g->mouse_y);
|
|
set_message(g->message, 2, 0);
|
|
|
|
return 0;
|
|
}
|
|
|