Microbit Bop It Game

Game MicrobitNormal

In this project we will turn our microbit into a Bop It game using the different inputs in the microbit.

The microbit will show a random image and you must perform the correct action for that image.

ImageAction
Left arrowPress A button
Right arrowPress B button
Left and right arrowsPress A & B together

You have 20 seconds to see how many you can get right!

1

Create a new Microbit project

2

Create a variable called ‘action’

Create a variable called ‘action’, we will use this variable to store and remember a random action. In the on start block, set ‘action’ to -1 by adding this code:

let action = -1

3

Add a start countdown block

Add a start countdown block and set it to 20000 milliseconds (which is 20 seconds).

let action = -1 
game.startCountdown(20000)

4

Set 'action' to a random number

In the forever block we want to check if ‘action’ is equal to -1, if it is then we want to set ‘action’ to a random number between 0 and 2.

basic.forever(function () {
    if (action == -1) {
        action = Math.randomRange(0, 2)
    }
})

5

Display the image

Next depending on what ‘action’ is now equal to, we are going to display an image on our Microbits telling the user what action they need to do.

  • action = 0 means you should press the A button (arrow left)
  • action = 1 means you should press the B button (arrow right)
  • action = 2 means you should press the A+B buttons (arrows left and right)

Add the following code to check what 'action' is equal to and show the appropriate image.

basic.forever(function () {
    if (action == -1) {
        action = Math.randomRange(0, 2)
    }
    if (action == 0) {
        basic.showLeds(`
            . . # . .
            . # . . .
            # . . . .
            . # . . .
            . . # . .
            `)
    } else if (action == 1) {
        basic.showLeds(`
            . . # . .
            . . . # .
            . . . . #
            . . . # .
            . . # . .
            `)
    } else if (action == 2) {
        basic.showLeds(`
            . . # . .
            . # . # .
            # . . . #
            . # . # .
            . . # . .
            `)
    }
})


6

Program the buttons

Now we will program the buttons to check if you pressed the right one! If you get it right you get a point, if you get it wrong it's game over!!

action equalsImageButton
0Left arrowPress A button
1Right arrowPress B button
2Left and right arrowsPress A & B together
-2

Game over


Add the following code to program the buttons:

input.onButtonPressed(Button.A, function () {
    if (action == 0) {
        game.addScore(1)
        action = -1
    } else {
        action = -2
        game.gameOver()
    }
})
input.onButtonPressed(Button.AB, function () {
    if (action == 2) {
        game.addScore(1)
        action = -1
    } else {
        action = -2
        game.gameOver()
    }
})
input.onButtonPressed(Button.B, function () {
    if (action == 1) {
        game.addScore(1)
        action = -1
    } else {
        action = -2
        game.gameOver()
    }
})

7

Check Game Over

If the countdown has elapsed then we need to set the variable 'action' to -2. Otherwise it will set it to a random number and then show an instruction on the screen and we just want to show game over and the score.

Add 'if <is game over> then' code to the top of the 'forever' block:

basic.forever(function () {
    if (game.isGameOver()) {
        action = -2
    }
    if (action == -1) {
        action = Math.randomRange(0, 3)
    }
    if (action == 0) {
        basic.showLeds(`
            . . # . .
            . # . . .
            # . . . .
            . # . . .
            . . # . .
            `)
    } else if (action == 1) {
        basic.showLeds(`
            . . # . .
            . . . # .
            . . . . #
            . . . # .
            . . # . .
            `)
    } else if (action == 2) {
        basic.showLeds(`
            . . # . .
            . # . # .
            # . . . #
            . # . # .
            . . # . .
            `)
    } else if (action == 3) {
        basic.showLeds(`
            # . # . #
            . # . # .
            # . # . #
            . # . # .
            # . # . #
            `)
    }
})

8

Challenge!

That's it you've completed the game! Download it onto your microbit and play it for real, how many points can you score?

Challenge

Can you add another action and image? How about when you show an X on the screen the player must shake the microbit.

End of lesson
Copyright Notice
This lesson is copyright of Coding Ireland. Unauthorised use, copying or distribution is not allowed.