BBC micro:bit

Catch the Star

Program a real game: move left and right with the buttons to catch the falling stars before you run out of lives.

Day 6 50 to 60 minutes 7 steps Key Stage 3
Start step 1
A star falls towards the player.

Stars fall from the top of the micro:bit, one at a time. Your player is the bright light at the bottom. Press A and B to move left and right, and catch each star before it lands. Miss three and the game is over, and every catch makes the stars fall faster.

You will learn

  • Light single lights with set_pixel()
  • Move a player and keep it on the board
  • Use variables for the game's score, lives and speed
  • End a game with a while loop's condition

You will need

  • A computer with Chrome or Edge
  • A micro:bit and a USB data cable, or the simulator
  • Days 2 and 3, for buttons and random numbers

Words to know

pixel
One light on the micro:bit's display.
coordinates
x across, from 0 to 4, then y down, from 0 to 4.
game loop
Read the buttons, update the game, draw it, wait, and repeat.
variable
A named value that changes as the game goes on, such as score.

Step by step

  1. Step 1: Put the player on the board

    Start a new project called Catch the Star. display.set_pixel(x, y, 9) lights one light at brightness 9. x counts the columns from 0 on the left to 4 on the right, and y counts the rows from 0 at the top to 4 at the bottom. The player starts in the middle of the bottom row.

    main.pyMicroPython micro:bit editor
    from microbit import * player = 2 while True:    display.clear()    display.set_pixel(player, 4, 9)    sleep(100)
    The player: one bright light in the middle of the bottom row.
  2. Step 2: Move left and right

    A moves the player left by taking 1 from player, and B moves it right by adding 1. player > 0 stops it going off the left edge, and player < 4 stops it going off the right.

    New: lines 6 to 10.

    main.pyMicroPython micro:bit editor
    from microbit import * player = 2 while True:    if button_a.was_pressed() and player > 0:        player = player - 1    if button_b.was_pressed() and player < 4:        player = player + 1     display.clear()    display.set_pixel(player, 4, 9)    sleep(100)
    Press B twice: the player moves to the right edge, and no further.
  3. Step 3: A falling star

    The star has its own x and y. The loop runs ten times a second, which is too fast for a star, so tick counts the loops, and the star moves down a row every fifth one. When it reaches the bottom, a new star starts at the top, in a random column. The star is dimmer than the player, at 5.

    New: lines 2, 5 to 7, 15 to 22 and 25.

    main.pyMicroPython micro:bit editor
    from microbit import *import random player = 2star_x = random.randint(0, 4)star_y = 0tick = 0 while True:    if button_a.was_pressed() and player > 0:        player = player - 1    if button_b.was_pressed() and player < 4:        player = player + 1     tick = tick + 1    if tick == 5:        tick = 0        star_y = star_y + 1        if star_y == 4:            star_x = random.randint(0, 4)            star_y = 0     display.clear()    display.set_pixel(player, 4, 9)    display.set_pixel(star_x, star_y, 5)    sleep(100)
    A dim star falls towards the bright player.
  4. Step 4: Catch it!

    When the star reaches the bottom row, check whether it is in the same column as the player. If it is, that is a catch: add 1 to score.

    New: lines 8, 21 and 22.

    main.pyMicroPython micro:bit editor
    from microbit import *import random player = 2star_x = random.randint(0, 4)star_y = 0tick = 0score = 0 while True:    if button_a.was_pressed() and player > 0:        player = player - 1    if button_b.was_pressed() and player < 4:        player = player + 1     tick = tick + 1    if tick == 5:        tick = 0        star_y = star_y + 1        if star_y == 4:            if star_x == player:                score = score + 1            star_x = random.randint(0, 4)            star_y = 0     display.clear()    display.set_pixel(player, 4, 9)    display.set_pixel(star_x, star_y, 5)    sleep(100)
    The star is right above the player, so this one will be caught.
  5. Step 5: Three lives

    Now the game can end. Every miss costs one of three lives, and the loop changes to while lives > 0:, so it stops when the lives run out. After the loop, show a skull, then scroll the score.

    New: lines 9, 11, 24, 25 and 33 to 36.

    main.pyMicroPython micro:bit editor
    from microbit import *import random player = 2star_x = random.randint(0, 4)star_y = 0tick = 0score = 0lives = 3 while lives > 0:    if button_a.was_pressed() and player > 0:        player = player - 1    if button_b.was_pressed() and player < 4:        player = player + 1     tick = tick + 1    if tick == 5:        tick = 0        star_y = star_y + 1        if star_y == 4:            if star_x == player:                score = score + 1            else:                lives = lives - 1            star_x = random.randint(0, 4)            star_y = 0     display.clear()    display.set_pixel(player, 4, 9)    display.set_pixel(star_x, star_y, 5)    sleep(100) display.show(Image.SKULL)sleep(1000)display.scroll("Score: " + str(score))
    Three misses, and the game is over.
  6. Step 6: Faster and faster

    Make it harder as you get better. speed is how many loops the star waits between rows. Every catch takes 1 off it, down to 2, so the stars fall faster and faster. The test becomes tick >= speed.

    New: lines 10, 19, 25 and 26.

    main.pyMicroPython micro:bit editor
    from microbit import *import random player = 2star_x = random.randint(0, 4)star_y = 0tick = 0score = 0lives = 3speed = 5 while lives > 0:    if button_a.was_pressed() and player > 0:        player = player - 1    if button_b.was_pressed() and player < 4:        player = player + 1     tick = tick + 1    if tick >= speed:        tick = 0        star_y = star_y + 1        if star_y == 4:            if star_x == player:                score = score + 1                if speed > 2:                    speed = speed - 1            else:                lives = lives - 1            star_x = random.randint(0, 4)            star_y = 0     display.clear()    display.set_pixel(player, 4, 9)    display.set_pixel(star_x, star_y, 5)    sleep(100) display.show(Image.SKULL)sleep(1000)display.scroll("Score: " + str(score))
    Each catch makes the next star fall faster.
  7. Step 7: Play the real thing

    Send it to your micro:bit and play. What is your best score? Challenge a friend to beat it.

    Send it, then play Catch the Star on your micro:bit.

Test it

Tick each one when it works. If one does not, look back at the step it comes from.

Stretch challenges

Finished early? Try these. Each one has a hint if you get stuck.

  1. Lives left

    Show how many lives are left: blink the lives as lights along the top row whenever one is lost.

    Show a hint

    After lives = lives - 1, clear the display, light lives pixels in row 0 with a for loop, and sleep(500).

  2. Two stars

    Have two stars falling at once.

    Show a hint

    Give the second star its own star2_x and star2_y, and copy the code that moves and checks the first star.

  3. High score

    Play again after game over, and remember the best score.

    Show a hint

    Put the whole game inside another while True: loop. Keep best outside it, and after each game, if score > best: best = score.

The finished code

Show the whole program
main.pyMicroPython micro:bit editor
from microbit import *import random player = 2star_x = random.randint(0, 4)star_y = 0tick = 0score = 0lives = 3speed = 5 while lives > 0:    if button_a.was_pressed() and player > 0:        player = player - 1    if button_b.was_pressed() and player < 4:        player = player + 1     tick = tick + 1    if tick >= speed:        tick = 0        star_y = star_y + 1        if star_y == 4:            if star_x == player:                score = score + 1                if speed > 2:                    speed = speed - 1            else:                lives = lives - 1            star_x = random.randint(0, 4)            star_y = 0     display.clear()    display.set_pixel(player, 4, 9)    display.set_pixel(star_x, star_y, 5)    sleep(100) display.show(Image.SKULL)sleep(1000)display.scroll("Score: " + str(score))
Notes for the teacher

Timing

  1. 15 minutes: steps 1 and 2. Draw the 5 by 5 grid with its x and y numbers on the board.
  2. 20 minutes: steps 3 and 4. The tick counter is the tricky part: compare it with counting seconds on a clock.
  3. 15 minutes: steps 5 to 7. Leave time to play!

Mistakes to look out for

  1. set_pixel(4, 2, 9) instead of set_pixel(2, 4, 9): x comes first, then y.
  2. A player that wraps off the edge: check the player > 0 and player < 4 tests.
  3. while lives > 0: with lives never going down, because the else is lined up with the wrong if.
  4. A star that never moves: tick == 5 with tick reset in the wrong place.

Going further

This is a game loop: read the input, update the game, draw it, wait, repeat. Every video game works this way, many times a second.