Flappy Bird on micro:bit

Create a full-fledged classic flappy bird game on your micro:bit


  • 1 x BBC micro:bit
  • 1 x Micro USB Cable


We’re going to create a full-fledged classic flappy bird game on your micro:bit, playable for 9 days to 90 years old.

We'll learn:
(a) To code with Python for the micro:bit, to
• Import the micro:bit library to Python
• Display text and images on the micro:bit
• Detect and react to button presses on the micro:bit

(b) Python concepts - data types, modulo, variables, randomness, conditionals, loops.


Coding in Python

(a) On your laptop, go to the official micro:bit Python editor at https://python.microbit.org/v/2.0.

(b) Delete the automatic sample code that appears.

(c) In the following steps, we will:
• Start a countdown on start
• Create and move the bird by pressing button A
• Create pipes for the bird to fly between
• Track the number of pipes the bird passes
• Set game over when the bird collides into a pipe


(a) Import the default micro:bit library to write code for the micro:bit.

(b) Import randint from the random library to draw the pipes randomly later.

(c) Show a 'READY' message and begin countdown.
• To customize the scrolling rate of 'READY' across the micro:bit screen, pass a 'delay' parameter, eg. display.scroll(“READY”, delay = 200) scrolls twice as fast as the default delay of 400.

(d) sleep(1000) between the countdown flashes each countdown number for 1000ms (1s).

(e) Clear the welcome message.


Create the Bird
(a) Create a variable 'y' to store the y-coordinates of the bird. The classic Flappy Bird game only allows the bird to move up and down, thus there is no need to create a variable for the x-coordinate of the bird which remains constant at 1 (2nd LED from left).

(b) For more realistic bird-flapping, split the 5 LED rows into 100 different positions - from y = 0 (top) to y = 99 (bottom).

(c) Set y = 50 so the bird starts right in the middle.


Display the Bird
(a) Create a 'while' loop repeating everything indented under it. This is similar to 'forever' on MakeCode.

(b) As there are 100 positions and 5 LED rows, divide y by 20.
• 'led_y' is the actual y-coordinate of the bird on the micro:bit, which is 2 now (50 / 20 and rounded down to integer).

(c) Light up this LED, at the 2nd column (x=1), 3rd row (led_y = 2) with maximum brightness (9).

(d) Pause for 20ms (0.2s) to prevent crashing.


Run the Code
(a) Let's check our code! Plug the micro USB cable from your laptop into the top of the micro:bit.

(b) On the Python editor, click 'Connect' icon, select your micro:bit and click 'Connect'.

(c) Click the 'Flash' icon. Wait for the orange light to stop flashing on the micro:bit.

(d) You should see a countdown and a single LED lighting up on your micro:bit screen.


Leaving the Nest
(a) Create 'speed' to store the bird's rate of descent. Set to 0 on start.

(b) Shift display.clear() under the 'while' loop to clear the countdown and the bird's old position.

(c) For more realistic movement, set maximum 'speed' to 2.

(d) y += speed sets a new y-coordinate for the bird, increasing by 'speed' (rate of descent) every time.

(e) Set max y to 99, min y to 0, to keep the bird within the micro:bit screen.

(f) Adding (d) and (e) under the 'while' loop continually updates the bird's position (every 20ms).


Defying Gravity
(a) If button A is pressed, give the bird a falling and flapping motion - bring it up, reset falling rate, and accelerate it back down to the ground. We can do so by setting 'speed' to -8.

(b) Create 'score' to store the number of pipes that the bird has flown past. Set it to 0 at the start. We will increase it accordingly later.

(c) Show the player's points stored in 'score' when button B is pressed.


Test the Code
(a) Download the code to your micro:bit, like in step 6 earlier.

(b) Your micro:bit screen should now:
• Show and clear a welcome message and countdown
• Show a single LED pixel (the bird) lighting up then falling down
• Flap the bird upwards when button A is pressed
• Show the score when button B is pressed (should show 0 now)


Create the Pipe
(a) Create a 'make_pipe' function.

(b) Create a custom pipe image with last LED column lighting at brightness 4. Store this in 'i'.

(c) Blast 2 holes next to each other in the pipe image (i) created, by setting brightness to 0 at y = gap and y = gap+1. As 'gap' is a random integer from 0 to 3, these 2 holes created are randomised, next to each other.

(d) Return 'i', and assign 'i' to 'make_pipe'.

(e) Under the while loop, display the pipe with the hole.


Frame Rate
(a) Set up game constants in uppercase (Python convention).

(b) Set DELAY to 20ms between each frame. 'frame' increases by 1 every 20ms (0.02s) (line 37).

(c) Change sleep() timing (line 62) to sleep(DELAY).

(d) Set 'frame' variable to 0 on start.

(e) Set time for the wall to shift to the left, to 0.4s (20 frames).

(f) Set time between a new wall appearing to 0.2s (100 frames).

(g) Set time between the score increasing to the same value as (e), so passing each wall adds one to the score.

(h) To increase difficulty, you can adjust (e) and (f) to reduce time between the walls.


Pipe Dreams
Now, let's decide when to move and create a new wall, and increase the score.

Add these under the 'while' loop to check every 20ms:
(a) If 'frame' is divisible by 'FRAMES_PER_WALL_SHIFT' (remainder = 0), move current pipe left. This allows the pipes to move every 0.4s. You can customize this to increase difficulty.

(b) Every 100 frames (2s), create a new pipe with make_pipe() for i.

(c) For every 2s the bird travels, add 1 to 'score'. 2s is the time for the bird to pass each wall.


Quick Check
(a) Just to check that everything is running normally, download the code to your micro:bit, like in steps 6 and 9 earlier.

(b) Your micro:bit screen should:
• Show and clear a welcome message and countdown
• Show a single LED pixel (the bird) lighting up then falling down
• Flap the bird upwards when button A is pressed
• Show score when button B is pressed
• Show walls moving past randomly every 2s

Yay, we’re nearly done now!


Collision Course
Last step - now, we just need to add a collision reaction!

(a) 'i.get_pixel' returns the LED brightness value at 'i'.

(b) If there is a pipe at led_y where the bird is, 'i.get_pixel' returns 4 (brightness of the wall), which is not 0. This means that the bird has collided into the pipe.

(c) If so, show a sad image for 500ms (0.5s), display the player's score, and 'break' out of the 'while' loop to end the game. Game Over!


(a) And we're done, congratulations! Don't forget to connect your micro:bit to your laptop and download the code to your micro:bit.

(b) Take note of the countdown once the code is downloaded. Press button A to flap the bird upwards!

(c) As an extension, you can also add a game loop, to play again without resetting the device.
• Hint: change the while loop’s requirements from True to a certain variable, eg. play_again function, which can be changed with the press of a button.

(d) Have fun playing, enjoy!

Downloads: Flappy Bird Microbit.hex Flappy Bird Microbit.py

Made this activity? Good Job! Now spread the word. Share a photo of your creation on social media with #letsgethacking #FlappyBirdonmicro:bit