#include #include #include #include "world.h" #include "game.h" // Start is called one in the beginning 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; st->caty = 0; st->catx_position = 15; st->caty_position = 15; // Store pointer to the state to the world variable return st; } // Step is called in a loop once in interval. // It should modify the state and draw it. int game_event(struct event* event,void* game){ // Get state pointer struct game* state = game; char msg[200]; sprintf(msg,"%d",event->type); set_message(msg,10,0); if ( event->type == EVENT_ESC){ // Non zero means finish the loop and stop the game. return 1; } // Read game variable and update the eventstate 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 //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 < 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; //state->caty_position += state->caty; // random mouse movement int m = rand() % 6; if (m == 0){ state->mousey -= 1; } else if (m == 1){ state->mousey += 1; } else if (m == 2){ state->mousex -= 1; } else if (m == 3){ state->mousex += 1; } // Je myska mimo plochy } else if (event->type == EVENT_KEY){ // Move cat according to keyboard 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; } } // Draw world state // // Draw cat clear_screen(); 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); //set_cell('c',state->catx_position,state->caty_position); // Draw mouse for (i = 0; i < MOUSE_COUNT; i++) { if (state->mousex[i] == -1) continue; set_cell('m',state->mousex,state->mousey); } for (i = 0; i < COLS; i++) set_color_cell('-', i, 0, 0, (rand() % 7) + 1); for (i = 0; i < LINES; i++) set_color_cell('-', 0, i, 0, (rand() % 7) + 1); for (i = 0; i < COLS; i++) set_color_cell('-', i, LINES - 1, 0, (rand() % 7) + 1); for (i = 0; i < LINES; i++) set_color_cell('-', COLS - 1, i, 0, (rand() % 7) + 1); set_message( state->message,1,0); return 0; }