# Radio hot potato: a free micro:bit lesson from Maze Education
# https://maze.education/microbit/lessons/hot-potato
# MicroPython for the micro:bit Python Editor: https://python.microbit.org

# Radio hot potato: A and B together start a game, a shake throws the potato and its fuse to the other board.
from microbit import *
import radio
import random
import music

radio.on()
radio.config(group=12)
POTATO = Image("00000:09990:99999:09990:00000")
holding = False
fuse = 0

while True:
    if button_a.is_pressed() and button_b.is_pressed():
        fuse = random.randint(10000, 25000)
        holding = True
        display.show(POTATO)
        accelerometer.was_gesture("shake")
        sleep(500)
    message = radio.receive()
    if message and message.isdigit():
        fuse = int(message)
        holding = True
        display.show(POTATO)
        accelerometer.was_gesture("shake")
    if holding:
        if accelerometer.was_gesture("shake"):
            holding = False
            radio.send(str(fuse))
            display.clear()
        else:
            fuse = fuse - 100
            if fuse <= 0:
                holding = False
                display.show(Image.SKULL)
                music.play(music.WAWAWAWAA, wait=False)
    sleep(100)
