In this challenge we will create a game for the BBC micro:bit.
Imagine you have been asked to bring a cupcake to Her Majesty the Queen Elizabeth II. You have picked the best cupcake from the kitchen and placed it at the centre of a silver tray.
You will have to carry the cupcake on its tray to the Queen, walking through the many rooms and corridors of Buckingham Palace.
Watch out, if you tilt the tray, you may end up dropping the Queen’s cupcake!
For this challenge we will replace the silver tray with a BBC micro:bit. The cupcake will be a sprite positioned in the centre of the LED screen, the tray will be the grid of 5×5 LEDs on the micro:bit.
The player will have to carry the micro:bit flat on the back of their hand, and carry it around the room, making sure they keep it as flat as possible.
Our program will use the built-in accelerometer input sensor of the micro:bit to find out if the micro:bit is leaning (forward, backward, to the left or to the right).
If so the sprite (cupcake) will slide in the right direction (The LED light will move on the 5×5 grid).
The game will end when/if the cupcake is at the edge of the 5X5 LED grid and the micro:bit is still tilted: the cupcake is falling off the grid/tray.
You will need to use the micro:bit website to create the code using the Block Editor.
We will soon have a video to show you how to build the code step by step. In the meantime you can check the full code below:
Checking the code above answer the following questions:
- Can you identify the block of code used for the micro:bit to detect if it has been tilted to the left?
- Can you identify the block of code used for the micro:bit to know when the cupcake is falling off the tray?
- Can you explain how does the microbit decide to stop the game by running the last block to display the Game Over message?
- Can you explain the purpose of the variable called tolerance?
- Why do we need a tolerance?
- What would be the impact on the game if the tolerance with 500 instead of 200? (Would it be easier to play or harder? Why?)
- What would be the impact on the game if the tolerance with 50 instead of 200? (Would it be easier to play or harder? Why?)
Extension Task 1
Add some more code to this game to record how long the player/waiter took to deliver the cupcake.
The micro:bit should record the running time from the start (just after the “3-2-1-Go” message) and allow the user to stop the game when they press button A.
The player will have to follow a set route (e.g. around the classroom) and if they manage to complete the route without dropping the cupcake, they should press button A to stop the counter, and the micro:bit should display their time.
Extension Task 2
Tweak the code so that if a player has been playing for 10 seconds without dropping the cupcake, the tolerance then changes to make the game harder to play (e.g. tolerance change from 200 to 100)