173 lines
4.9 KiB
C
173 lines
4.9 KiB
C
#include <curses.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "world.h"
|
|
#include "game.h"
|
|
|
|
// Start is called once in the beginning
|
|
void* init_game(){
|
|
// Allocate memory for the state
|
|
struct game* st = calloc(1,(sizeof(struct game)));
|
|
// Initialize state
|
|
st->catx = 0;
|
|
st->caty = 0;
|
|
st->catx_position = 15;
|
|
st->caty_position = 15;
|
|
st->caught = 0;
|
|
|
|
// Initialize mice at random positions
|
|
for (int i = 0; i < MOUSE_COUNT; i++){
|
|
st->mousex[i] = rand() % 20 + 30;
|
|
st->mousey[i] = rand() % 20 + 5;
|
|
st->mouse_eaten[i] = 0;
|
|
}
|
|
|
|
// Initialize obstacles at random positions
|
|
for (int i = 0; i < 10; i++){
|
|
st->obstaclex[i] = rand() % 40 + 5;
|
|
st->obstacley[i] = rand() % 15 + 5;
|
|
}
|
|
|
|
// 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;
|
|
}
|
|
|
|
// Is all mice caught?
|
|
if (state->caught == MOUSE_COUNT){
|
|
clear_screen();
|
|
set_message("HAM HAM HAM! Tomas chytil vsetky mysky!",12,13);
|
|
if (event->key == 'q'){
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
else if(event->type == EVENT_TIMEOUT) {
|
|
// Move cat
|
|
int cx = state->catx_position + state->catx;
|
|
int cy = state->caty_position + state->caty;
|
|
|
|
// Check obstacle collision for cat
|
|
int hit = 0;
|
|
for (int i = 0; i < 10; i++){
|
|
if (cx == state->obstaclex[i] && cy == state->obstacley[i]){
|
|
hit = 1;
|
|
}
|
|
}
|
|
if (cx <= 0 || cy <= 0 || cx >= event->width - 1 || cy >= event->height - 1 || hit){
|
|
}
|
|
else {
|
|
state->catx_position = cx;
|
|
state->caty_position = cy;
|
|
}
|
|
|
|
// random mouse movement
|
|
for (int i = 0; i < MOUSE_COUNT; i++){
|
|
if (state->mouse_eaten[i]) continue;
|
|
|
|
int m = rand() % 6;
|
|
int nx = state->mousex[i];
|
|
int ny = state->mousey[i];
|
|
if (m == 0){
|
|
ny -= 1;
|
|
}
|
|
else if (m == 1){
|
|
ny += 1;
|
|
}
|
|
else if (m == 2){
|
|
nx -= 1;
|
|
}
|
|
else if (m == 3){
|
|
nx += 1;
|
|
}
|
|
|
|
// Check obstacle collision for mouse
|
|
int blocked = 0;
|
|
for (int j = 0; j < 10; j++){
|
|
if (nx == state->obstaclex[j] && ny == state->obstacley[j]){
|
|
blocked = 1;
|
|
}
|
|
}
|
|
// Je myska mimo plochy
|
|
if (nx > 0 && ny > 0 && nx < event->width - 1 && ny < event->height - 1 && !blocked){
|
|
state->mousex[i] = nx;
|
|
state->mousey[i] = ny;
|
|
}
|
|
|
|
// Je myska chytena?
|
|
if (state->mousex[i] == state->catx_position &&
|
|
state->mousey[i] == state->caty_position){
|
|
state->mouse_eaten[i] = 1;
|
|
state->caught++;
|
|
}
|
|
}
|
|
}
|
|
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
|
|
clear_screen();
|
|
|
|
// Draw walls
|
|
for (int x = 0; x < event->width; x++){
|
|
set_color_cell('#', x, 0, COLOR_WHITE, COLOR_BLACK);
|
|
set_color_cell('#', x, event->height - 1, COLOR_WHITE, COLOR_BLACK);
|
|
}
|
|
for (int y = 0; y < event->height; y++){
|
|
set_color_cell('#', 0, y, COLOR_WHITE, COLOR_BLACK);
|
|
set_color_cell('#', event->width - 1, y, COLOR_WHITE, COLOR_BLACK);
|
|
}
|
|
|
|
// Draw obstacles
|
|
for (int i = 0; i < 10; i++){
|
|
set_color_cell('*', state->obstaclex[i], state->obstacley[i], COLOR_CYAN, COLOR_BLACK);
|
|
}
|
|
|
|
// Draw cat
|
|
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);
|
|
|
|
// Draw mice
|
|
for (int i = 0; i < MOUSE_COUNT; i++){
|
|
if (!state->mouse_eaten[i]){
|
|
set_cell('m',state->mousex[i],state->mousey[i]);
|
|
}
|
|
}
|
|
|
|
// Draw counter
|
|
sprintf(state->message,"Chytene: %d/%d", state->caught, MOUSE_COUNT);
|
|
set_message(state->message,1,0);
|
|
|
|
return 0;
|
|
}
|