Lose one life at a time
Add a variable which will keep track of the last time we took damage. Calling the global function microbit.running_time()
will return us the time (in microseconds) since the Microbit powered on.
Also create a function which checks if we have been damaged recently. This is so we can avoid losing all our life at once. Furthermore we increase our life to 3.
In our If
statement we call the function to ask for whether we have been damaged recently.
from microbit import *
last_damaged_at = microbit.running_time()
life = 3
def shaking():
gesture = accelerometer.current_gesture()
return gesture == "shake"
def not_damaged_recently():
now = microbit.running_time()
time_difference = last_damaged_at - now
return time_difference > 1000
while life > 0:
gesture = accelerometer.current_gesture()
if shaking() && not_damaged_recently():
life = life - 1
last_damaged_at = microbit.running_time()
display.show(Image.ANGRY)
else:
display.show(Image.HAPPY)
display.show(Image.SKULL)