refresh
This commit is contained in:
parent
591bc55c3d
commit
8e44e30c67
57
a4/game.c
57
a4/game.c
@ -1,45 +1,46 @@
|
||||
#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;
|
||||
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* event, void* g_state) {
|
||||
struct game* game = (struct game*)g_state;
|
||||
if (!game) return 0;
|
||||
int game_event(struct event* e, void* data) {
|
||||
struct game* g = (struct game*)data;
|
||||
|
||||
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;
|
||||
}
|
||||
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++;
|
||||
}
|
||||
|
||||
game->mouse_x += (rand() % 3) - 1;
|
||||
game->mouse_y += (rand() % 3) - 1;
|
||||
g->mouse_x += (rand() % 3) - 1;
|
||||
g->mouse_y += (rand() % 3) - 1;
|
||||
|
||||
if (game->cat_x == game->mouse_x && game->cat_y == game->mouse_y) {
|
||||
snprintf(game->message, sizeof(game->message), "Мышь поймана! Ура!");
|
||||
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;
|
||||
}
|
||||
|
||||
|
15
a4/main.c
15
a4/main.c
@ -1,19 +1,8 @@
|
||||
#include "game.h"
|
||||
#include "world.h"
|
||||
#include <stdlib.h>
|
||||
#include "world.h"
|
||||
#include <ncurses.h>
|
||||
|
||||
int main(void) {
|
||||
void* game_state = init_game();
|
||||
if (!game_state) {
|
||||
fprintf(stderr, "Не удалось инициализировать игру!\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
world_run(game_state, game_event);
|
||||
|
||||
free(game_state);
|
||||
|
||||
return 0;
|
||||
return start_world(init_game, game_event, free);
|
||||
}
|
||||
|
||||
|
113
a4/world.h
113
a4/world.h
@ -1,113 +1,36 @@
|
||||
#ifndef _WORLD_H_
|
||||
#define _WORLD_H_
|
||||
|
||||
#include <curses.h>
|
||||
|
||||
/**
|
||||
* World represented as a rectangular matrix of colorful characters.
|
||||
*
|
||||
* Point [0,0] is displayed the upper left corner of the screen.
|
||||
*
|
||||
*/
|
||||
|
||||
enum event_type {
|
||||
EVENT_START,
|
||||
EVENT_TIMEOUT,
|
||||
EVENT_KEY,
|
||||
EVENT_MOUSE,
|
||||
EVENT_RESIZE,
|
||||
EVENT_ESC,
|
||||
EVENT_END,
|
||||
};
|
||||
|
||||
struct event {
|
||||
/**
|
||||
* Last width of the screen.
|
||||
*/
|
||||
int width;
|
||||
/**
|
||||
* Last height of the screen.
|
||||
*/
|
||||
int height;
|
||||
/**
|
||||
* Last pressed key or Curses event.
|
||||
*
|
||||
* Special event values:
|
||||
* ERR if timeout,
|
||||
* KEY_RESIZE if screen resize
|
||||
* KEY_EVENT, other event,
|
||||
* KEY_MOUSE, mouse clicked
|
||||
*
|
||||
* Key values:
|
||||
*
|
||||
* ' ' Space
|
||||
* KEY_DOWN Arrow down
|
||||
* KEY_UP Arrow up
|
||||
* KEY_LEFT Arrow left
|
||||
* KEY_RIGHT Arrow right
|
||||
* KEY_A1 Upper left of keypad
|
||||
* KEY_A3 Upper right of keypad
|
||||
* KEY_B2 Center of keypad
|
||||
* KEY_C1 Lower left of keypad
|
||||
* KEY_C3 Lower right of keypad
|
||||
*
|
||||
* KEY_ENTER
|
||||
* KEY_BACKSPACE
|
||||
*/
|
||||
int type;
|
||||
int key;
|
||||
int alt_key;
|
||||
enum event_type type;
|
||||
int height;
|
||||
int width;
|
||||
int time_ms;
|
||||
int mouse_x;
|
||||
int mouse_y;
|
||||
int mouse_left;
|
||||
int mouse_right;
|
||||
int mouse_middle;
|
||||
long int time_ms;
|
||||
int alt_key;
|
||||
};
|
||||
|
||||
/**
|
||||
* Sets cell to a state.
|
||||
* @param event
|
||||
* @param x coordinate of cell
|
||||
* @param y coordinate of cell
|
||||
* @param new state of the cell
|
||||
*/
|
||||
void set_cell(int character,int x,int y);
|
||||
|
||||
/**
|
||||
* COLOR_BLACK 0
|
||||
* COLOR_RED 1
|
||||
* COLOR_GREEN 2
|
||||
* COLOR_YELLOW 3
|
||||
* COLOR_BLUE 4
|
||||
* COLOR_MAGENTA 5
|
||||
* COLOR_CYAN 6
|
||||
* COLOR_WHITE 7
|
||||
*/
|
||||
#define EVENT_KEY 1
|
||||
#define EVENT_MOUSE 2
|
||||
#define EVENT_RESIZE 3
|
||||
#define EVENT_ESC 4
|
||||
#define EVENT_TIMEOUT 5
|
||||
#define EVENT_START 6
|
||||
|
||||
#define COLOR_COUNT 8
|
||||
|
||||
void set_color_cell(int character,int x,int y,short front_color,short back_color);
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param event
|
||||
* @param number of commandline arguments
|
||||
* @param init_world
|
||||
* @param destroy_world
|
||||
*
|
||||
* void init_world(struct event* w);
|
||||
* Initializes user state.
|
||||
* Free user state.
|
||||
* @param event
|
||||
*/
|
||||
|
||||
int start_world(void* (*init_game)(),int (*world_event)(struct event* event,void* game),void (*destroy_game)(void* game));
|
||||
|
||||
void game_speed(int value);
|
||||
|
||||
void set_message(const char* message,int x,int y);
|
||||
void clear_screen();
|
||||
void set_cell(int c, int x, int y);
|
||||
void set_message(const char* msg, int x, int y);
|
||||
void set_color_cell(int c, int x, int y, short f, short b);
|
||||
void game_speed(int v);
|
||||
void abort_game(const char* m);
|
||||
int world_run(void* (*init_game)(), int (*world_event)(struct event*, void*)); // ← ВОТ ЭТА СТРОЧКА
|
||||
|
||||
#endif
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user