In this stage we’re going to make the flames rise up the screen, fading out as they rise.
- Add the highlighted code above your
from microbit import * import random # create an empty image i = Image("00000:"*5) # start with the fire at medium intensity intensity = 0.5 # keep looping while True: # show the image and wait display.show(i) sleep(100) # shift the image up and fade it slightly i = i.shift_up(1) * intensity # let the fire burn down a little (reduce the intensity) intensity *= 0.98 # choose random brightness for bottom row of fire for x in range(5): i.set_pixel(x, 4, random.randint(0,9))
Line 17 does two things. Firstly, i.shift_up(1) moves the flame image up by one LED and then * intensity multiplies that new image by whatever value is currently stored in the variable intensity , which stores a value between 0.0 and 1.0.
When you multiply a number by something less than 1, that number gets smaller: Anything multiplied by 0 is always 0. Anything multiplied by 1 stays the same.
We’re not multiplying a number by something less than 1, we’re multiplying an image by a number less than 1 ( i is an image and i.shift_up(1) is that image shifted up by one LED / pixel). This makes the brightness of the image for each LED / pixel slightly less bright: effectively dimming the flames as they rise up.
Experiment by changing 0.98 on line 20 to any value between 0.0 and 1.0 and seeing what happens when you run the simulator.
You could also try testing this value outside of it’s expected bounds: try 1.5 or -0.3. Do you get any error messages? What happens and why?
The next page makes it so that you can shake the micro:bit to stoke up the fire when it burns down too low.