translation missing: en.general.accessibility.skip_to_content
Micro:Bit Says Game

Micro:Bit Says Game

If you're lucky enough to have a BBC Micro:Bit V2 we’ve got a great tutorial here to make a quick little “Simon Says” style game using JavaScript and Python. You can find the BBC Micro:Bit V2 here, or one of our Starter or Deluxe Kits.

Getting Started.

Open your web browser and go to the MakeCode website, click on New Project and name your project Micro:Bit Says then click the green Create button.
Connect your BBC Micro:Bit V2 to your computer, at the bottom of the screen press the three dots and press "Connect device". Once connected, once the program is finished hit the download button to instantly load it onto the Micro:Bit.

JavaScript Code:

Lines 1-2 - Setting up the game

The first two lines of code set the game up by creating a variable called Microbit and calling the function GameChoice which we will set up in the next step.

let MicroBit = 0
GameChoice()

Lines 3-12 Creating the GameChoice Function.

The code below creates the GameChoice Function which runs once the game starts and every time the user presses the correct option. This function sets the MicroBit variable to a random number between 0 and 2. It checks the value assigned to the variable and depending on the number, will display an A, B or up arrow.

function GameChoice() {
MicroBit = randint(0, 2)
if (MicroBit == 0) {
basic.showString("A")
} else if (MicroBit == 1) {
basic.showString("B")
} else if (MicroBit == 2) {
basic.showIcon(IconNames.Triangle)
}
}

Lines 13-19 Coding the A button.

The code below runs once the A button is pressed, it first checks to see if the value of the variable MicroBit is 0, if it is true it then recalls the function GameChoice. If it is the wrong button pressed it plays the sound Wawawawaa.

input.onButtonPressed(Button.A, function () {
if (MicroBit == 0) {
GameChoice()
} else {
music.startMelody(music.builtInMelody(Melodies.Wawawawaa), MelodyOptions.Once)
}
})

Lines 20-33 Coding the B button and TouchPad.

The code below is similar to when the A button is pressed but works for both the B button and Touchpad (logo).

input.onButtonPressed(Button.B, function () {
if (MicroBit == 1) {
GameChoice()
} else {
music.startMelody(music.builtInMelody(Melodies.Wawawawaa), MelodyOptions.Once)
}
})
input.onLogoEvent(TouchButtonEvent.Pressed, function () {
if (MicroBit == 2) {
GameChoice()
} else {
music.startMelody(music.builtInMelody(Melodies.Wawawawaa), MelodyOptions.Once)
}
})

Python Code:

Lines 1-2 - Setting up the game

The first two lines of code set the game up by creating a variable called Microbit and calling the function GameChoice which we will set up in the next step.

MicroBit = 0
GameChoice()

Lines 3-11 Creating the GameChoice Function.

The code below creates the GameChoice Function which runs once the game starts and every time the user presses the correct option. This function sets the MicroBit variable to a random number between 0 and 2. It checks the value assigned to the variable and depending on the number, will display an A, B or up arrow.

def GameChoice():
global MicroBit
MicroBit = randint(0, 2)
if MicroBit == 0:
basic.show_string("A")
elif MicroBit == 1:
basic.show_string("B")
elif MicroBit == 2:
basic.show_icon(IconNames.TRIANGLE)

Lines 12-17 Coding the A button.

The code below runs once the A button is pressed, it first checks to see if the value of the variable MicroBit is 0, if it is true it then recalls the function GameChoice. If it is the wrong button pressed it plays the sound Wawawawaa.

def on_button_pressed_a():
if MicroBit == 0:
GameChoice()
else:
music.start_melody(music.built_in_melody(Melodies.WAWAWAWAA),MelodyOptions.ONCE)
input.on_button_pressed(Button.A, on_button_pressed_a)

Lines 18-29 Coding the B button and TouchPad.

The code below is similar to when the A button is pressed but works for both the B button and Touchpad (logo).

def on_button_pressed_b():
if MicroBit == 1:
GameChoice()
else:
music.start_melody(music.built_in_melody(Melodies.WAWAWAWAA),MelodyOptions.ONCE)
input.on_button_pressed(Button.B, on_button_pressed_b)
def on_logo_pressed():
if MicroBit == 2:
GameChoice()
else:
music.start_melody(music.built_in_melody(Melodies.WAWAWAWAA),MelodyOptions.ONCE)
input.on_logo_event(TouchButtonEvent.PRESSED, on_logo_pressed)
Previous article Quadruped Robot Goat
Next article Assembling the Protective Raspberry Pi Zero 2 W Case