Update 'a3/game.c'

This commit is contained in:
Anzhelika Nikolaieva 2023-04-23 12:33:07 +00:00
parent 3387b1606c
commit 193a2e7e05

View File

@ -9,6 +9,17 @@ void* init_game(){
// Allocate memory for the state
struct game* st = calloc(1,(sizeof(struct game)));
// Initialize state
int cislo = 0;
int i;
srand(time(NULL));
for (i = 0; i < MOUSE_COUNT; i++) {
cislo = (COLS > LINES) ? ((rand() % (LINES - 4)) + 2) : ((rand() % (COLS - 4) + 2));
st->mousex[i] = cislo;
st->mousey[i] = cislo;
}
st->mousex = 5;
st->mousey = 5;
st->catx = 0;
@ -47,20 +58,36 @@ void update_game(struct game_state *game, char input){
// Is mouse caught ?
if ((state->caty_position == state->mousey) && (state->catx_position == state->mousex)){
clear_screen();
set_message("HAM",12,13);
return 0;
}
int i, ChytenyMysi = 0;
for (i = 0; i < MOUSE_COUNT; i++) {
if (state->caty_position == state->mousey[i] && state->catx_position == state->mousex[i]) {
state->mousey[i] = -1;
state->mousex[i]= -1;
}
}
for (i = 0; i < MOUSE_COUNT; i++) {
if (state->mousex[i] == -1) ChytenyMysi++;
}
if (ChytenyMysi == MOUSE_COUNT) {
clear_screen();
set_message("Tom zjedol Jerryov.", (COLS / 2) - (strlen("Tom zjedol Jerryov.") / 2), LINES / 2);
return 0;
}
else if(event->type == EVENT_TIMEOUT) {
// Move cat
//state->catx_position += state->catx;
//state->caty_position += state->caty;
int cx = state->catx_position + state->catx;
int cy = state->caty_position + state->caty;
if (cx < 0 || cy < 0 || cx > event->width || cy > event->height){
}
if (cx < 2) state->catx_position += abs(cx) + 10;
else if (cy < 2) state->caty_position += abs(cy) + 10;
else if (cx == COLS - 1) state->catx_position -= (cx - COLS + 10);
else if (cy == LINES - 1) state->caty_position -= (cy - LINES + 10);
else {
state->catx_position = cx;
state->caty_position = cy;