SnakeGame

A simple snake game. Uses code-generated graphics to avoid relying on external assets.

Game rules: * The player controls a snake. * The player must collect apples to get score, which also makes the snake grow. * Every time an apple is collected, a new one is randomly spawned on the map at a point outside of the snake. * If the player either hits the snake or wall, they lose. * If the player somehow makes the snake to grow so big a new apple cannot be spawned, then the player wins.

Design guidelines: * No much external assets. (except for engine essentials). * Snake tail follows past directions

Some possible assignments to practice using the engine: * Use some external assets. * Add some obstacles. * Add a second, third, etc. player.

Constructors

this
this()

Constructor to initialize things. It is important to keep initialization and game logic separate.

Members

Enums

Direction
enum Direction

Used for direction tracing in the API.

TileTypes
enum TileTypes

Defines tile type codes.

Functions

axisEvent
void axisEvent(uint id, BindingCode code, uint timestamp, float value)
Undocumented in source. Be warned that the author may not have intended to support it.
clearTilemap
void clearTilemap()

Since tilemaps get initialized with 0xFFFF (none) tiles, we need to set it to 0x0000

controllerAdded
void controllerAdded(uint id)
Undocumented in source. Be warned that the author may not have intended to support it.
controllerRemoved
void controllerRemoved(uint id)
Undocumented in source. Be warned that the author may not have intended to support it.
keyEvent
void keyEvent(uint id, BindingCode code, uint timestamp, bool isPressed)

Key event data is received here.

moveSnake
void moveSnake()

Moves the snake in the given direction.

onQuit
void onQuit()

Makes it possible to close the game the proper way.

placeNextApple
void placeNextApple()

Places the next apple to a random empty location. If it can no longer put any new apples anywhere, then the game have been won.

timerEvent
void timerEvent(Duration jitter)

A timer event, to make the game run stable regardless of the framerate.

whereTheMagicHappens
void whereTheMagicHappens()

Main loop cycle, where all the good things happen.

Variables

apple
Bitmap8Bit apple;

These are the tiles, we're going to use in our game

dir
ubyte dir;

Contains the current direction.

empty
Bitmap8Bit empty;

These are the tiles, we're going to use in our game

ih
InputHandler ih;

Handles all the inputs. On every keypress/buttonpress, a new event is created, which changes the internal state of the program.

output
OutputScreen output;

The output screen, where the output is being displayed.

playfield
TileLayer playfield;

The TileLayer, that: * Displays the current game state * Stores playfield data (snaketail, apple's position, etc)

prevDir
ubyte prevDir;

Contains the previous direction.

raster
Raster raster;

The raster handles frame buffer and layer priority management. Even though we currently only have a single tilelayer, we will still needing it.

rng
RandomNumberGenerator rng;

A random number generator

score
int score;

Counts how much score the player has.

snakeH
Bitmap8Bit snakeH;

These are the tiles, we're going to use in our game

snakeHead
Point snakeHead;

The position of the head of the snake. We can track the rest of the snake by tracking the directions.

snakeNE
Bitmap8Bit snakeNE;
snakeNW
Bitmap8Bit snakeNW;
snakeSE
Bitmap8Bit snakeSE;
snakeSW
Bitmap8Bit snakeSW;
snakeV
Bitmap8Bit snakeV;

These are the tiles, we're going to use in our game

state
ubyte state;

Program and game state. 4: program exit 5: game start 6: game over 7: game won

Meta