Implement game.c, food.c and map.c
- Implemented the functions in the above files - game and map are partially tested - food is extensively tested using the created test-function - Also added DELAY(ms) macro to common.c
This commit is contained in:
@@ -10,15 +10,34 @@
|
||||
//conditional logging when DEBUG_OUTPUT_ENABLED is defined in config.h
|
||||
//example: LOGD("game: %d", count)
|
||||
#ifdef DEBUG_OUTPUT_ENABLED
|
||||
#define LOGD(format, ...) printf(format, ##__VA_ARGS__)
|
||||
#define LOGD(format, ...) printf("[D] " format, ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOGD(format, ...) do {} while (0)
|
||||
#endif
|
||||
|
||||
//conditional logging when INFO_OUTPUT_ENABLED is defined in config.h
|
||||
//example: LOGD("game: %d", count)
|
||||
//example: LOGI("game: %d", count)
|
||||
#ifdef INFO_OUTPUT_ENABLED
|
||||
#define LOGI(format, ...) printf(format, ##__VA_ARGS__)
|
||||
#define LOGI(format, ...) printf("[I] " format, ##__VA_ARGS__)
|
||||
#else
|
||||
#define LOGI(format, ...) do {} while (0)
|
||||
#endif
|
||||
|
||||
|
||||
//===========================
|
||||
//========== DELAY ==========
|
||||
//===========================
|
||||
//macro for DELAY(ms) function that works on Windows and Linux
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#define DELAY(ms) Sleep(ms)
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#define DELAY(ms) usleep((ms) * 1000)
|
||||
#endif
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
// global configuration macros
|
||||
#define MAX_MAP_SIZE 10
|
||||
#define MAX_MAP_SIZE 20
|
||||
#define MAX_MAP_FIELDS (MAX_MAP_SIZE*MAX_MAP_SIZE)
|
||||
|
||||
// logging settings
|
||||
#define DEBUG_OUTPUT_ENABLED
|
||||
#define INFO_OUTPUT_ENABLED
|
||||
|
||||
|
||||
|
||||
// struct for storing game configuration
|
||||
typedef struct config_t
|
||||
{
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
//function that spawns food respecting the following rules:
|
||||
// - not at Collision, Snake-tail, Snake-head, portal-in, portal-out position (objects)
|
||||
// - not closer to an object than minDist (if possible)
|
||||
// - not further from an object than maxDist (if possible)
|
||||
// maxDist and minDist are currently defined in food.c
|
||||
void placeFood();
|
||||
// platziert zufällig (mit bestimmtem Algorithmus) Fressen auf dem Spielfeld
|
||||
// darf nicht auf der Schlange oder auf Wänden sein
|
||||
|
||||
|
||||
//function that returns true when snake head is at current food position
|
||||
bool checkEaten();
|
||||
// Überprüft, ob Snake gefressen hat -> true wenn gefressen
|
||||
// Vergleich mit gameData_t foodX, foodY
|
||||
|
||||
|
||||
// indefinitely spawn food and print the map to console until the program is killed
|
||||
// for testing and adjusting the food placement algorithm
|
||||
void startFoodPlacementTest();
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "config.h"
|
||||
#include "map.h"
|
||||
|
||||
|
||||
// Enum that defines the current game state
|
||||
typedef enum gameState_t
|
||||
{
|
||||
@@ -18,32 +19,32 @@ typedef enum gameState_t
|
||||
// Struct that stores all data of the running game (all game-related functions access it globally)
|
||||
typedef struct gameData_t
|
||||
{
|
||||
snake_t snake;
|
||||
map_t map; // definition der geladenen karte
|
||||
bool mapIsLoaded; // true when config.map is valid
|
||||
int foodX, foodY; // Positon des Futters (es gibt immer nur 1 Futter)
|
||||
int lifesRemaining; // implementieren wir nicht!!
|
||||
int timestampLastCycle;
|
||||
gameState_t gameState;
|
||||
snake_t snake; // data describing snake
|
||||
map_t map; // loaded map
|
||||
bool mapIsLoaded; // true when game.map is valid
|
||||
int foodX, foodY; // current position of food
|
||||
int lifesRemaining; // not implemented
|
||||
int timestampLastCycle; // time last game cycle started
|
||||
gameState_t gameState; // state the game is in
|
||||
} gameData_t;
|
||||
|
||||
// global struct for storing all game data (defined in game.c)
|
||||
extern gameData_t game;
|
||||
|
||||
|
||||
// run once at game start and does the following:
|
||||
// - init snake
|
||||
// - load map
|
||||
// - place initial food
|
||||
void gameInit();
|
||||
// berechnet BlockSizePx: windowSize/mapWidth
|
||||
// ruft snakeInit auf
|
||||
// ruft placeFood auf
|
||||
// platziert Wände
|
||||
|
||||
void handlePortals(); //(local)
|
||||
// Prüft, ob Snake sich auf einem Portal befindet
|
||||
//if true: snakeSetHeadPos auf
|
||||
|
||||
void runGameCycle();
|
||||
// checkCollision() auf
|
||||
// ruft placeFood() auf
|
||||
// ruft checkEaten() auf
|
||||
// if checkEaten then snakeGrow()
|
||||
// Snakemove(), TickTimerReset
|
||||
//ruft am Ende vom gameCycle renderGame() auf
|
||||
// when snake head is on a portal-input, sets snake head to portal-target
|
||||
void handlePortals(); //(ran in gameCycle)
|
||||
|
||||
|
||||
// function that is repeatedly run at every game tick
|
||||
// - moves snake to next position
|
||||
// - handles collision, portals, food
|
||||
// - triggers frame update (render.c)
|
||||
void runGameCycle();
|
||||
@@ -1,6 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "config.h"
|
||||
#include "snake.h"
|
||||
|
||||
|
||||
// Struct that stores all information needed for one Portal on the map
|
||||
@@ -11,32 +13,57 @@ typedef struct portal_t
|
||||
char *color;
|
||||
} portal_t;
|
||||
|
||||
|
||||
// Struct that stores all information needed for one Collision box on the map
|
||||
typedef struct collisionBox_t
|
||||
{
|
||||
int posX, posY;
|
||||
} collisionBox_t;
|
||||
|
||||
|
||||
// Struct that describes an entire map
|
||||
typedef struct map_t {
|
||||
int width;
|
||||
int height;
|
||||
const char *name[128];
|
||||
char name[128];
|
||||
collisionBox_t collisions[MAX_MAP_FIELDS];
|
||||
int collisionCount;
|
||||
portal_t portals[MAX_MAP_FIELDS];
|
||||
int portalCount;
|
||||
} map_t;
|
||||
|
||||
//return true when provided coordinate matches a collision box
|
||||
bool checkCollides(int x, int y);
|
||||
|
||||
//generate random map based on difficulty level
|
||||
map_t generateMap(int difficulty);
|
||||
|
||||
//search and load map by name (if not found loads default map)
|
||||
// search and load map by name in storedMaps[] (map.c)
|
||||
// stops program when map not found!
|
||||
void loadMapByName(char *name);
|
||||
|
||||
|
||||
//load map by passed definition
|
||||
void loadMap(map_t map);
|
||||
|
||||
|
||||
//return true when provided coordinate matches the position of a collision box
|
||||
bool checkCollides(map_t map, int x, int y);
|
||||
|
||||
|
||||
// generate random map based on difficulty level
|
||||
// NOT IMPLEMENTED
|
||||
map_t generateMap(int difficulty, int sizeX, int sizeY);
|
||||
|
||||
|
||||
void printMap(map_t map);
|
||||
// function that prints a map to console (stdout)
|
||||
// note: currently also prints snake and food which may be bugged/unintended
|
||||
|
||||
|
||||
// function that renders all current game objects to one 2d int array
|
||||
// NOTE: passed Array has to be zero-initialized! (int arr[][] = {{0}})
|
||||
// useful for rendering game to console or sdl
|
||||
// 1=collision, 2=portalIn, 3=portalOut, 4=snakeHead, 5=snakeTail
|
||||
void renderGameToArray(int mapFrame[MAX_MAP_SIZE][MAX_MAP_SIZE], map_t map, snake_t snake);
|
||||
|
||||
|
||||
// stored map presets can be globally accessed (maybe needed by menu.c)
|
||||
// not: maps defined in map.c end of file
|
||||
extern const map_t * storedMaps[16];
|
||||
extern const int storedMapsCount;
|
||||
Reference in New Issue
Block a user