Update 'a3/game.c'

This commit is contained in:
Anzhelika Nikolaieva 2023-05-02 12:29:49 +00:00
parent db097301ee
commit 149f9a1f09

View File

@ -8,17 +8,8 @@
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;
}
// Initialize stat
st->mousex = 5;
st->mousey = 5;
@ -46,36 +37,13 @@ int game_event(struct event* event,void* game){
void update_game(struct game_state *game, char input){
// update cat position based on input
switch (input) {
case 'w': game->caty -= 1; break;
case 's': game->caty += 1; break;
case 'a': game->catx -= 1; break;
case 'd': game->catx += 1; break;
}
// Is mouse caught ?
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
@ -84,14 +52,7 @@ void update_game(struct game_state *game, char input){
int cx = state->catx_position + state->catx;
int cy = state->caty_position + state->caty;
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;
}
//state->catx_position += state->catx;
//state->caty_position += state->caty;