From 193a2e7e058cca739bf271ba5c82412b7380053a Mon Sep 17 00:00:00 2001 From: Anzhelika Nikolaieva Date: Sun, 23 Apr 2023 12:33:07 +0000 Subject: [PATCH] Update 'a3/game.c' --- a3/game.c | 43 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/a3/game.c b/a3/game.c index 549072b..62dbce1 100644 --- a/a3/game.c +++ b/a3/game.c @@ -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;