Mathematics Game Microbit
Normal
60 mins
375 points
What you need:
  • Computer/laptop

Higher or Lower Game

In this lesson we will create a game on the Microbit to display a random number between 1 and 100. Using the A and B buttons on the Microbit, the player has to guess if the next number will be higher or lower. See who can get the highest score!

This message will be visible to everyone. Send

    This code along has finished! ⌚

    You can watch it back by playing the video!

    The code along is open for everyone to join in but if you would like to earn the points and badges please join our club!

    1 - Create a new Microbit 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.

    Add the following code.

    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)
            game.addScore(1)
            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.

    6 - Resetting the numbers

    Now that the player has guessed correctly and scored a point, we need to reset the 'number' and 'nextnumber' variables so we can play the next round.

    First set the 'number' variable to be the same as the 'nextnumber' variable, as this is now our starting number. Then we need to reset the 'nextnumber' variable to a random number between 0 and 100.

    Finally in this step, we will display the 'number' variable on screen again so the player can make their guess. 

    Add the new code underneath the pause (ms) 1000  block.

    input.onButtonPressed(Button.A, function () {
        basic.pause(500)
        if (nextnumber <= number) {
            basic.showIcon(IconNames.Yes)
            basic.pause(1000)
            game.addScore(1)
            basic.pause(1000)
            number = nextnumber
            nextnumber = randint(1, 100)
            basic.showNumber(number)
        } else {
            basic.showIcon(IconNames.No)
        }
    })
    
    

    7 - Game over

    If the player chose lower (pressed the A button) and the next number was actually higher then we will:

    1. pause 1 second
    2. show what the next number was
    3. pause another 1 second
    4. end the game

    Add the following new code underneath the incorrect show icon in the 'else' part of the block.

    input.onButtonPressed(Button.A, function () {
        basic.pause(500)
        if (nextnumber <= number) {
            basic.showIcon(IconNames.Yes)
            basic.pause(1000)
            game.addScore(1)
            basic.pause(1000)
            number = nextnumber
            nextnumber = randint(1, 100)
            basic.showNumber(number)
        } else {
            basic.showIcon(IconNames.No)
            basic.pause(1000)
            basic.showNumber(nextnumber)
            basic.pause(1000)
            game.gameOver()
        }
    })
    
    


    The game over block ends the game, plays an animation and displays what your score was.

    8 - Choosing higher

    As the code for guessing higher is mostly the same as for guessing lower, we will duplicate the entire 'on button A pressed' group of blocks and then modify them.

    1. Right click on the 'on button A pressed' and then click on 'Duplicate', this will create a copy of this group of blocks.
    2. Drag the new blocks into an area that has some clear space.

    Now we need to make 2 changes to the new code we duplicated:

    1. Change the button in the 'on button A pressed' to be the B button.
    2. As they're guessing higher we need to set the condition of the if block to check if the 'nextnumber' variable is higher than the 'number' variable.

    Those are the only 2 changes you need to make to the 'on button B pressed' group of blocks, the scoring and game over is the exact same.

    input.onButtonPressed(Button.B, function () {
        basic.pause(500)
        if (nextnumber >= number) {
            basic.showIcon(IconNames.Yes)
            basic.pause(1000)
            game.addScore(1)
            basic.pause(1000)
            number = nextnumber
            nextnumber = randint(1, 100)
            basic.showNumber(number)
        } else {
            basic.showIcon(IconNames.No)
            basic.pause(1000)
            basic.showNumber(nextnumber)
            basic.pause(1000)
            game.gameOver()
        }
    })
    
    

    Join our club 😃

    Create your profile, earn points & badges and learn how to code!

    Copyright Notice
    This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.
    🍪 Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more