From 3541e2f1727daa2b561d490f1f801cb7cb764abf Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sun, 21 Mar 2021 07:24:36 +0100 Subject: [PATCH] Simple game working --- accelerometer.py | 41 ++++++++++++++++++++++++++++++++++++ game.py | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 34 +----------------------------- 3 files changed, 96 insertions(+), 33 deletions(-) create mode 100644 accelerometer.py create mode 100644 game.py diff --git a/accelerometer.py b/accelerometer.py new file mode 100644 index 0000000..a5afa68 --- /dev/null +++ b/accelerometer.py @@ -0,0 +1,41 @@ +from microbit import * + +SENSITIVITY = 250 + + +def main(): + while True: + x = accelerometer.get_x() + y = accelerometer.get_y() + + image = get_image(x, y) + display.show(image) + + +def get_image(x, y) -> Image: + if x > SENSITIVITY: + if y < -SENSITIVITY: + image = Image.ARROW_NE + elif y > SENSITIVITY: + image = Image.ARROW_SE + else: + image = Image.ARROW_E + elif x < -SENSITIVITY: + if y < -SENSITIVITY: + image = Image.ARROW_NW + elif y > SENSITIVITY: + image = Image.ARROW_SW + else: + image = Image.ARROW_W + else: + if y < -SENSITIVITY: + image = Image.ARROW_N + elif y > SENSITIVITY: + image = Image.ARROW_S + else: + image = Image.CLOCK1 + return image + + +if __name__ == "__main__": + main() diff --git a/game.py b/game.py new file mode 100644 index 0000000..de90be1 --- /dev/null +++ b/game.py @@ -0,0 +1,54 @@ +import random + +from microbit import * + + +def main(): + wait_for_ready() + while True: + countdown() + winner = game() + display_winner(winner) + wait_for_ready() + + +def display_winner(winner): + display.show(winner) + sleep(2000) + display.show(Image.HAPPY) + + +def game(): + wait = random.randint(1, 10) * 1000 + sleep(wait) + display.show("!") + while True: + if button_a.is_pressed(): + winner = "A" + break + elif button_b.is_pressed(): + winner = "B" + break + return winner + + +def wait_for_ready(): + display.scroll("A or B...") + while True: + if button_a.is_pressed() or button_b.is_pressed(): + break + + +def countdown(): + display.show("3") + sleep(1000) + display.show("2") + sleep(1000) + display.show("1") + sleep(1000) + display.scroll("...") + display.clear() + + +if __name__ == "__main__": + main() diff --git a/main.py b/main.py index a5afa68..3af66d8 100644 --- a/main.py +++ b/main.py @@ -1,40 +1,8 @@ from microbit import * -SENSITIVITY = 250 - def main(): - while True: - x = accelerometer.get_x() - y = accelerometer.get_y() - - image = get_image(x, y) - display.show(image) - - -def get_image(x, y) -> Image: - if x > SENSITIVITY: - if y < -SENSITIVITY: - image = Image.ARROW_NE - elif y > SENSITIVITY: - image = Image.ARROW_SE - else: - image = Image.ARROW_E - elif x < -SENSITIVITY: - if y < -SENSITIVITY: - image = Image.ARROW_NW - elif y > SENSITIVITY: - image = Image.ARROW_SW - else: - image = Image.ARROW_W - else: - if y < -SENSITIVITY: - image = Image.ARROW_N - elif y > SENSITIVITY: - image = Image.ARROW_S - else: - image = Image.CLOCK1 - return image + pass if __name__ == "__main__":