Introduction: Let It Rain
This is a game which is to played on the Micro:bit
The game is called Rain and the aim is to avoid being hit by the falling objects.
Either by moving left of right and/or shooting the objects.
The game is operated by the following controls.
Shake – Initiates a new game.
A – Moves the character to the left.
B – Moves the character to the right.
A+B – Fires the projectile at the falling objects.
One point is awarded for each falling object hit by the projectile.
If the character is hit by the falling object then they lose a life after losing three lives the game ends, this is indicated by the sad face image and the score.
Each stage of the game is broken down in to a number of functions explained in the text.
Step 1: Initialize
Initialization of game parameters is accomplished by assigning a combination of numeric and Boolean variables.
Tidying the screen of previous information if any, assigning the start position of the gun character and allowing the user time to position the Micro-bit ready for the game to start.
It makes it easier to understand the code if the variables are given names that indicate their function
For example, ‘fire’ initiates the ‘gun’ that launches the ‘missile’.
Likewise, if endgamecount is greater than 3 then endgame is true.
As the gun only moves horizontally on the bottom row, y is always 4 and the x variable is changed to illuminate the appropriate LED using the plot and unplot commands.
The initialization process is used in two functions, On Start and On Shake.
As the names indicate On Start calls Initialize at the start of the code to set the variables used to the required values.
On Shake calls Initialize when the user initiates a new game.
This allows the user to restart the game and initializes the
game parameter variables, clears the screen and positions the game character in its start position.
Step 2: Forever Raining
This is the part of the game code that randomly generates the falling objects and initialises the start positions prior to moving them down the screen.
The impression of falling is defined by a time delay between turning the LED on and off and incrementing the position counter. At each increment of the position counter a check is made to determine if the falling object has collided with the game character.
If a collision is detected lives are lost up to a maximum of three then end of game notification is given the score is displayed and the falling object loop is terminated.
Rainstart is the start x position of this character, which is incremented down the screen by the ‘for’ loop changing index2 for the ‘y’ position. From 0 to 4 with 0 being the top and 4 being the bottom.
We do not want the rain falling too quickly that we cannot see it, so we add a delay with a pause.
As the rain falls it checks ahead to determine if there is a collision with the gun, this is accomplished using ‘point’ this determines the state of the LED ahead of it.
If the LED ahead of the rain is on then this is determined as a hit and a life is lost or the game is over.
To keep everything tidy we have to remove the previously lit LED so that there is the appearance of one rain drop continuously falling without leaving a trail.
Step 3: Moving the Gun Character
There are two input functions used to move the gun character.
These are On button A pressed and On button B pressed.
On button A pressed.
This function moves the game character to the left when the A button is pressed.
If it’s not the end of the game and with the gun value within its allowable range the previous gun character position is unplotted, the gun value decremented and the new gun position plotted.
If the gun value is less than 0 then the gun is plotted at x=0, y=4
On button B pressed.
This function moves the game character to the right when the B button is pressed.
If it’s not the end of the game and with the gun value iithin its allowable range the previous gun character position is unplotted, the gun value incremented and the new gun position plotted.
If the gun value is greater than 4 then the gun is plotted at x=4, y=4
Step 4: Launching the Projectile
We can move the gun character left and right and now it has to be able to fire the projectile at the objects raining down.
This is the part of the game code than generates the projectile and initialises the start positions prior to moving it up the screen.
The impression of upward motion is defined by a time delay between turning the LED on and off and incrementing the position counter. At each increment of the position counter a check is made to determine if the projectile has collided with a falling object.
If a collision is detected the falling object is deleted at that position, the score is updated, the falling object position variable is reset, the projectile position and initiator are reset, and the projectile loop is terminated.
If the endgame is false, fire = true and missile = 4 for its start position.
A while loop is create this enables single or continuous firing of projectiles while fire = true.
The missile variable is decremented from 4 (bottom) to 0 (top) to
At each point as the missile ascends up the display it checks for a collision with a rain object if no collision is detected the the projectile continues up and off the screen.
If a collision is detected the rain object is un-plotted, as it is now destroyed, its variable reset and the hit counter incremented by 1.
The missile is un-plotted to give the impression that this was destroyed.
The variables for the Rain, missile and fire are reset.
Step 5: Endofgame
This function sets the end game flag, displays the end game notification and the score and for each life lost displays the destruction blast.
The endofgame function is called for two instanced when the user loses a life and when all three lives have expired signalling the end of the game.
Each time the endofgame function is called the endgamecount is incremented, if less than 3 the blast function is called which animates the destruction of the gun character. The blast animation is a sequence of changing LED positions that indicate with animation the effects of the shock wave.
Whilst at the same time clearing the screen, resetting the gun variable to the default start position and applying a pause before re-commenting the game.
If endgamecount = 3 the blast function is called and following this the sad face is displayed indicating the end of the game followed by the score obtained. Once the score is cleared your ready to restart the game by shaking the Micro:bit.