How does it work?

First, the code imports two modules: microbit and music:

# musical quiz buzzer for micro:bit
# blog.withcode.uk

from microbit import *
import music

music.set_tempo(bpm=220)
PLAYER_A_TUNE = ["c", "d", "e", "f", "g"]

PLAYER_B_TUNE = ["g", "f", "e", "d", "c"]

display.scroll("Quiz buzzer")

while True:
    display.show(Image.HAPPY)
    if button_a.was_pressed():
        display.show("A")
        music.play(PLAYER_A_TUNE)
        sleep(2000)
    elif button_b.was_pressed():
        display.show("B")
        music.play(PLAYER_B_TUNE)
        sleep(2000)
    sleep(20)

Importing a module in python means that your code can access and use additional features. The more modules you import, the more features your code can access, but the more memory it needs to run.

The microbit module lets you control the display and sense the buttons and the music module lets you play simple melodies. The sound quality is pretty poor – the sort of thing you’d expect from a really old fashioned arcade machine of the pong / space invaders era.

# musical quiz buzzer for micro:bit
# blog.withcode.uk

from microbit import *
import music

music.set_tempo(bpm=220)
PLAYER_A_TUNE = ["c", "d", "e", "f", "g"]

PLAYER_B_TUNE = ["g", "f", "e", "d", "c"]

display.scroll("Quiz buzzer")

while True:
    display.show(Image.HAPPY)
    if button_a.was_pressed():
        display.show("A")
        music.play(PLAYER_A_TUNE)
        sleep(2000)
    elif button_b.was_pressed():
        display.show("B")
        music.play(PLAYER_B_TUNE)
        sleep(2000)
    sleep(20)

Line 7 sets the speed (tempo) of the music that plays when either player presses a button. A higher number for bpm(beats per minute) makes the tune play faster

Lines 8 and 10 make two lists, each containing 5 characters. The characters represent the names of the musical note that should be played.

tunes for each player

tunes for each player

These are pretty rubbish tunes and you’ll want to write your own much better ones.

Have a look here for more detail about how to write your own tunes but here are some examples:

“c#:4”  plays a c sharp for 4 beats

“db5:1”  plays a d flat for 1 beat. The 5 means it plays an octave above middle C (which is octave 4)

“eb3:3”  plays an e flat for 3 beats. The first 3 before the colon means it plays an octave below middle C (which is octave 4)

“r:2” plays a rest (nothing – it’s a gap or pause in the music) for two beats.

# musical quiz buzzer for micro:bit
# blog.withcode.uk

from microbit import *
import music

music.set_tempo(bpm=220)
PLAYER_A_TUNE = ["c", "d", "e", "f", "g"]

PLAYER_B_TUNE = ["g", "f", "e", "d", "c"]

display.scroll("Quiz buzzer")

while True:
    display.show(Image.HAPPY)
    if button_a.was_pressed():
        display.show("A")
        music.play(PLAYER_A_TUNE)
        sleep(2000)
    elif button_b.was_pressed():
        display.show("B")
        music.play(PLAYER_B_TUNE)
        sleep(2000)
    sleep(20)

Line 12 scrolls the text “Quiz buzzer” across the screen. The quotation marks are important. You can use single quotes ‘ or double quotes ” as long as you use the same ones at the start and end of your text.

Line 14 is the start of a while loop. While loops repeat a block of code whilst a condition is met. The code that will repeat is the code that is indented (pushed in from the side), so lines 14-24 will repeat. In this case, the code will keep repeating forever, because Trueis always True but often you’d see a conditional statement after the word while such as:while button_a.is_pressed() == False: which would keep looping until the condition isn’t met any more.

Indentation is really important in python. It’s how python works out how many lines to repeat in a while loop, or how many lines to run in a conditional block like an if statement. A general rule is that any line that ends with a :expects the next line (or lines) to be indented.

Lines 16 -19 and 20-23 are similar: they detect if a button has been pressed, show which player has pressed the button and play their tune. The sleep(2000)pauses the program for 2 seconds (2000 ms)

Line 24 just makes the program pause for 20ms every time the loop goes round. This stops the browser from freezing in the simulator or stops the micro:bit from using up too much battery power. If you didn’t have it, the code would keep looping as fast as it possibly could which would use more CPU resources than were necessary.