Fire Simulator

Challenge

Experiment with the Image class, create a blank image then build a simulated fire by creating a layer of pixels of random light intensity along the bottom of the screen. To simulate the flames moving upwards, shift the image up the screen as time passes, making room for new random layers at the bottom of the screen. As the image moves up the screen, it should fade out (intensity -> 0).

Used with thanks from: http://blog.withcode.uk/2016/06/microbit-python-tutorial-shake-n-burn-fire-simulator/

Extensions

Eventually the fire will “burn out” as the intensity decreases gradually. Use the accelerometer to wake the fire up by shaking the microbit.

Example Solution


from microbit import *
import random

fire = Image("00000:" * 5) # create an empty image

intensity = 0.5 # start with the fire at medium intensity

while True:
    display.show(fire)
    sleep(100)

    # shake to stoke the fire
    if accelerometer.was_gesture("shake"):
        intensity = 1

    # shift the image up and fade it slightly
    fire = fire.shift_up(1) * intensity

    # let the fire burn down a little (reduce the intensity)
    intensity = intensity * 0.98

    # choose random brightness for bottom row of fire
    for x in range(5):
        fire.set_pixel(x, 4, random.randint(0,9))