Catch

Microbit Python English # 1

We are making a game of Catch in Micobit in Python
The game is about having a microbit which loses life when you get shaken. Meanwhile your goal is to shake the enemy's microbit.

You will need https://python.microbit.org/v/2.0 as editor.

You can use WebUSB to flash your microbit directly from this page, see
https://support.microbit.org/support/solutions/articles/19000084059-using-web-usb-with-the-apps-and-editors

Documentation for MicroPython can be found on https://microbit-micropython.readthedocs.io/en/latest/
From here we are using Gestures and Images among others

Angry on Shake

We are making a program which 1. imports the microbit library 2. checks if there is a gesture 3. if the gesture is a shake, we show an angry image

from microbit import *

while True:
    gesture = accelerometer.current_gesture()

    if gesture == "shake":
        display.show(Image.ANGRY)

Happy when Microbit not shaking

We add logic so that if the microbit is not shaking, we show it a happy image

from microbit import *

while True:
    gesture = accelerometer.current_gesture()

    if gesture == "shake":
        display.show(Image.ANGRY)
    else:
        display.show(Image.HAPPY)

Move shake logic into own method

Create a function that contains the logic for detecting a shake gesture. We use this in our if statement to make it more readable.

from microbit import *
life = 1

def shaking():
    gesture = accelerometer.current_gesture()
    return gesture == "shake"

while life > 0:
    if shaking():
        life = life - 1
    else:
        display.show(Image.HAPPY)

display.show(Image.SKULL)

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)

Time as an argument

Lets change the function for checking when we were damaged, so that it accepts an argument for how often you can be damaged and lose life.

from microbit import *

last_damaged_at = microbit.running_time()
life = 3

def shaking():
    gesture = accelerometer.current_gesture()
    return gesture == "shake"

def not_damaged_in(microseconds):
    now = microbit.running_time()
    time_difference = last_damaged_at - now
    return time_difference > microseconds

while life > 0:
    if shaking() && not_damaged_in(2000):
        life = life - 1
		last_damaged_at = microbit.running_time()
        display.show(Image.ANGRY)
    else:
        display.show(Image.HAPPY)

display.show(Image.SKULL)

Take damage function

Move logic for taking damage into its own method

from microbit import *

last_damaged_at = microbit.running_time()
life = 3

def shaking():
    gesture = accelerometer.current_gesture()
    return gesture == "shake"

def not_damaged_in(microseconds):
    now = microbit.running_time()
    time_difference = last_damaged_at - now
    return time_difference > microseconds

def take_damage():
    life = life - 1
    last_damaged_at = microbit.running_time()
    display.show(Image.ANGRY)

while life > 0:
    if shaking() && not_damaged_in(2000):
        take_damage()
    else:
        display.show(Image.HAPPY)

display.show(Image.SKULL)