Move your player left and right to prevent it from getting hit by the falling rocks!
1 x MicroBit,
1 x USB cable
Under the variables tab, create 5 new variables: boulder, player, score, speed and game_over
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
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)
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.
- 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.
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.
- Destroy both the player object and the boulder object ("destroy function found under game tab). ____________________________________________
- Display the score of the player.
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!!
Code and References
Completed this tutorial?
Spread the word! Let's get this trending on social media with #letsgethacking #Rainfall