Flappy bird Micro:bit python tutorial for beginners

Step 2: Display the bird

In case you’ve been living on another planet for the last few years and have never played flappy bird: the bird itself doesn’t move left of right – only up and down.

We need to keep track of where it is vertically (the y axis). There are only 5 rows of LEDs on the micro:bit screen but in order to make the bird flap up and down more realistically, we’re going to pretend that there are 100 different positions the bird can be on the y axis and then scale this down to display it on the LED grid:

Scaling down y coordinates to fit on the 5x5 LED screen
Scaling down y coordinates to fit on the 5×5 LED screen
  • Write  y = 50 at the bottom of your code.

    This makes a new variable called y.

    A variable stores some data that might change when the program runs. This variable stores the y coordinate (from 0: top to 99:bottom) and we set it to 50 at the start of the game.

  • Add the line  led_y = int(y / 20)  to the bottom of your code.

    This makes a new variable called led_y which stores a number from 0 – 4, telling us which row of the LED screen to light up.

    y / 20 divides the value stored in the y variable by 20 so that we can scale down the bird coordinate to fit on the LED screen.

    The int() function rounds the result of y / 20 down to a whole number (an integer is a whole number with no decimal places)

  • Add the line  display.set_pixel(1, led_y, 9)  to the bottom of your code.

    This turns on one pixel which will represent our bird.

    There are three parameters for set_pixel: x, y and brightness. We’re setting the x coordinate to 1. The x coordinate for the first column is 0, so our bird will be on the second column.

    We’re setting the y coordinate in set_pixel to led_y. led_y will be 2 (y was 50 and we divided that by 20 and rounded down). This means the bird will be on the third row down (the first row is row 0)

    The 9 as the last parameter in set_pixel is the brightness. This can be any number from 0 to 9 with 0 meaning completely off and 9 meaning as bright as it’ll go. We’re going to have a bright LED for the bird and dim LEDs for the pipes that you’ve got to try to avoid.

  • Your code should now look like this. The new lines are highlighted.
    from microbit import *
    display.scroll("Get ready...")
    y = 50
    led_y = int(y / 20)
    display.set_pixel(1, led_y, 9)

Debug it with code

The code below contains some common mistakes. See if you can find and fix them all.

18 thoughts on “Flappy bird Micro:bit python tutorial for beginners

    • June 10, 2016 at 3:31 pm

      Hi Darryl,

      Thanks – you’re right – sorry about that. I made a mistake when making micro:bit python simulator for the Image.shift_left() function – it ignored the parameter, which your comment has helped me fix: cheers!

      I’ve changed line 71 from i = i.shift_left() to i = i.shift_left(1) which should fix the problem when you test it on a micro:bit.

  • Pingback: Molla -robotin prototyyppi | Innokas-verkoston blogi

  • August 25, 2018 at 10:58 am

    I have a name error on line 22. It says name not defined.

    • August 25, 2018 at 11:50 am

      Thanks for getting in touch, sorry about the error. Please could you post a link to your code or let me know which example you’re getting the error with?

      • August 25, 2018 at 12:26 pm

        I’ve got the code https://create.withcode.uk/python/58B

        It is at the section of
        def make_pipe():
        i = Image("00003:00003:00003:00003:00003")
        gap = random.randint(0,3)
        i.set_pixel(4, gap, 0)
        i.set_pixel(4, gap+1, 0)
        return i

        The micro bit shows 'name error' ' name not defined'
        Hope this helps

  • August 25, 2018 at 12:45 pm

    I’ve edited your comment to replace the code with a link if that’s ok? – posting python directly on here gets rid of indentation and swaps some characters like quotation marks. I can’t seem to get the same error as you on the simulator or a micro:bit. Please could you check the link and see if it works for you or if the code matches yours?

    • August 26, 2018 at 2:13 am

      I’ve tested the code you post on micro bit and it seems like it’s still having the same error message.
      Line 17 ‘name error’ name ‘random’ is not defined.

    • August 26, 2018 at 2:19 am

      Sorry my misunderstanding with your comment, i thought you fixed the issue. Yes the code matches my original code that I’ve got.

  • August 26, 2018 at 11:27 am

    Sorry-that’s very frustrating. It runs on a microbit without an error for me. Are you getting the hex file from create.withcode.uk when you run the simulator or are you using a different editor? Could you try clearing your browser cache in case you’ve got an old version of the runtime stored. You’re the second person to say there’s a problem with the random module so I’d like to track down what the issue is, but I can’t replicate the problem on any browser. Thanks for your patience and help.

    • August 26, 2018 at 1:04 pm

      Hi, I tried clearing the cache and the same error occurs again. I’m using https://python.microbit.org/v/1 to edit my coding. Normally I would save the file from as a .py file and drag the file into the Micro:Bit folder.

      Hope those information helps

      • August 26, 2018 at 7:36 pm

        Thanks – that’s helpful.

        The micro:bit can’t run .py files directly: micro:bits need to be flashed with .hex files which contain both your python code and the micropython runtime which tell the micro:bit how to interpret your code. In the https://python.microbit.org/v/1 editor you’re using, you need to press the download button to create a hex file. That’s the one you then drag into the micro:bit folder. In create.withcode.uk, press ctrl + enter to run your code and you’ll see a link above the simulator that will let you download the hex file.

        Hope that helps.


Leave a Reply