Flappy bird Micro:bit python tutorial for beginners

Step 3: Simulate gravity

In this stage we’re going to start simulating gravity so that the bird falls down towards the ground.

Simulating gravity
Simulating gravity
  • Make the code that displays the bird keep looping round as long as the game is being played. Make the changes on the highlighted lines below:
    from microbit import *
    
    display.scroll("Get ready...")
    
    y = 50
    while True:
        led_y = int(y / 20)
        display.set_pixel(1, led_y, 9)
        sleep(20)

    The  while True: line tells python to keep on repeating a block of code. The code to repeat is the code that is indented (moved across to the right). You can indent one or more line at a time by selecting it and pressing tab (which is usually to the left of the Q button on your keyboard – it looks like an arrow). You can un-indent lines (move them back to the left) by selecting them and pressing Shift + Tab.

    The sleep(20) tells python to wait for 20ms every time the game loops. This stops the CPU from working too hard and slows your game down to a manageable speed. If you didn’t have this line your micro:bit would use more battery power than necessary or your simulator would crash the browser.

     

  • At the moment, the bird just hangs in mid-air. We need to simulate gravity so that it falls towards the ground. Before your while loop (above line 6), add the line:  speed = 1 . Then, as the first line of the while loop (under line 7) add the line:  y += speed . Make sure that this line is indented so that it’s lined up with the line below.

    y += speed means that each time the loop goes round, the bird’s y coordinate will increase by the value of speed. As we’ve set speed to 1, this means y will increase by 1 each time the loop repeats.

    If you run this code, you’ll see there’s two problems with it:

    1) The bird (or dot!) does fall slowly towards the ground, but the previous position is never erased so you end up with a line rather than a dot.

    Fix this by adding the line   display.clear() as the first line of the while loop (line 8). This clears the screen each time the program loops round so you can draw a fresh dot each time.

    2) When the bird reaches the ground it keeps falling so the program crashes when the micro:bit attempts to draw the dot on row 6 (which doesn’t exist).

    Fix this by adding these lines under y += speed:

        if y > 99:
            y = 99

    This limits the maximum y coordinate to 99.

     

Your code should look like this so far, with the new lines highlighted:

from microbit import *

display.scroll("Get ready...")

y = 50
speed = 1
while True:
    display.clear()
    y += speed
    if y > 99:
        y = 99
    led_y = int(y / 20)
    display.set_pixel(1, led_y, 9)
    sleep(20)

This isn’t particularly realistic. We’ve assumed that the bird should fall to the ground at a constant speed (which we’ve set to 1). In reality, gravity causes objects to accelerate towards the ground. So, let’s make the speed start at 0 then increase, up to a maximum of 2 (you might have covered terminal velocity in science – that’s what this is).

Try it with code
Gravity and terminal velocity

Try out the code below:

Curious computing
Investigate the source code and spot the changes

I’ve made some small but important changes. See if you can:

  1. Find the lines that are comments

    Comments are part of the code that don’t actually do anything other than make it more readable for other programmers. They start with a #. You can have a whole line that’s a comment or put a # after some code and make the rest of the line into a comment.

  2. Find the lines of code that stop the speed from going above 2

    Notice that the line under the if statement is indented. A general rule to help you with debugging is that whenever you see a line that ends with a colon (:), the next line should be indented.

  3. Find the lines of code that stop the bird from disappearing off the top of the screen.

    This isn’t necessary yet, as the bird only ever falls downward, but it will be important in the next step when we start making the bird flap upwards.

 

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

    • June 10, 2016 at 3:31 pm
      Permalink

      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.

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

  • August 25, 2018 at 10:58 am
    Permalink

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

    Reply
    • August 25, 2018 at 11:50 am
      Permalink

      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?

      Reply
      • August 25, 2018 at 12:26 pm
        Permalink

        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
        Thanks

        Reply
  • August 25, 2018 at 12:45 pm
    Permalink

    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?

    Reply
    • August 26, 2018 at 2:13 am
      Permalink

      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.

      Reply
    • August 26, 2018 at 2:19 am
      Permalink

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

      Reply
  • August 26, 2018 at 11:27 am
    Permalink

    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.

    Reply
    • August 26, 2018 at 1:04 pm
      Permalink

      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

      Reply
      • August 26, 2018 at 7:36 pm
        Permalink

        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.

        Reply

Leave a Reply