Don't be the one left holding the Microbit when the countdown stops! In this project we make the Microbit into a Hot Potatoe game.
Go to the https://makecode.microbit.org website and create a new project.
Go to the Makecode.com Microbit website using the link below and click on the 'New Project' button underneath the 'My Projects' heading.
Install the micro:bit app on your iPad or tablet.
Open the app, tap 'Create code' and then create a new project.
In the game there will be a countdown of a random number of seconds. Create a variable called seconds, we will use this to store the amount of seconds.
Once you've created the variable, add the following code to set the amount of seconds to a number between 4 and 20 when you press the A button.
input.onButtonPressed(Button.A, function () { seconds = randint(4, 20) })
In the Variables toolbox, create a new variable by clicking the 'Make a Variable' button.
Once you click this button a box will appear asking what you want to call your variable. Give it a name that reminds you what you will be using it for. For example, if you wanted to keep track of your score in a game, you would create a variable called 'score'.
Now add the following new code to subtract 1 from the seconds variable until it is equal to 0.
let seconds = 0 input.onButtonPressed(Button.A, function () { seconds = randint(4, 20) while (seconds > 0) { seconds += -1 basic.pause(1000) } })
Add the following new code to show an animation while the countdown is on.
Note that each icon takes 600 milliseconds to display, so each 'second' in the game is actually 2.2 seconds (600 + 600 + 1,000 = 2,200 milliseconds).
let seconds = 0 input.onButtonPressed(Button.A, function () { seconds = randint(4, 20) while (seconds > 0) { seconds += -1 basic.pause(1000) basic.showIcon(IconNames.Diamond) basic.showIcon(IconNames.SmallDiamond) } })
Finally add the following new code to show the skull icon and play a sound when the countdown reaches 0.
let seconds = 0 input.onButtonPressed(Button.A, function () { seconds = randint(4, 20) while (seconds > 0) { seconds += -1 basic.pause(1000) basic.showIcon(IconNames.Diamond) basic.showIcon(IconNames.SmallDiamond) } basic.showIcon(IconNames.Skull) soundExpression.sad.play() })