Rainfall

Move your player left and right to prevent it from getting hit by the falling rocks!


You'll need

1 x MicroBit,
1 x USB cable

Step 1

Instructions

Under the variables tab, create 5 new variables: boulder, player, score, speed and game_over




Materials


Step 2

Instructions

Firstly, we are going to have to set some parameters under the "on start" block! ____________________________________________
1. Set score to 0 ____________________________________________
2. Set speed to 250 (time taken for boulder to move down one LED) ____________________________________________
3. Set game_over to false ____________________________________________
4. Create a sprite for the player at positions 2,4 : that is 3 LED spots to the right and 5 LED spots downwards ("create sprite" function can be found under games tab) ____________________________________________
5. Create a sprite for the boulder at a random position along the top
row of LEDs. ("pick random" function can be found under math
tab)




Materials


Step 3

Instructions

We are also going to need to take in the user input! ____________________________________________
Add two new input blocks: On button B pressed, make the player variable shift to the right by one LED; On button A pressed, make the player variable shit to the left by one LED. (note: green block can be found under "game" tab)




Materials


Step 4

Instructions

Once we have added our buttons, go back to the "on start block" and add a while loop. This while loop will run as long as game_over is equals to false. ____________________________________________
1. Under the while loop, add the pause function and attach the speed variable to it. This will make the boulder pause for a while before moving down.


  1. After that, make the boulder variable move down by 1 LED every time the loop runs. This will ensure that the boulder moves down the screen.



Materials


Step 5

Instructions

Now, every time the boulder moves down by 1 LED, we need to check if the boulder has hit the player. ____________________________________________
1. Add an if/else block that checks if the (x,y) position of the player is equals to the (x,y) position of the boulder. ____________________________________________
2. If true, then set the game_over variable to true as the boulder has collided with the player.


  1. Destroy both the player object and the boulder object ("destroy function found under game tab). ____________________________________________
  2. Display the score of the player.



Materials


Step 6

Instructions

You are almost there! Now, if the player successfully managed to dodge the boulder, we need to destroy the existing boulder and spawn a new one at the top. ____________________________________________
1. Add an else if condition under the same block and check if the boulder has reached the bottom row ( Y = 4) ____________________________________________
2. If true, then add a pause function and attach the speed variable. Then destroy the boulder game object, thus giving time for it to be destroyed. ____________________________________________
3. Add 1 to the score variable, and decrease the speed variable by -5. This will increase the difficulty as the boulders will now rain faster. ____________________________________________
4. Lastly, spawn a new boulder variable at a random position along the top row of LEDs (you can copy paste this from step 2 on top) ____________________________________________
Your game is now complete. Have fun!!




Materials


Code and References

Completed this tutorial?

Spread the word! Let's get this trending on social media with #letsgethacking #Rainfall