pvjc23/a3/game.c

115 lines
3.0 KiB
C

#include <curses.h>
#include <stdlib.h>
#include <string.h>
#include "world.h"
#include "game.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);
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->mousey[i] = 0;
state->mousex[i] = 0;
if (state->mousex[i] == 0 && state->mousey[i] == 0) {
count++;
if (count == 5) {
set_message("HAM", 12, 13);
}
}
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 < 12 || cy < 11 || cx > 38 || cy > 18){
return 0;
}
else {
state->catx_position = cx;
state->caty_position = cy;
}
//for(int i = 0; i < 5; i++){
/*int m = rand() % 6;
if (m == 0){
state->mousey[i] -= 1;
}
else if (m == 1){
state->mousey[i] += 1;
}
else if (m == 2){
state->mousex[i] -= 1;
}
else if (m == 3){
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();
for (int y = 10; y < 20; y++) {
for (int x = 10; x < 40; x++) {
if (x == 10 || y == 10 || x == 39 || y == 19) {
mvaddch(y, x, '#');
} else {
mvaddch(y, x, ' ');
}
}
}
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++){
set_cell('m',state->mousex[i],state->mousey[i]);
}
set_message( state->message,1,0);
return 0;
}