152 lines
4.5 KiB
C
152 lines
4.5 KiB
C
|
#include <curses.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <time.h>
|
||
|
#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 vygenerovaneCislo = 0, i;
|
||
|
srand(time(NULL));
|
||
|
|
||
|
for (i = 0; i < MOUSE_COUNT; i++) {
|
||
|
vygenerovaneCislo = (COLS > LINES) ? ((rand() % (LINES - 4)) + 2) : ((rand() % (COLS - 4) + 2));
|
||
|
st->mousex[i] = vygenerovaneCislo;
|
||
|
st->mousey[i] = vygenerovaneCislo;
|
||
|
}
|
||
|
|
||
|
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 ?
|
||
|
int i, pocetChytenychMysi = 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) pocetChytenychMysi++;
|
||
|
}
|
||
|
|
||
|
if (pocetChytenychMysi == MOUSE_COUNT) {
|
||
|
clear_screen();
|
||
|
set_message("Tom zjedol Jerryov.", (COLS / 2) - (strlen("Tom zjedol Jerryov.") / 2), LINES / 2);
|
||
|
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 < 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;
|
||
|
|
||
|
for (i = 0; i < MOUSE_COUNT; i++) {
|
||
|
if (state->mousey[i] == -1) continue;
|
||
|
m = rand() % 4;
|
||
|
switch (m) {
|
||
|
case 0: if (state->mousey[i] == 2) state->mousey[i] = 2;
|
||
|
else state->mousey[i] -= 1;
|
||
|
break;
|
||
|
|
||
|
case 1: if (state->mousey[i] == LINES - 1) state->mousey[i] = LINES - 10;
|
||
|
else state->mousey[i] += 1;
|
||
|
break;
|
||
|
|
||
|
case 2: if (state->mousex[i] == 2) state->mousex[i] = 2;
|
||
|
else state->mousex[i] -= 1;
|
||
|
break;
|
||
|
|
||
|
case 3: if (state->mousex[i] == COLS - 1) state->mousex[i] = COLS - 10;
|
||
|
else state->mousex[i] += 1;
|
||
|
break;
|
||
|
|
||
|
default: break;
|
||
|
}
|
||
|
}
|
||
|
// 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 mouses
|
||
|
for (i = 0; i < MOUSE_COUNT; i++) {
|
||
|
if (state->mousex[i] == -1) continue;
|
||
|
set_cell('m', state->mousex[i], state->mousey[i]);
|
||
|
}
|
||
|
|
||
|
|
||
|
//vykresli stenu dookola
|
||
|
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;
|
||
|
}
|