140 lines
4.2 KiB
C
140 lines
4.2 KiB
C
#include <curses.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "world.h"
|
|
#include "game.h"
|
|
#include <time.h>
|
|
void* init_game(){
|
|
struct game* st = calloc(1,(sizeof(struct game)));
|
|
for(int i = 0; i < 5; i++){
|
|
st->mousex[i] = rand() % 5 + 19;
|
|
st->mousey[i] = rand() % 5 + 13;
|
|
}
|
|
st->catx = 0;
|
|
st->caty = 0;
|
|
st->catx_position = 15;
|
|
st->caty_position = 15;
|
|
|
|
return st;
|
|
}
|
|
int game_event(struct event* event,void* game){
|
|
struct game* state = game;
|
|
char msg[200];
|
|
sprintf(msg,"%d",event->type);
|
|
set_message(msg,10,0);
|
|
int mouse_count = 0;
|
|
if ( event->type == EVENT_ESC){
|
|
return 1;
|
|
}
|
|
int count = 0;
|
|
for (int i = 0; i < 5;i++){
|
|
if ((state->caty_position == state->mousey[i]) && (state->catx_position == state->mousex[i])) {
|
|
count = 1;
|
|
state->mouse_state[i] = 1;
|
|
state->mousex[i] = 0;
|
|
state->mousey[i] = 0;
|
|
}
|
|
if(state->mouse_state[i] == 1){
|
|
mouse_count++;
|
|
if(mouse_count == 5){
|
|
clear_screen();
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(count == 0){
|
|
count = 2;
|
|
if(event->type == EVENT_TIMEOUT) {
|
|
int cx = state->catx_position + state->catx;
|
|
int cy = state->caty_position + state->caty;
|
|
if (cx < 11 || cy < 11 || cx > 38 || cy > 18){
|
|
|
|
}
|
|
else {
|
|
state->catx_position = cx;
|
|
state->caty_position = cy;
|
|
}
|
|
for (int i = 0; i < 5; i++) {
|
|
int m = rand() % 6;
|
|
if(state->mouse_state[i] == 0){
|
|
if (m == 0) {
|
|
if (state->mousey[i] == 12) {
|
|
state->mousey[i] = 13;
|
|
}
|
|
state->mousey[i] -= 1;
|
|
} else if (m == 1) {
|
|
if (state->mousey[i] == 18) {
|
|
state->mousey[i] = 17;
|
|
}
|
|
state->mousey[i] += 1;
|
|
} else if (m == 2) {
|
|
if (state->mousex[i] == 12) {
|
|
state->mousex[i] = 13;
|
|
}
|
|
state->mousex[i] -= 1;
|
|
} else if (m == 3) {
|
|
if (state->mousex[i] == 38) {
|
|
state->mousex[i] = 37;
|
|
}
|
|
state->mousex[i] += 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if(count == 2){
|
|
if (event->type == EVENT_KEY){
|
|
if ( event->key == KEY_UP){
|
|
state->catx = 0;
|
|
state->caty = -1;
|
|
}
|
|
else if ( event->key == KEY_DOWN){
|
|
state->catx = 0;
|
|
state->caty = 1;
|
|
}
|
|
else if ( event->key == KEY_LEFT){
|
|
state->catx = -1;
|
|
state->caty = 0;
|
|
}
|
|
else if ( event->key == KEY_RIGHT){
|
|
state->catx = +1;
|
|
state->caty = 0;
|
|
}
|
|
}
|
|
}
|
|
count = 0;
|
|
// Draw world state
|
|
clear_screen();
|
|
initscr();
|
|
start_color();
|
|
init_pair(1, COLOR_BLUE, COLOR_BLUE); // цветовая пара для стен - белый на голубом
|
|
init_pair(2, COLOR_BLACK, COLOR_BLACK); // цветовая пара для мышей - черный на белом
|
|
|
|
for (int y = 10; y < 20; y++) {
|
|
for (int x = 10; x < 40; x++) {
|
|
if (x == 10 || y == 10 || x == 39 || y == 19) {
|
|
attron(COLOR_PAIR(1)); // включаем цветовую пару для стен
|
|
mvaddch(y, x, '#');
|
|
attroff(COLOR_PAIR(1)); // выключаем цветовую пару для стен
|
|
} else {
|
|
attron(COLOR_PAIR(2)); // включаем цветовую пару для мышей
|
|
mvaddch(y, x, ' ');
|
|
attroff(COLOR_PAIR(2)); // выключаем цветовую пару для мышей
|
|
}
|
|
}
|
|
}
|
|
|
|
refresh();
|
|
set_color_cell('c',state->catx_position,state->caty_position,COLOR_YELLOW,COLOR_RED);
|
|
//set_color_cell('-',state->catx_position-1,state->caty_position,COLOR_YELLOW,COLOR_GREEN);
|
|
|
|
for(int i = 0; i < 5; i++){
|
|
if(state->mouse_state[i] == 0){
|
|
set_color_cell('m',state->mousex[i],state->mousey[i],COLOR_BLACK,COLOR_WHITE);
|
|
}
|
|
}
|
|
set_message( state->message,1,0);
|
|
return 0;
|
|
}
|