You are going to code your micro:bit to tell you how compatible you are with your friends.
Step 2: Displaying a rating
Show a friendship rating on the screen.
- Click the ‘Globals’ library, and create a new variable called
- Drag your variable to the end of your
- Click the ‘Random’ library, and use the random block to set the rating to a random number between 0 and 100.
- Add code to
saythe rating to the screen.
- Finally, after a delay, display the instructions again.
- Test your code. Now, if you press buttons A and B together, a random rating should be displayed.
Step 3: Best mates
Let’s display a heart when two friends have a high rating.
- Add an
ifblock to your code, just before the instructions are shown.
- You want to display a heart if the rating is high (say over 75). To do this, click the down arrow in your
- Drag your
ratingvariable into the left side of your
ifblock, and add the number
75to the right side.
Any code inside this
if block will run if the rating is over 75.
- Show a heart image on the screen for 1 second if the rating is over 75.
- Test your code. Does an image of a heart show if the rating is over 75? You might have to try a few times to test it!
Challenge: Low rating
Can you display a cross or a sad face for a rating under 25?
Step 4: Adding an animation
Let’s display short animations in your program.
- Here’s the code for displaying a question mark for 1 second before clearing the screen:
- To make this question mark flash 5 times, you’ll need to put this code inside a
- Finally, you just need to tell the
forloop to start at
1, and count up to
5. This will make the code repeat 5 times.
Challenge: More animations
Can you make your other images flash on the screen?