Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
UFCFVK-15-2 - Internet of things - Challenge 1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
ja3-saxby
UFCFVK-15-2 - Internet of things - Challenge 1
Commits
0be11d68
Verified
Commit
0be11d68
authored
5 years ago
by
ja3-saxby
Browse files
Options
Downloads
Patches
Plain Diff
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
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
source/main.cpp
+145
-0
145 additions, 0 deletions
source/main.cpp
with
145 additions
and
0 deletions
source/main.cpp
+
145
−
0
View file @
0be11d68
...
...
@@ -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!"
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment