Mathematics Game Microbit Teacher Training
Beginner
60 mins
Teacher/Student led
160 points
What you need:
Chromebook/Laptop/PC
Microbit

# Higher or Lower Game

In this step-by-step lesson, you'll create a guessing game using Microbit. You'll learn to create variables, setup the game start, program button functions for guessing higher or lower, score points, reset numbers for new rounds, and handle game over scenarios. Finally, you'll download and play your game.
Learning Goals Learning Outcomes Teacher Notes Lesson Files

### 1 - Create a new Microbit project

Go to the https://makecode.microbit.org website and create a new project.

### 2 - Create 2 variables

Create 2 new variables called 'number' and 'nextnumber'.

We will use these to store the number that displays on the screen and the next number that the player needs to guess if it's higher or lower.

### 3 - Setup the start of the game

At the start of the game we're going to set the 'number' variable to a random number between 1 and 100, and we'll also set the 'nextnumber' variable to a random number between 1 and 100.

Once we've set the numbers we'll display the 'number' variable on our Microbit.

```let number = randint(1, 100)
let nextnumber = randint(1, 100)
basic.showNumber(number)

```

### 4 - Choosing lower

To play the game, the player needs to guess if the next number will be higher or lower than the number that's currently being displayed.

To guess lower they will press the A button, to guess higher they will press the B button.

Program the A button to guess lower
Add the following code to program the A button to:

1. pause half a second
2. check if the 'nextnumber' is less than or equal to 'number'
• if it is then show the correct icon as they guessed correctly
• else show the incorrect icon as they guessed incorrectly
```input.onButtonPressed(Button.A, function () {
basic.pause(500)
if (nextnumber <= number) {
basic.showIcon(IconNames.Yes)
} else {
basic.showIcon(IconNames.No)
}
})

```

### 5 - Scoring a point

If the player guesses correctly that the next number is lower, then as well as displaying the correct icon we will give them a point.

Add the following new code underneath the correct show icon block.

```input.onButtonPressed(Button.A, function () {
basic.pause(500)
if (nextnumber <= number) {
basic.showIcon(IconNames.Yes)
basic.pause(1000)
basic.pause(1000)
} else {
basic.showIcon(IconNames.No)
}
})

```

You will find the change score by 1  block in the Advanced > Game toolbox. This block plays a little flash animation and adds 1 point to your score.

# Unlock the Full Learning Experience

Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.