pvjc23/a3/game.c

188 lines
4.9 KiB
C
Raw Permalink Normal View History

2023-05-05 15:42:51 +00:00
#include <curses.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.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;
srand(time(NULL));
for (int i = 0; i < POCET_MYSI; i++) {
if(COLS > LINES){
vygenerovaneCislo = (rand() % (LINES - 4)) + 2;
}
else{
vygenerovaneCislo = 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 chytenemysi = 0;
for (int i = 0; i < POCET_MYSI; i++) {
if (state->caty_position == state->mousey[i] && state->catx_position == state->mousex[i]) {
state->mousey[i] = -1;
state->mousex[i]= -1;
}
}
for (int i = 0; i < POCET_MYSI; i++) {
if (state->mousex[i] == -1) chytenemysi++;
}
if (chytenemysi == POCET_MYSI) {
clear_screen();
set_message("Macka zjedla mysi.", (COLS / 2) - (strlen("Macka zjadla mysi.") / 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;
}
// random mouse movement
int m;
for (int i = 0; i < POCET_MYSI; i++) {
if (state->mousey[i] == -1) continue;
m = rand() % 15;
if(m==0){
if(state->mousey[i] == 2){
state->mousey[i] = 2;
}
else {
state->mousey[i] -= 1;
}
}
else if(m==1){
if(state->mousey[i] == LINES - 1){
state->mousey[i] = LINES - 10;
}
else{
state->mousey[i] += 1;
}
}
else if(m==2){
if(state->mousex[i] == 2){
state->mousex[i] = 2;
}
else{
state->mousex[i] -= 1;
}
}
else if(m==3){
if(state->mousex[i] == COLS - 1){
state->mousex[i] = COLS - 10;
}
else{
state->mousex[i] += 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
char countmessage[30];
sprintf(countmessage,"Pocet chytenych mysi: %d", chytenemysi);
clear_screen();
set_message(countmessage, COLS-25, 1);
set_color_cell('c',state->catx_position,state->caty_position,COLOR_YELLOW,COLOR_RED);
// Draw mouses
for (int i = 0; i < POCET_MYSI; i++) {
if(state->mousex[i] == -1){
continue;
}
set_cell('m', state->mousex[i], state->mousey[i]);
}
//vykresli stenu dookola
for (int i = 0; i < COLS; i++){
set_color_cell('#', i, 0, 0, COLOR_YELLOW);
set_color_cell('#', i, LINES - 1, 0, COLOR_YELLOW);
}
for (int i = 0; i < LINES; i++){
set_color_cell('#', 0, i, 0, COLOR_YELLOW);
set_color_cell('#', COLS - 1, i, 0, COLOR_YELLOW);
}
set_message( state->message,1,0);
return 0;
}