pvjc23/a3/game.c

160 lines
4.4 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();
start_color();
init_pair(1, COLOR_RED, COLOR_BLACK);
init_pair(2, COLOR_GREEN, COLOR_BLACK);
init_pair(3, COLOR_YELLOW, COLOR_BLACK);
init_pair(4, COLOR_BLUE, COLOR_BLACK);
init_pair(5, COLOR_MAGENTA, COLOR_BLACK);
init_pair(6, COLOR_CYAN, COLOR_BLACK);
init_pair(7, COLOR_WHITE, COLOR_BLACK);
char* text = "You Win!(,,,)=(^.^)=(,,,)";
int len = strlen(text);
for (int i = 0; i < len; i++) {
attron(COLOR_PAIR(i % 7 + 1));
mvprintw(15, 30 + i, "%c", text[i]);
attroff(COLOR_PAIR(i % 7 + 1));
refresh();
napms(100);
}
napms(1000);
endwin();
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;
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;
}