microbit python tutorial: shake ‘n’ burn fire simulator

Getting started

  1. Click here to start writing your python code using create.withcode.uk

    Create.withcode.uk is designed to let you write, run, debug and share python code without having to download any software – you can test it in your browser. It’s not quite the full version of python – the code can’t access any of your files or do any damage to your computer  – but it’s good enough to get started learning how to write python code.

  2. Delete all of the code that’s in the editor and replace it with:
    from microbit import *
    import random

    These lines tell python to import the microbit module and the random module. This lets you write code that can control the micro:bit and choose random numbers. We’ll need the random numbers to simulate the fire on the micro:bit screen.

  3. Run the code to test it by pressing the green run button in the bottom right of the screen.

    You can also run the code by holding down Ctrl and pressing Enter. The code wont do much yet but line 1 will load and display the micro:bit simulator


  4. Add the following lines of code to the bottom of your program:
    from microbit import *
    import random
    # create an empty image
    i = Image("00000:"*5)
    # start with the fire at medium intensity
    intensity = 0.5

    Lines 4 and 7 both start with a #  which makes them comments. They don’t make the program do anything so you don’t have to include them, but using comments is a really good idea as it makes your code easier to understand so that mistakes are easier to find and fix in the future.

    We’re going to use 2 variables in our program. Variables store data that may change whilst the program runs. The first variable is called i  which is an image we can display on the micro:bit screen.

    We need to set the brightness of each LED on the micro:bit screen, which has 5 rows and 5 columns of LEDs. Image("99999:99999:99999:99999:99999") would mean all of the LEDs set to maximum brightness .  Click here for more examples and detail on how images on a micro:bit in python work.

    Image("00000:"*5)  is the same as Image("00000:00000:00000:00000:00000")  which will turn all of the LEDs fully off.

    "00000:"  is a string (which means it’s text or numbers surrounded by quotation marks). The * 5  multiples the string 5 times which python does by repeating it 5 times.

How the intensity variable affects the fire simulation
How the intensity variable affects the fire simulation

The variable intensity on line 8 is a real value (which means it can store realistic numbers with a decimal point like 0.3 or 2.876 as well as integer values like 1, 3 or 7). We’ll use intensity to store how ‘hot’ the fire is (when you shake the micro:bit, the fire will get ‘hotter’ and burn higher and brighter):

The next page shows you how to start displaying the fire on the screen.

Leave a Reply

Your email address will not be published. Required fields are marked *