Last active
November 3, 2015 20:50
-
-
Save rjw57/e2319691c102644bf0a7 to your computer and use it in GitHub Desktop.
Microbit games
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import microbit | |
tolerance=500 # milliseconds | |
def game(): | |
target_time = 3 + microbit.random(5) | |
microbit.display.scroll('Guess ' + str(target_time)) | |
for n in '321': | |
microbit.display.print(n) | |
microbit.sleep(1000) | |
microbit.display.print(' ') | |
then = microbit.running_time() | |
while not microbit.button_a.is_pressed(): | |
pass | |
now = microbit.running_time() | |
diff = abs((now - then) - 1000*target_time) | |
if diff < tolerance: | |
microbit.display.print(microbit.Image.HAPPY) | |
microbit.sleep(3000) | |
else: | |
microbit.display.print(microbit.Image.SAD) | |
microbit.sleep(2000) | |
microbit.display.scroll(str(diff) + 'ms away') | |
microbit.sleep(1000) | |
while True: | |
game() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import microbit | |
def clock_dist(a, b): | |
if a > b: | |
a, b = b, a | |
d1, d2 = b - a, 360 - (b - a) | |
return min(d1, d2) | |
microbit.display.print(microbit.Image.NO) | |
microbit.compass.calibrate() | |
while microbit.compass.is_calibrating(): | |
pass | |
def one_game(): | |
target_h = microbit.random(360) | |
tolerance = 20 | |
while True: | |
h = microbit.compass.heading() | |
d = clock_dist(h, target_h) / float(tolerance) | |
if d < 1.0: | |
microbit.display.print(microbit.Image.YES) | |
microbit.sleep(2000) | |
return | |
else: | |
microbit.display.print(str(int(d))) | |
while True: | |
one_game() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment