#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 for(int i = 0; i < MOUSE_COUNT; i++){ st->mousex[i] = rand()%15; st->mousey[i] = rand()%15; st->mouse_state[i] = 0; } 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 // Is mouse caught ? for (int i = 0; i < MOUSE_COUNT;i++){ // Ak myška žije if (state->mouse_state[i] == 0){ // Ak mačka chytila myš if (state->mousex[i] == state->catx_position && state->mousey[i] == state->caty_position){ // Myška domyšila // Nastavíme jej stav na "zjedená". state->mouse_state[i] = 1; } } } //Are there nice left? for(int i = 0; i < MOUSE_COUNT; i++){ if(state->mouse_state[i] == 0){ break; } else{ if(i == MOUSE_COUNT-1){ clear_screen(); set_message("HAM", 12, 13); return 0; } } } 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 < 1 || cy < 0 || cx > event->width || cy > event->height){ /*state->catx_position = cx; state->caty_position = cy; if(state->catx_position < 0){ state->catx_position = 0; } else if(state->catx_position >= event->width){ state->catx_position = event->width-1; } else if(state->caty_position < 0){ state->caty_position = 0; } else if(state->caty_position >= event->height){ state->caty_position = event->height-1; }*/ } 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; for(int i = 0; i < MOUSE_COUNT; i++){ m = rand() % 4; if (m == 0 && state->mousex[i] != 0){ state->mousex[i] -= 1; } else if (m == 1 && state->mousex[i] < event->width){ state->mousex[i] += 1; } else if (m == 2 && state->mousey[i] != 0){ state->mousey[i] -= 1; } else if (m == 3 && state->mousey[i] < event->height){ state->mousey[i] += 1; } } //Je } else if (event->type == EVENT_KEY){ // Move cat according to keyboard if ( event->key == KEY_UP && state->caty_position > 0){ state->catx = 0; state->caty = -1; } else if ( event->key == KEY_DOWN && state->caty_position < event->height){ state->catx = 0; state->caty = 1; } else if ( event->key == KEY_LEFT && state->catx_position > 0){ state->catx = -1; state->caty = 0; } else if ( event->key == KEY_RIGHT && state->caty_position < event->width){ 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(int i = 0; i < MOUSE_COUNT; i++){ if(state->mouse_state[i] != 1){ set_cell('m', state->mousex[i], state->mousey[i]); } } set_message( state->message,1,0); return 0; }