Skip to content

Instantly share code, notes, and snippets.

@mboyar
Created May 1, 2018 19:58
Show Gist options
  • Save mboyar/98ba95b051de6747a239039cbd35a836 to your computer and use it in GitHub Desktop.
Save mboyar/98ba95b051de6747a239039cbd35a836 to your computer and use it in GitHub Desktop.
# Simon Game - based on 101Computing - www.101computing.net/microbit-simon-game
from microbit import *
import music
import random
plus = Image("00000:"
"00900:"
"09990:"
"00900:"
"00000")
def clear():
pin3.write_digital(0)
pin4.write_digital(0)
pin5.write_digital(0)
pin6.write_digital(0)
def doALED():
clear()
pin3.write_digital(1)
def doBLED():
clear()
pin4.write_digital(1)
def doCLED():
clear()
pin5.write_digital(1)
def doDLED():
clear()
pin6.write_digital(1)
def doABeep():
tune = ["C4:2"]
music.play(tune)
def doBBeep():
tune = ["D4:2"]
music.play(tune)
def doCBeep():
tune = ["E4:2"]
music.play(tune)
def doDBeep():
tune = ["F4:2"]
music.play(tune)
def doAllOn():
pin3.write_digital(1)
pin4.write_digital(1)
pin5.write_digital(1)
pin6.write_digital(1)
def flashReady():
for flashRDY in range(0 , 4):
doAllOn()
sleep(50)
clear()
simonsString = ["A", "B", "C", "D"]
sequence = random.choice(simonsString) + random.choice(simonsString) + random.choice(simonsString)
correct = True
sleep(1000)
while correct == True:
for character in sequence:
if character=="A":
doALED()
doABeep()
elif character=="B":
doBLED()
doBBeep()
elif character=="C":
doCLED()
doCBeep()
elif character=="D":
doDLED()
doDBeep()
sleep(500)
display.show(plus)
clear()
sleep(500)
flashReady()
maxInputs = len(sequence)
capturedInputs = 0
while capturedInputs < maxInputs and correct == True:
if pin16.read_digital() == 0:
doALED()
doABeep()
#Did the user guess it wrong?
if sequence[capturedInputs] != "A":
correct = False
sleep(200)
display.show(plus)
capturedInputs += 1
if pin1.read_digital() == 0:
doBLED()
doBBeep()
#Did the user guess it wrong?
if sequence[capturedInputs] != "B":
correct = False
sleep(200)
display.show(plus)
capturedInputs += 1
if pin12.read_digital() == 0:
doCLED()
doCBeep()
#Did the user guess it wrong?
if sequence[capturedInputs] != "C":
correct = False
sleep(200)
display.show(plus)
capturedInputs += 1
if pin2.read_digital() == 0:
doDLED()
doDBeep()
#Did the user guess it wrong?
if sequence[capturedInputs] != "D":
correct = False
sleep(200)
display.show(plus)
capturedInputs += 1
#Add an extra character to the sequence
if correct == True:
sequence = sequence + random.choice(simonsString)
display.show(Image.HAPPY)
sleep(1000)
#Display Game Over + final score
if len(sequence)>3:
music.play(music.FUNERAL)
display.scroll("Game Over: Score: " + str(len(sequence)))
else:
music.play(music.FUNERAL)
display.scroll("Game Over: Score: 0")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment