BBC micro:bit – Simon Game

simon-gameIn this blog post we are looking at recreating a fully working Simon Game using the BBC micro:bit.

If you are not sure what a Simon Game is you can check it on this wikipedia page.

You can watch the following two video clips to see the game in action, and understand the Python code used to implement it.

You will notice that our version of the game is simpler as we only use the two inputs from the micro:bit (A and B buttons) whereas the real Simon game is a bit more complex as it has four lights/buttons. The logic of the game remains the same though.

simon-game-vs-microbit

Python Code


If you have a micro:bit yoyu can use the micro:bit website and reuse the code provided below, ready to download to your micro:bit. You will have to use the Python code editor option.

# Simon Game - by 101Computing - www.101computing.net/microbit-simon-game
from microbit import *
import random

left = Image("96300:"
             "96300:"
             "96300:"
             "96300:"
             "96300")
             
right = Image("00369:"
             "00369:"
             "00369:"
             "00369:"
             "00369")

plus = Image("00000:"
             "00900:"
             "09990:"
             "00900:"
             "00000")

AB = ["A", "B"]

#Let's start with a sequence of three characters
sequence = random.choice(AB) + random.choice(AB) + random.choice(AB)

            
correct = True
sleep(1000)

while correct == True:
  #Let's start by displaying the sequence
  for character in sequence:
        if character=="A":
            display.show(left)
        elif character=="B":
            display.show(right)
        sleep(1000)
        display.show(plus)
        sleep(500)
   
  display.scroll("?")
   
  #Capture user input
  #The numbers of time the user will need to press the buttons depends on the length of the sequence
  maxInputs = len(sequence)
  capturedInputs = 0
  while capturedInputs < maxInputs and correct == True:
     if button_a.is_pressed():
        display.show(left)
        #Did the user guess it wrong? 
        if sequence[capturedInputs] == "B":
            correct = False
        sleep(500)    
        display.show(plus)        
        capturedInputs += 1
     if button_b.is_pressed():
        display.show(right)
       #Did the user guess it wrong? 
        if sequence[capturedInputs] == "A":
            correct = False
        sleep(500)
        display.show(plus)
        capturedInputs += 1
        
  #Add an extra character to the sequence
  if correct==True:
        sequence = sequence + random.choice(AB)
        display.show(Image.HAPPY)
        sleep(1000)

#Display Game Over  + final score
if len(sequence)>3:    
    display.scroll("Game Over: Score: " + str(len(sequence))) 
else:
    display.scroll("Game Over: Score: 0")

Did you like this challenge?

Click on a star to rate it!

Average rating 3.4 / 5. Vote count: 11

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!

Tagged with: