Skip to content
Snippets Groups Projects
Verified Commit 0be11d68 authored by ja3-saxby's avatar ja3-saxby
Browse files

Added a Block class for creating blocks to play with

Added some initial startup routines
parent 1cb0ff4f
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,14 @@
* @brief This is my submission for Coursework Challenge 1 of UWE's Internet of
* Things module.
*
* @details This a game called "Blocks", a clone of a popular and well-known
* block-stacking game which shall not be named.
* Blocks fall from the top of the screen and the player has to try and arrange
* them in the most space-efficient way possible without letting the blocks
* stack up all the way to the top of the screen.
* @details In this version, A is used to shift the current block left and B is
* used to shift it right.
*
* @author Joshua Saxby <Joshua2.Saxby@live.uwe.ac.uk>
* @date 2020
*
......@@ -28,9 +36,146 @@
MicroBit UBIT;
/**
* @brief The different kinds of shapes our Blocks can take
*/
enum class Shape {
FLAT_L, // looks like ¬ when facing SOUTH
TALL_L, // looks like L when facing NORTH
UPSIDE_DOWN_T, // looks like T when facing SOUTH
PIPE, // looks like | when facing EAST or WEST
SQUARE, // a 2x2 square
};
const size_t SHAPE_TYPES_COUNT = 5u;
/**
* @brief Describes rotation of the block based on compass direction
* @details The top of the shape is said to be pointing in the given direction.
* @note Special magic values are chosen to allow modulo-arithmetic to produce
* easy rotation calculations
*/
enum class RotationDirection : uint8_t {
NORTH = 0u,
EAST = 1u,
SOUTH = 2u,
WEST = 3u,
};
/*
* we store the sprites for our different block shapes in FLASH, i.e. read-only
* storage. This requires manual data-packing as follows
*/
const uint8_t FLAT_L_NORTH_SPRITE[] __attribute__ ((aligned (4))) = {
0xff, 0xff, // magic number requesting data to be stored in FLASH
3, 0, // image width followed by zero
3, 0, // image height followed by zero
// image data follows:
0, 0, 0,
1, 0, 0,
1, 1, 1,
};
const uint8_t TALL_L_NORTH_SPRITE[] __attribute__ ((aligned (4))) = {
0xff, 0xff, // magic number requesting data to be stored in FLASH
3, 0, // image width followed by zero
3, 0, // image height followed by zero
// image data follows:
1, 0, 0,
1, 0, 0,
1, 1, 0,
};
const uint8_t UPSIDE_DOWN_T_NORTH_SPRITE[] __attribute__ ((aligned (4))) = {
0xff, 0xff, // magic number requesting data to be stored in FLASH
3, 0, // image width followed by zero
3, 0, // image height followed by zero
// image data follows:
0, 0, 0,
0, 1, 0,
1, 1, 1,
};
const uint8_t SQUARE_NORTH_SPRITE[] __attribute__ ((aligned (4))) = {
0xff, 0xff, // magic number requesting data to be stored in FLASH
3, 0, // image width followed by zero
3, 0, // image height followed by zero
// image data follows:
0, 0, 0,
1, 1, 0,
1, 1, 0,
};
const uint8_t PIPE_NORTH_SPRITE[] __attribute__ ((aligned (4))) = {
0xff, 0xff, // magic number requesting data to be stored in FLASH
3, 0, // image width followed by zero
3, 0, // image height followed by zero
// image data follows:
0, 0, 0,
0, 0, 0,
1, 1, 1,
};
/**
* @brief Stores the shape and colour information for a Block in the game.
*/
class Block {
public:
/**
* @brief Default constructor, randomly selects attributes for this Block.
*/
Block();
/**
* @brief Returns a read-only image of what this Block looks like
*/
MicroBitImage image() const;
private:
/**
* @brief The Shape that this Block is
*/
Shape shape;
/**
* @brief The direction the top of this Block is facing
*/
RotationDirection facing;
};
Block::Block()
: shape((Shape)UBIT.random(SHAPE_TYPES_COUNT)) // choose a random shape
, facing(NORTH) // for now, all shapes face North
{}
MicroBitImage Block::image() const {
// NOTE: This doesn't handle rotation direction yet
switch (this->shape) {
case Shape::FLAT_L:
return MicroBitImage((ImageData*)FLAT_L_NORTH_SPRITE);
case Shape::TALL_L:
return MicroBitImage((ImageData*)TALL_L_NORTH_SPRITE);
case Shape::UPSIDE_DOWN_T:
return MicroBitImage((ImageData*)UPSIDE_DOWN_T_NORTH_SPRITE);
case Shape::SQUARE:
return MicroBitImage((ImageData*)SQUARE_NORTH_SPRITE);
case Shape::PIPE:
return MicroBitImage((ImageData*)PIPE_NORTH_SPRITE);
}
}
int main() {
// Initialise the micro:bit runtime.
UBIT.init();
// draw first dot to indicate runtime system is initialised
UBIT.display.setPixelValue(0, 2, 255);
// seed the PRNG with the HRNG
UBIT.seedRandom();
// draw second dot to indicate PRNG has been seeded
UBIT.display.setPixelValue(2, 2, 255);
// ensure display is set to black and white mode
UBIT.display.setDisplayMode(DISPLAY_MODE_BLACK_AND_WHITE);
// draw third and final dot to indicate screen has been set up
UBIT.display.setPixelValue(4, 2, 1);
UBIT.display.scroll("BLOCKS!");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment