Update 'a3/game.c'
This commit is contained in:
parent
eba95ee103
commit
703ce06f9b
157
a3/game.c
157
a3/game.c
@ -4,78 +4,101 @@
|
|||||||
#include "world.h"
|
#include "world.h"
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
|
||||||
void init_game(struct game *game)
|
void* init_game(){
|
||||||
{
|
// Allocate memory for the state
|
||||||
// Initialize random number generator
|
struct game* st = calloc(1,(sizeof(struct game)));
|
||||||
srand(time(NULL));
|
// Initialize state
|
||||||
|
st->mousex = 5;
|
||||||
// Initialize cat position
|
st->mousey = 5;
|
||||||
game->catx = rand() % 10;
|
st->catx = 0;
|
||||||
game->caty = rand() % 10;
|
st->caty = 0;
|
||||||
|
st->catx_position = 15;
|
||||||
// Initialize mouse positions
|
st->caty_position = 15;
|
||||||
for (int i = 0; i < MOUSE_COUNT; i++) {
|
// Store pointer to the state to the world variable
|
||||||
game->mousex[i] = rand() % 10;
|
return st;
|
||||||
game->mousey[i] = rand() % 10;
|
|
||||||
game->mouse_state[i] = 0; // Mouse is alive
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void update_game(struct game *game, int key)
|
// Step is called in a loop once in interval.
|
||||||
{
|
// It should modify the state and draw it.
|
||||||
// Move cat according to key
|
int game_event(struct event* event,void* game){
|
||||||
if (key == 'w') {
|
// Get state pointer
|
||||||
game->caty--;
|
struct game* state = game;
|
||||||
} else if (key == 's') {
|
char msg[200];
|
||||||
game->caty++;
|
sprintf(msg,"%d",event->type);
|
||||||
} else if (key == 'a') {
|
set_message(msg,10,0);
|
||||||
game->catx--;
|
if ( event->type == EVENT_ESC){
|
||||||
} else if (key == 'd') {
|
// Non zero means finish the loop and stop the game.
|
||||||
game->catx++;
|
return 1;
|
||||||
}
|
}
|
||||||
|
// Read game variable and update the eventstate
|
||||||
|
|
||||||
// Check if any mouse is caught by the cat
|
// Is mouse caught ?
|
||||||
for (int i = 0; i < MOUSE_COUNT; i++) {
|
if ((state->caty_position == state->mousey) && (state->catx_position == state->mousex)){
|
||||||
if (game->mouse_state[i] == 0 && game->mousex[i] == game->catx && game->mousey[i] == game->caty) {
|
clear_screen();
|
||||||
game->mouse_state[i] = 1; // Mouse is eaten
|
set_message("HAM",12,13);
|
||||||
|
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 < 0 || cy < 0 || cx > event->width || cy > event->height){
|
||||||
|
|
||||||
|
}
|
||||||
|
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
|
||||||
// Move alive mice randomly
|
//
|
||||||
for (int i = 0; i < MOUSE_COUNT; i++) {
|
|
||||||
if (game->mouse_state[i] == 0) { // Mouse is alive
|
|
||||||
int m = rand() % 4;
|
|
||||||
if (m == 0) {
|
|
||||||
game->mousey[i]--;
|
|
||||||
} else if (m == 1) {
|
|
||||||
game->mousey[i]++;
|
|
||||||
} else if (m == 2) {
|
|
||||||
game->mousex[i]--;
|
|
||||||
} else if (m == 3) {
|
|
||||||
game->mousex[i]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void draw_game(struct game *game)
|
|
||||||
{
|
|
||||||
// Draw cat
|
// Draw cat
|
||||||
printf("Cat position: (%d, %d)\n", game->catx, game->caty);
|
clear_screen();
|
||||||
|
set_color_cell('c',state->catx_position,state->caty_position,COLOR_YELLOW,COLOR_RED);
|
||||||
// Draw alive mice
|
set_color_cell('-',state->catx_position-1,state->caty_position,COLOR_YELLOW,COLOR_GREEN);
|
||||||
int alive_count = 0;
|
//set_cell('c',state->catx_position,state->caty_position);
|
||||||
for (int i = 0; i < MOUSE_COUNT; i++) {
|
// Draw mouse
|
||||||
if (game->mouse_state[i] == 0) { // Mouse is alive
|
set_cell('m',state->mousex,state->mousey);
|
||||||
printf("Mouse %d position: (%d, %d)\n", i, game->mousex[i], game->mousey[i]);
|
set_message( state->message,1,0);
|
||||||
alive_count++;
|
return 0;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (alive_count == 0) {
|
|
||||||
printf("All mice are eaten! Game over.\n");
|
|
||||||
} else {
|
|
||||||
printf("Number of alive mice: %d\n", alive_count);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user