Add "Snake" game example
This commit is contained in:
committed by
Ryan C. Gordon
parent
69a2d7960c
commit
dbb4e05c28
51
examples/game/01-snake/snake.h
Normal file
51
examples/game/01-snake/snake.h
Normal file
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Interface definition of the Snake game.
|
||||
*
|
||||
* This code is public domain. Feel free to use it for any purpose!
|
||||
*/
|
||||
#ifndef SNAKE_H
|
||||
#define SNAKE_H
|
||||
#define SNAKE_GAME_WIDTH 24U
|
||||
#define SNAKE_GAME_HEIGHT 18U
|
||||
#define SNAKE_MATRIX_SIZE (SNAKE_GAME_WIDTH * SNAKE_GAME_HEIGHT)
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SNAKE_CELL_NOTHING = 0U,
|
||||
SNAKE_CELL_SRIGHT = 1U,
|
||||
SNAKE_CELL_SUP = 2U,
|
||||
SNAKE_CELL_SLEFT = 3U,
|
||||
SNAKE_CELL_SDOWN = 4U,
|
||||
SNAKE_CELL_FOOD = 5U
|
||||
} SnakeCell;
|
||||
|
||||
#define SNAKE_CELL_MAX_BITS 3U /* floor(log2(SNAKE_CELL_FOOD)) + 1 */
|
||||
|
||||
typedef enum
|
||||
{
|
||||
SNAKE_DIR_RIGHT,
|
||||
SNAKE_DIR_UP,
|
||||
SNAKE_DIR_LEFT,
|
||||
SNAKE_DIR_DOWN
|
||||
} SnakeDirection;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned char cells[(SNAKE_MATRIX_SIZE * SNAKE_CELL_MAX_BITS) / 8U];
|
||||
char head_xpos;
|
||||
char head_ypos;
|
||||
char tail_xpos;
|
||||
char tail_ypos;
|
||||
char next_dir;
|
||||
char inhibit_tail_step;
|
||||
unsigned occupied_cells;
|
||||
} SnakeContext;
|
||||
|
||||
typedef int (*RandFunc)(int n);
|
||||
|
||||
void snake_initialize(SnakeContext *ctx, RandFunc rand);
|
||||
void snake_redir(SnakeContext *ctx, SnakeDirection dir);
|
||||
void snake_step(SnakeContext *ctx, RandFunc rand);
|
||||
SnakeCell snake_cell_at(const SnakeContext *ctx, char x, char y);
|
||||
|
||||
#endif /* SNAKE_H */
|
||||
Reference in New Issue
Block a user