Simple game working

This commit is contained in:
Gabriel Augendre 2021-03-21 07:24:36 +01:00
parent ce858e23f2
commit 3541e2f172
3 changed files with 96 additions and 33 deletions

41
accelerometer.py Normal file
View file

@ -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()

54
game.py Normal file
View file

@ -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()

34
main.py
View file

@ -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__":