From 5cb51ec0e213572e20b2b34450fae5f964ae6b14 Mon Sep 17 00:00:00 2001 From: Daniel Hladek Date: Tue, 20 Apr 2021 09:00:42 +0200 Subject: [PATCH] new version of world, removed game.c --- README.md | 3 +- game.c | 88 ------------------------------------------------------- game.h | 11 ------- main.c | 16 ++++++++-- 4 files changed, 15 insertions(+), 103 deletions(-) delete mode 100644 game.c delete mode 100644 game.h diff --git a/README.md b/README.md index 0d01c7b..79a2d67 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,6 @@ Please do not change file names. - `snake.h`: you implementation should follow this header. - `snake.c`: implemement the game according to the documentation in header file in this file to pass automatic tests. - `Makefile`: rules to build the game. -- `game.c`: modify this file to change the appereance of the game and the initial state. -- `main.c`: trivial main function that runs the game +- `main.c`: trivial main function that runs the game, modify this file to change the appereance of the game and the initial state. - `world.c`: world game loop and ASCII graphics library (do not change). - `world.h`: world library interface (do not change). diff --git a/game.c b/game.c deleted file mode 100644 index 1ec7102..0000000 --- a/game.c +++ /dev/null @@ -1,88 +0,0 @@ -#include -#include -#include -#include "world.h" -#include "game.h" -#include "snake.h" -// This file contains functions for drawing the the game and changing the state - -// Start is called one in the beginning -void* init_game(){ - // Allocate memory for the state - struct state* st = calloc(1,(sizeof(struct state))); - return st; - -} - -void init_snake(struct event* world, struct state* st){ - int cy = world->height/2; - int cx = world->width/2 - 5; - for (int i = 0; i < 5; i++){ - st->snake = add_snake(st->snake,cx + i ,cy); - } - - int h = world->height; - int w = world->width; - for (int i = 0; i < 5; i++){ - st->foodx[i] = rand() % w; - st->foody[i] = rand() % h; - } -} - -// Step is called in a loop once in interval. -// It should modify the state and draw it. -int world_event(struct event* w,void* game){ - // Get state pointer - struct state* st = game; - if (w->type == EVENT_START){ - init_snake(w,st); - } - else if (w->type == EVENT_KEY){ - int key = w->key; - - if (key == KEY_RIGHT){ - st->sx = 1; - st->sy = 0; - } - else if (key == KEY_LEFT){ - st->sx = -1; - st->sy = 0; - } - else if (key == KEY_DOWN){ - st->sx = 0; - st->sy = 1; - } - else if (key == KEY_UP){ - st->sx = 0; - st->sy = -1; - } - } - else if (w->type == EVENT_ESC){ - // Non zero means finish the loop and stop the game. - return 1; - } - else if (w->type == EVENT_TIMEOUT){ - st->width = w->width; - st->height = w->height; - int r = step_state(st); - // Draw snake - struct snake* sn = st->snake; - while (sn != NULL){ - set_cell('x',sn->x,sn->y); - sn = sn->next; - } - for (int i = 0 ; i < FOOD_COUNT; i++){ - if (st->foodx[i] >= 0 && st->foody[i] >= 0){ - set_cell('*',st->foodx[i],st->foody[i]); - } - } - if (r){ - char message[] = "Koniec"; - for (int i = 0; i < 6; i++){ - set_cell(message[i],10+i,10); - } - } - } - return 0; -} - diff --git a/game.h b/game.h deleted file mode 100644 index c3eea84..0000000 --- a/game.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef _GAME_H_INCLUDE_ -#define _GAME_H_INCLUDE_ -#include "world.h" - -// Returns pointer to newly allocated state -void* init_game(); - -// Changes world according to the game state (pressed key, screen size or other event) -int world_event(struct event* event,void* state); - -#endif diff --git a/main.c b/main.c index 9e95e66..a67292d 100644 --- a/main.c +++ b/main.c @@ -11,9 +11,13 @@ void* init_game(){ // Allocate memory for the state struct state* st = calloc(1,(sizeof(struct state))); return st; - } +// The first event +// Initialize game state +// - borders +// - snake position +// - food position void init_snake(struct event* world, struct state* st){ int cy = world->height/2; int cx = world->width/2 - 5; @@ -35,11 +39,13 @@ int world_event(struct event* w,void* game){ // Get state pointer struct state* st = game; if (w->type == EVENT_START){ + // Called on beginning init_snake(w,st); } + // Called on key press else if (w->type == EVENT_KEY){ int key = w->key; - + // Modifies vector of snake movement if (key == KEY_RIGHT){ st->sx = 1; st->sy = 0; @@ -57,13 +63,17 @@ int world_event(struct event* w,void* game){ st->sy = -1; } } + // Called on esc key else if (w->type == EVENT_ESC){ // Non zero means finish the loop and stop the game. return 1; } + // Called on interval timeout else if (w->type == EVENT_TIMEOUT){ + // Copy screen size st->width = w->width; st->height = w->height; + // Change game state int r = step_state(st); // Draw snake struct snake* sn = st->snake; @@ -71,11 +81,13 @@ int world_event(struct event* w,void* game){ set_cell('x',sn->x,sn->y); sn = sn->next; } + // Draw food for (int i = 0 ; i < FOOD_COUNT; i++){ if (st->foodx[i] >= 0 && st->foody[i] >= 0){ set_cell('*',st->foodx[i],st->foody[i]); } } + // Stop the game if (r){ char message[] = "Koniec"; for (int i = 0; i < 6; i++){