Skip to content

Instantly share code, notes, and snippets.

@aewhite
Created September 20, 2019 01:34
Show Gist options
  • Save aewhite/754d834b17215f45843c6d4bd407429f to your computer and use it in GitHub Desktop.
Save aewhite/754d834b17215f45843c6d4bd407429f to your computer and use it in GitHub Desktop.
from microbit import *
import random
def buildPattern(length):
formatString = '{0:0' + str(length) + 'b}'
binaryString = formatString.format(random.getrandbits(length))
letterPattern = ['A' if char == '0' else 'B' for char in binaryString]
return letterPattern
patternLength = random.randint(3, 5)
pattern = buildPattern(patternLength)
targetPosition = random.randint(0, patternLength)
targetLetter = pattern[targetPosition]
fullSequence = pattern + pattern + pattern + pattern[0:targetPosition]
displayText = " ".join(fullSequence) + " ?"
display.show(displayText, 400)
expectedButton = button_a if targetLetter == 'A' else button_b
unexpectedButton = button_a if targetLetter == 'B' else button_b
while True:
if expectedButton.is_pressed():
display.show(Image.HAPPY)
break
elif unexpectedButton.is_pressed():
display.show(Image.SAD)
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment