# Catch the drop: a free micro:bit lesson from Maze Education
# https://maze.education/microbit/lessons/catch-game
# MicroPython for the micro:bit Python Editor: https://python.microbit.org

# Catch the drop: A and B move the basket along the bottom row. Miss once and the game is over.
from microbit import *
import random

basket = 2
drop_x = random.randint(0, 4)
drop_y = 0
score = 0
last_fall = running_time()

while True:
    if button_a.was_pressed() and basket > 0:
        basket = basket - 1
    if button_b.was_pressed() and basket < 4:
        basket = basket + 1
    if running_time() - last_fall > 400:
        last_fall = running_time()
        drop_y = drop_y + 1
        if drop_y == 4:
            if drop_x == basket:
                score = score + 1
                drop_x = random.randint(0, 4)
                drop_y = 0
            else:
                break
    display.clear()
    display.set_pixel(basket, 4, 9)
    display.set_pixel(drop_x, drop_y, 5)
    sleep(20)

display.show(Image.SAD)
sleep(1000)
display.scroll("Score " + str(score), loop=True)
