In this project we create a Microbit game where you have to guess when exactly 11 seconds has passed!
Create a new Microbit 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 the player will try and guess when 11 seconds has passed. We will need a variable to remember the start time to create a variable called 'starttime'.
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 code to remember when the clock is started and to show an icon to let the user know the clock has started.
let starttime = 0 input.onButtonPressed(Button.A, function () { starttime = input.runningTime() basic.showIcon(IconNames.SmallDiamond) })
The running time (ms) block returns the number of milliseconds since the program started. (One second is 1000 milliseconds).
Now create a variable called 'taken', we are going to use this to store the amount of time it has taken between pressing A and pressing B.
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 let's program the B button to set the 'taken' by subtracting the 'starttime' from the current running time.
Add the following code.
input.onButtonPressed(Button.B, function () { taken = input.runningTime() - starttime })