pvjc25/a4/game.c
2025-05-02 19:35:19 +02:00

46 lines
1.3 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "game.h"
#include "world.h"
#include <stdlib.h>
#include "world.h"
#include <stdio.h>
#include <ncurses.h>
void* init_game(void) {
struct game* state = (struct game*)calloc(1, sizeof(struct game));
if (!state) {
return NULL;
}
state->cat_x = 5;
state->cat_y = 5;
state->cat_dx = 1;
state->cat_dy = 1;
state->mouse_x = 15;
state->mouse_y = 10;
snprintf(state->message, sizeof(state->message), "Давай, попробуй поймать меня!");
return state;
}
int game_event(struct event* event, void* g_state) {
struct game* game = (struct game*)g_state;
if (!game) return 0;
if (event->type == KEY_SREDO) {
switch (event->key) {
case KEY_UP: game->cat_y -= game->cat_dy; break;
case KEY_DOWN: game->cat_y += game->cat_dy; break;
case KEY_LEFT: game->cat_x -= game->cat_dx; break;
case KEY_RIGHT: game->cat_x += game->cat_dx; break;
default: break;
}
}
game->mouse_x += (rand() % 3) - 1;
game->mouse_y += (rand() % 3) - 1;
if (game->cat_x == game->mouse_x && game->cat_y == game->mouse_y) {
snprintf(game->message, sizeof(game->message), "Мышь поймана! Ура!");
}
return 0;
}