How does it work: Part 1: Getting ready?

Let’s break down the code into chunks and go through what each section does:

# Doodle jump for BBC micro:bit
# blog.withcode.uk

# Use A and B to steer

from microbit import *
import random

The first few lines are comments that don’t affect the way the code works. They’re just there to provide extra information for anyone who wants to use or edit the code in future.

Lines 6 and 7 import the microbit and random modules.

Pro tip: Line 6 imports all of the functionality of the microbit module into the global namespace, which makes it easier to write the rest of the code but is considered bad practice. If we were writing a larger project, it’d be better to replace line 6 with import microbitwhich means all of the stuff inside the microbit module (like display, the buttons and all of the pins) don’t get confused with any other variables we might define or use. If we did this, we’d have to replace things like display  with microbit.displayand Image  with microbit.Imageevery time we see them in the rest of the code.

# Doodle jump for BBC micro:bit
# blog.withcode.uk

# Use A and B to steer

from microbit import *
import random

# force a value to be between a minimum and maximum
def check_bounds(val, min, max):
    if val < min:
        val = min
    if val > max:
        val = max
    return val

Line 10 defines a function called check_bounds . This function has three parameters: val , min  and max . We’re going to use this function on lines 52 and 53:

    # make sure doodle x and y coordinates are between 0 and 99
    dy = check_bounds(dy, 0, 99)
    dx = check_bounds(dx, 0, 99)

In both cases, check_bounds is used to make sure a number is somewhere between a minimum and maximum value. dx  is the x coordinate of our doodle jump alien and dy  is the y coordinate of our doodle jump alien.

# global variables
dx = 50             # doodle x coordinate (0-99)
dy = 50             # doodle y coordinate (0-99)
score = 0           # score
speed_y = 0         # vertical speed
frame = 0           # frame counter

# constants
GAP_BETWEEN_PLATFORMS = 2   # distance between platforms
SCORE_BETWEEN_LEVELS = 10   # number of points you have to score before getting to next level

Lines 18-22 are the variables that store the data that our game needs to run. They’re global variables because they can be accessed or changed from anywhere in the program.

Variables store data that may change as the program runs. Constants store data that never changes while the program runs. Both variables and constants let you give names to data which can make your code easier to read. In python, you’re supposed to write constants in CAPITAL_LETTERS_SEPARATED_BY_UNDERSCORES and variables should be written in lowercase_separated_by_underscores.

In some programming languages, you’ll get a run time error if you try to change the value of a constant whilst the program is running. Python trusts the programmer enough to let you change a constant if you really want to or have to.

We’ve used two constants so that you can easily tweak them to change the way the game works before you run the code.

# initial background image (with platforms)
background = Image("03330:00000:00033:00000:33333")

# time delay for each level of difficulty
levels = [30, 25, 20, 18, 15, 12, 10]
current_level = 0

All of the platforms that our doodle jump alien is going to jump up from are going to be dark red (the LEDs are going to be faded to 3/9 brightness). Line 29 creates an image that has three platforms ready to show when the game starts.

Line 32 is a list of numbers that determines how the game changes in each level. In level 1, the main game loop should repeat 30 times before the platforms scroll down the screen. In level 2, it should only loop 25 times before scrolling. This means that as you go through the levels, the game speeds up and gets harder.

Line 33 makes the game start on level 1 even though it sets current_level  to  0  . This is because python starts counting positions in a list from 0 rather than 1 (levels[0]  is 30  and levels[1]is 25 )

The next page shows how the main game loop works