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.
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
whileloop'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
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.xcounts the columns from 0 on the left to 4 on the right, andycounts the rows from 0 at the top to 4 at the bottom. The player starts in the middle of the bottom row.1from microbit import *2 3player = 24 5while True:6 display.clear()7 display.set_pixel(player, 4, 9)8 sleep(100)ReferenceIdeasAPIProjectCatch the Star1from microbit import *2 3player = 24 5while True:6 display.clear()7 display.set_pixel(player, 4, 9)8 sleep(100)Send to micro:bitSaveOpen…SimulatorThe player: one bright light in the middle of the bottom row. 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 > 0stops it going off the left edge, andplayer < 4stops it going off the right.New: lines 6 to 10.
1from microbit import *2 3player = 24 5while True:6 if button_a.was_pressed() and player > 0:7 player = player - 18 if button_b.was_pressed() and player < 4:9 player = player + 110 11 display.clear()12 display.set_pixel(player, 4, 9)13 sleep(100)ReferenceIdeasAPIProjectCatch the Star1from microbit import *2 3player = 24 5while True:6 if button_a.was_pressed() and player > 0:7 player = player - 18 if button_b.was_pressed() and player < 4:9 player = player + 110 11 display.clear()12 display.set_pixel(player, 4, 9)13 sleep(100)Send to micro:bitSaveOpen…SimulatorPress B twice: the player moves to the right edge, and no further. 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
tickcounts 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.
1from microbit import *2import random3 4player = 25star_x = random.randint(0, 4)6star_y = 07tick = 08 9while True:10 if button_a.was_pressed() and player > 0:11 player = player - 112 if button_b.was_pressed() and player < 4:13 player = player + 114 15 tick = tick + 116 if tick == 5:17 tick = 018 star_y = star_y + 119 if star_y == 4:20 star_x = random.randint(0, 4)21 star_y = 022 23 display.clear()24 display.set_pixel(player, 4, 9)25 display.set_pixel(star_x, star_y, 5)26 sleep(100)ReferenceIdeasAPIProjectCatch the Star1from microbit import *2import random3 4player = 25star_x = random.randint(0, 4)6star_y = 07tick = 08 9while True:10 if button_a.was_pressed() and player > 0:11 player = player - 112 if button_b.was_pressed() and player < 4:13 player = player + 1Send to micro:bitSaveOpen…SimulatorA dim star falls towards the bright player. 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.
1from microbit import *2import random3 4player = 25star_x = random.randint(0, 4)6star_y = 07tick = 08score = 09 10while True:11 if button_a.was_pressed() and player > 0:12 player = player - 113 if button_b.was_pressed() and player < 4:14 player = player + 115 16 tick = tick + 117 if tick == 5:18 tick = 019 star_y = star_y + 120 if star_y == 4:21 if star_x == player:22 score = score + 123 star_x = random.randint(0, 4)24 star_y = 025 26 display.clear()27 display.set_pixel(player, 4, 9)28 display.set_pixel(star_x, star_y, 5)29 sleep(100)ReferenceIdeasAPIProjectCatch the Star6star_y = 07tick = 08score = 09 10while True:11 if button_a.was_pressed() and player > 0:12 player = player - 113 if button_b.was_pressed() and player < 4:14 player = player + 115 16 tick = tick + 117 if tick == 5:18 tick = 0Send to micro:bitSaveOpen…SimulatorThe star is right above the player, so this one will be caught. 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.
1from microbit import *2import random3 4player = 25star_x = random.randint(0, 4)6star_y = 07tick = 08score = 09lives = 310 11while lives > 0:12 if button_a.was_pressed() and player > 0:13 player = player - 114 if button_b.was_pressed() and player < 4:15 player = player + 116 17 tick = tick + 118 if tick == 5:19 tick = 020 star_y = star_y + 121 if star_y == 4:22 if star_x == player:23 score = score + 124 else:25 lives = lives - 126 star_x = random.randint(0, 4)27 star_y = 028 29 display.clear()30 display.set_pixel(player, 4, 9)31 display.set_pixel(star_x, star_y, 5)32 sleep(100)33 34display.show(Image.SKULL)35sleep(1000)36display.scroll("Score: " + str(score))ReferenceIdeasAPIProjectCatch the Star7tick = 08score = 09lives = 310 11while lives > 0:12 if button_a.was_pressed() and player > 0:13 player = player - 114 if button_b.was_pressed() and player < 4:15 player = player + 116 17 tick = tick + 118 if tick == 5:19 tick = 0Send to micro:bitSaveOpen…SimulatorThree misses, and the game is over. Step 6: Faster and faster
Make it harder as you get better.
speedis 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 becomestick >= speed.New: lines 10, 19, 25 and 26.
1from microbit import *2import random3 4player = 25star_x = random.randint(0, 4)6star_y = 07tick = 08score = 09lives = 310speed = 511 12while lives > 0:13 if button_a.was_pressed() and player > 0:14 player = player - 115 if button_b.was_pressed() and player < 4:16 player = player + 117 18 tick = tick + 119 if tick >= speed:20 tick = 021 star_y = star_y + 122 if star_y == 4:23 if star_x == player:24 score = score + 125 if speed > 2:26 speed = speed - 127 else:28 lives = lives - 129 star_x = random.randint(0, 4)30 star_y = 031 32 display.clear()33 display.set_pixel(player, 4, 9)34 display.set_pixel(star_x, star_y, 5)35 sleep(100)36 37display.show(Image.SKULL)38sleep(1000)39display.scroll("Score: " + str(score))ReferenceIdeasAPIProjectCatch the Star8score = 09lives = 310speed = 511 12while lives > 0:13 if button_a.was_pressed() and player > 0:14 player = player - 115 if button_b.was_pressed() and player < 4:16 player = player + 117 18 tick = tick + 119 if tick >= speed:20 tick = 0Send to micro:bitSaveOpen…SimulatorEach catch makes the next star fall faster. 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.
ReferenceIdeasAPIProjectCatch the Star1from microbit import *2import random3 4player = 25star_x = random.randint(0, 4)6star_y = 07tick = 08score = 09lives = 310speed = 511 12while lives > 0:13 if button_a.was_pressed() and player > 0:Send to micro:bitSaveOpen…SimulatorSend 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.
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, lightlivespixels in row 0 with aforloop, andsleep(500).Two stars
Have two stars falling at once.
Show a hint
Give the second star its own
star2_xandstar2_y, and copy the code that moves and checks the first star.High score
Play again after game over, and remember the best score.
Show a hint
Put the whole game inside another
while True:loop. Keepbestoutside it, and after each game,if score > best: best = score.
The finished code
Show the whole program
1from microbit import *2import random3 4player = 25star_x = random.randint(0, 4)6star_y = 07tick = 08score = 09lives = 310speed = 511 12while lives > 0:13 if button_a.was_pressed() and player > 0:14 player = player - 115 if button_b.was_pressed() and player < 4:16 player = player + 117 18 tick = tick + 119 if tick >= speed:20 tick = 021 star_y = star_y + 122 if star_y == 4:23 if star_x == player:24 score = score + 125 if speed > 2:26 speed = speed - 127 else:28 lives = lives - 129 star_x = random.randint(0, 4)30 star_y = 031 32 display.clear()33 display.set_pixel(player, 4, 9)34 display.set_pixel(star_x, star_y, 5)35 sleep(100)36 37display.show(Image.SKULL)38sleep(1000)39display.scroll("Score: " + str(score))Notes for the teacher
Timing
- 15 minutes: steps 1 and 2. Draw the 5 by 5 grid with its x and y numbers on the board.
- 20 minutes: steps 3 and 4. The
tickcounter is the tricky part: compare it with counting seconds on a clock. - 15 minutes: steps 5 to 7. Leave time to play!
Mistakes to look out for
set_pixel(4, 2, 9)instead ofset_pixel(2, 4, 9): x comes first, then y.- A player that wraps off the edge: check the
player > 0andplayer < 4tests. while lives > 0:with lives never going down, because theelseis lined up with the wrongif.- A star that never moves:
tick == 5withtickreset 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.