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 "game.h"
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "world.h"
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
|
||||||
void* init_game(void) {
|
void* init_game(void) {
|
||||||
struct game* state = (struct game*)calloc(1, sizeof(struct game));
|
struct game* obj = calloc(1, sizeof(struct game));
|
||||||
if (!state) {
|
if (!obj) return NULL;
|
||||||
return NULL;
|
|
||||||
}
|
obj->cat_x = 5;
|
||||||
state->cat_x = 5;
|
obj->cat_y = 3;
|
||||||
state->cat_y = 5;
|
obj->mouse_x = 20;
|
||||||
state->cat_dx = 1;
|
obj->mouse_y = 10;
|
||||||
state->cat_dy = 1;
|
obj->cat_dx = 1;
|
||||||
state->mouse_x = 15;
|
obj->cat_dy = 1;
|
||||||
state->mouse_y = 10;
|
snprintf(obj->message, sizeof(obj->message), "Лови меня, если сможешь!");
|
||||||
snprintf(state->message, sizeof(state->message), "Давай, попробуй поймать меня!");
|
|
||||||
return state;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
int game_event(struct event* event, void* g_state) {
|
int game_event(struct event* e, void* data) {
|
||||||
struct game* game = (struct game*)g_state;
|
struct game* g = (struct game*)data;
|
||||||
if (!game) return 0;
|
|
||||||
|
|
||||||
if (event->type == KEY_SREDO) {
|
if (e->type == EVENT_KEY) {
|
||||||
switch (event->key) {
|
if (e->key == KEY_UP) g->cat_y--;
|
||||||
case KEY_UP: game->cat_y -= game->cat_dy; break;
|
if (e->key == KEY_DOWN) g->cat_y++;
|
||||||
case KEY_DOWN: game->cat_y += game->cat_dy; break;
|
if (e->key == KEY_LEFT) g->cat_x--;
|
||||||
case KEY_LEFT: game->cat_x -= game->cat_dx; break;
|
if (e->key == KEY_RIGHT) g->cat_x++;
|
||||||
case KEY_RIGHT: game->cat_x += game->cat_dx; break;
|
|
||||||
default: break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
game->mouse_x += (rand() % 3) - 1;
|
g->mouse_x += (rand() % 3) - 1;
|
||||||
game->mouse_y += (rand() % 3) - 1;
|
g->mouse_y += (rand() % 3) - 1;
|
||||||
|
|
||||||
if (game->cat_x == game->mouse_x && game->cat_y == game->mouse_y) {
|
if (g->cat_x == g->mouse_x && g->cat_y == g->mouse_y) {
|
||||||
snprintf(game->message, sizeof(game->message), "Мышь поймана! Ура!");
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
15
a4/main.c
15
a4/main.c
@ -1,19 +1,8 @@
|
|||||||
#include "game.h"
|
#include "game.h"
|
||||||
#include "world.h"
|
#include "world.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "world.h"
|
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
void* game_state = init_game();
|
return start_world(init_game, game_event, free);
|
||||||
if (!game_state) {
|
|
||||||
fprintf(stderr, "Не удалось инициализировать игру!\n");
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
world_run(game_state, game_event);
|
|
||||||
|
|
||||||
free(game_state);
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
113
a4/world.h
113
a4/world.h
@ -1,113 +1,36 @@
|
|||||||
#ifndef _WORLD_H_
|
#ifndef _WORLD_H_
|
||||||
#define _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 {
|
struct event {
|
||||||
/**
|
int type;
|
||||||
* 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 key;
|
int key;
|
||||||
int alt_key;
|
int height;
|
||||||
enum event_type type;
|
int width;
|
||||||
|
int time_ms;
|
||||||
int mouse_x;
|
int mouse_x;
|
||||||
int mouse_y;
|
int mouse_y;
|
||||||
int mouse_left;
|
int mouse_left;
|
||||||
int mouse_right;
|
int mouse_right;
|
||||||
int mouse_middle;
|
int mouse_middle;
|
||||||
long int time_ms;
|
int alt_key;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
#define EVENT_KEY 1
|
||||||
* Sets cell to a state.
|
#define EVENT_MOUSE 2
|
||||||
* @param event
|
#define EVENT_RESIZE 3
|
||||||
* @param x coordinate of cell
|
#define EVENT_ESC 4
|
||||||
* @param y coordinate of cell
|
#define EVENT_TIMEOUT 5
|
||||||
* @param new state of the cell
|
#define EVENT_START 6
|
||||||
*/
|
|
||||||
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 COLOR_COUNT 8
|
#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 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
|
#endif
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user