Update 'a3/game.c'
This commit is contained in:
parent
751b9c2994
commit
eba95ee103
161
a3/game.c
161
a3/game.c
@ -4,122 +4,66 @@
|
||||
#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
|
||||
st->mousex = 5;
|
||||
st->mousey = 5;
|
||||
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;
|
||||
void init_game(struct game *game)
|
||||
{
|
||||
// Initialize random number generator
|
||||
srand(time(NULL));
|
||||
|
||||
// Initialize cat position
|
||||
game->catx = rand() % 10;
|
||||
game->caty = rand() % 10;
|
||||
|
||||
// Initialize mouse positions
|
||||
for (int i = 0; i < MOUSE_COUNT; i++) {
|
||||
game->mousex[i] = rand() % 10;
|
||||
game->mousey[i] = rand() % 10;
|
||||
game->mouse_state[i] = 0; // Mouse is alive
|
||||
}
|
||||
}
|
||||
|
||||
// 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.
|
||||
|
||||
switch (event->key) {
|
||||
case KEY_LEFT:
|
||||
game->catx--;
|
||||
break;
|
||||
case KEY_RIGHT:
|
||||
game->catx++;
|
||||
break;
|
||||
case KEY_UP:
|
||||
game->caty--;
|
||||
break;
|
||||
case KEY_DOWN:
|
||||
game->caty++;
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
void update_game(struct game *game, int key)
|
||||
{
|
||||
// Move cat according to key
|
||||
if (key == 'w') {
|
||||
game->caty--;
|
||||
} else if (key == 's') {
|
||||
game->caty++;
|
||||
} else if (key == 'a') {
|
||||
game->catx--;
|
||||
} else if (key == 'd') {
|
||||
game->catx++;
|
||||
}
|
||||
// Read game variable and update the eventstate
|
||||
|
||||
// Is mouse caught ?
|
||||
if ((state->caty_position == state->mousey) && (state->catx_position == state->mousex)){
|
||||
clear_screen();
|
||||
set_message("HAM",12,13);
|
||||
return 0;
|
||||
// Check if any mouse is caught by the cat
|
||||
for (int i = 0; i < MOUSE_COUNT; i++) {
|
||||
if (game->mouse_state[i] == 0 && game->mousex[i] == game->catx && game->mousey[i] == game->caty) {
|
||||
game->mouse_state[i] = 1; // Mouse is eaten
|
||||
}
|
||||
}
|
||||
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){
|
||||
|
||||
// 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]++;
|
||||
}
|
||||
}
|
||||
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
|
||||
//
|
||||
|
||||
|
||||
void draw_game(struct game *game){
|
||||
void draw_game(struct game *game)
|
||||
{
|
||||
// Draw cat
|
||||
printf("Cat position: (%d, %d)\n", game->catx, game->caty);
|
||||
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);
|
||||
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
|
||||
// Draw alive mice
|
||||
int alive_count = 0;
|
||||
for (int i = 0; i < MOUSE_COUNT; i++) {
|
||||
if (game->mouse_state[i] == 0) { // Mouse is alive
|
||||
@ -133,10 +77,5 @@ int game_event(struct event* event,void* game){
|
||||
} else {
|
||||
printf("Number of alive mice: %d\n", alive_count);
|
||||
}
|
||||
|
||||
|
||||
//set_cell('m',state->mousex,state->mousey);
|
||||
//set_message( state->message,1,0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user