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.


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 -
from microbit import *
import random

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

plus = Image("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

while correct == True:
  #Let's start by displaying the sequence
  for character in sequence:
        if character=="A":
        elif character=="B":
  #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():
        #Did the user guess it wrong? 
        if sequence[capturedInputs] == "B":
            correct = False
        capturedInputs += 1
     if button_b.is_pressed():
       #Did the user guess it wrong? 
        if sequence[capturedInputs] == "A":
            correct = False
        capturedInputs += 1
  #Add an extra character to the sequence
  if correct==True:
        sequence = sequence + random.choice(AB)

#Display Game Over  + final score
if len(sequence)>3:    
    display.scroll("Game Over: Score: " + str(len(sequence))) 
    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: