Doodle jump: microbit python game tutorial

Doodle jump: microbit python game tutorial

This tutorial will talk you through how to write, test, debug and improve the python code for a doodle jump style game on a BBC micro:bit.

Challenging
Warning: this isn’t easy

This microbit python game tutorial is possibly a little too advanced for beginners so have a look at some of the other tutorials if you’re just getting started or jump straight in here if you’re feeling confident.

Doodle jump is a brilliantly simple but infuriatingly addictive game where you have to control a green alien by moving it left and right to jump up to the next platform, as the world falls away beneath it. The original game won an Apple design award and it quickly took the gaming world by storm. You can play it here (it’s likely to be blocked if you’re viewing this tutorial page in school). The challenge of this tutorial is to attempt to scale down the game so it still works on a 5×5 LED screen on the BBC micro:bit whilst still being fun to play.

 

Try it with code
Try the code

Below is a sample of the doodle jump game that we’ll create using this tutorial.

Press Ctrl + Enter to run the code, or click on the run button that appears when you click on the {+} button in the bottom right corner of the code editor.

Press button A to move left and button B to move right.

If you want to test the game on an actual micro:bit, plug it into your computer, run the code and click on Download HEX and save that file onto your micro:bit.

microbit python tutorial: shake ‘n’ burn fire simulator

microbit python tutorial: shake ‘n’ burn fire simulator

This microbit python tutorial talks you through how to write python code that simulates fire on your micro:bit. It burns down gradually until you shake it to stoke the fire back to life.

It’s written for beginners so you don’t need any experience writing python code. It covers:

Concepts you'll understand
Concepts you’ll understand

Concepts:

  • Loops: repeating code with while and for loops
  • Selection: only running certain parts of the code if a condition is met
  • Images: how images can be represented as data in memory before being displayed on a screen
  • Operators: how the multiplication operator (*) works with strings, numbers and images

 

 

Skills covered in this tutorial
Skills covered in this tutorial

Skills:

  • How to change each pixel of an image on the micro:bit screen
  • How to use the accelerometer to check if the micro:bit has been shaken
  • How to choose a random number
  • How to fade and shift an image on the micro:bit screen

The next page shows a simulation of the code we’ll create in this tutorial.

Python microbit basics: buttons and touch pins

Python microbit basics: buttons and touch pins

This beginners tutorial talks you through how to write python programs for a micro:bit that can respond when you press a button or touch one of the input pins along the bottom of the device.

Try it with code
Try the code: Python microbit basics: buttons and touch pins

The code below turns a micro:bit into a counter: press button A to count down, button B to count up and touch pin 0 to reset back to 0. Try it out and then read through how it works.

You could use this to keep score in a competition, especially if you use two or more micro:bits – one for each team.

After line 3 imports the micro:bit module so that python can start controlling the inputs and outputs, line 5 and 6 create two new variables: counter and new_value :

# https://blog.withcode.uk/python-microbit-basics-buttons-and-touch-pins
# micro:bit counter
from microbit import *

counter = 0
new_value = 0

# keep looping forever
while True:
    
    # decrease by 1 if button A was pressed
    if button_a.was_pressed():
        new_value = counter - 1
        
    # increase by 1 if button B  was pressed
    if button_b.was_pressed():
        new_value = counter + 1
        
    # reset to 0 if you touch pin 0
    if pin0.is_touched():
        new_value = 0
    
    # stop counter from going less than 0
    if new_value < 0:
        new_value = 0
        
    # scroll new value if it's changed
    if new_value != counter:
        counter = new_value
        display.scroll(str(counter))
        
    # avoid hogging all the CPU time / using too much power
    sleep(50)

The variable counteris used to store the number that is displayed on the micro:bit at any given time.

The variable new_valueis used to detect if the number has changed so that the new number can be scrolled on the screen.

Both variables store whole numbers only (e.g. 1, 2 or 3, not 1.4, 1.7 or 3.8). We call these types of numbers integers

# https://blog.withcode.uk/python-microbit-basics-buttons-and-touch-pins
# micro:bit counter
from microbit import *

counter = 0
new_value = 0

# keep looping forever
while True:
    
    # decrease by 1 if button A was pressed
    if button_a.was_pressed():
        new_value = counter - 1
        
    # increase by 1 if button B  was pressed
    if button_b.was_pressed():
        new_value = counter + 1
        
    # reset to 0 if you touch pin 0
    if pin0.is_touched():
        new_value = 0
    
    # stop counter from going less than 0
    if new_value < 0:
        new_value = 0
        
    # scroll new value if it's changed
    if new_value != counter:
        counter = new_value
        display.scroll(str(counter))
        
    # avoid hogging all the CPU time / using too much power
    sleep(50)

Lines 9 makes all of the lines that are indented underneath it (lines 10-33) repeat forever.

You can see on line 12 and 16 how to check if a button has been pressed. There are two ways of doing this: button_a.is_pressed()checks to see if the button is currently being pressed at that exact moment in time whereas button_a.was_pressed()checks to see if the button has been pressed at all since last time you checked (or since the program started).

 

Line 20 checks to see if someone is currently touching pin 0. You can test this in the simulator using the I/O pins tab when you run the code in your browser:

How to test the I/O pins one the micro:bit simulator
How to test the I/O pins one the micro:bit simulator

There are 20 pins on the micro:bit that run along the bottom edge of the device. You can find out about each of them on the micropython documentation page but there are four types:

Analog pins: You can input (sense) a value from -1024 to 1024 or output (send) a value in the same range

Digital pins: You can input (sense) if they’re turned on / off, or output (send) an on / off signal to them.

Power pins: These are connected to the 3v power supply and can be used for powering other gadgets like additional LEDs

Touch pins: These are the easiest to use – they can detect the change in capacitance that happens when a human finger touches them so you can use them as touch sensors.

Pins 0, 1 and 2 can be used as touch pins. These are the larger, numbered pins along the bottom of a micro:bit.

# https://blog.withcode.uk/python-microbit-basics-buttons-and-touch-pins
# micro:bit counter
from microbit import *

counter = 0
new_value = 0

# keep looping forever
while True:
    
    # decrease by 1 if button A was pressed
    if button_a.was_pressed():
        new_value = counter - 1
        
    # increase by 1 if button B  was pressed
    if button_b.was_pressed():
        new_value = counter + 1
        
    # reset to 0 if you touch pin 0
    if pin0.is_touched():
        new_value = 0
    
    # stop counter from going less than 0
    if new_value < 0:
        new_value = 0
        
    # scroll new value if it's changed
    if new_value != counter:
        counter = new_value
        display.scroll(str(counter))
        
    # avoid hogging all the CPU time / using too much power
    sleep(50)

Lines 23-25 stop the counter value from going below 0 if you keep pressing button a.

Lines 28-30 might take some explaining: if new_value != counter: checks to see if the new_valueis not equal to counter , which will happen if you’ve just pressed one of the buttons.

display.scroll(...)expects its parameter (the value in brackets) to be a string (which is one or more letters / digits / characters). counter is an integer (whole number) rather than a string so str(counter)converts the integer into a string so it’ll work with display.scrollproperly.

 

sleep(50)  on line 33 isn’t necessary but it’s a good idea to have it. If you didn’t and you tried to test the code in the simulator, your browser will crash. Testing it on a micro:bit would work but it’d use more battery power than necessary. In both cases, the problems are caused by the while loop repeating so quickly that the CPU is always busy. sleep(50)  just pauses the program for 50ms, which isn’t long, but long enough for your browser / micro:bit to have a breather.