Science Oxford Logo

CCC - Time Games

Resources for the 'Time Games' Creative Computing Club.

This page recaps what we discussed as a group.

To skip to ideas for your next steps, click here.


Creating your first program

Open the Mu editor on your computer. If there is code on the screen, start a New program from the menu.

Mu menu, highlighting 'New'

Type these two lines of code into your blank window:

from microbit import *
display.show(Image.HAPPY)

Plug your micro:bit into the computer, then click Flash:

Mu menu, highlighting 'Flash'

Some code should be running on your micro:bit!

If you get a message scrolling across the micro:bit's screen, this is an error message!

Debug your code, then click Flash again to test your changes.

Once your code is working - change it!

Instead of HAPPY, can you think of another Image the micro:bit could show on its display?

How does the game work?

  1. The micro:bit randomly chooses one of its options.
  2. It stores this choice in a list, so it can keep track of all of its choices.
  3. The player chooses from their options.
  4. The micro:bit stores the players choice in another list, so it can keep track of all their choices.
  5. When the player has chosen the same number of things as the micro:bit has, the two lists are compared.
  6. If the two lists are the same, the micro:bit knows that the player was correct!
  7. If the two lists are not the same, it knows that the player got it wrong.

  8. animation showing the game's memory

Testing the game

The code below is a working game.

  1. Copy and paste it into Mu.
  2. Flash it to your micro:bit without changing anything.
  3. Play the game - does it work how you would expect?
  4. Think about what improvements you could make to the game.
  5. Make some changes!

from microbit import *
import random
import music
'''
------------WHAT DOES THE MICROBIT DO?------------
'''
sequence = []
options = ["A", "B"]

def action():
    global sequence
    choice = random.choice(options)
    sequence.append(choice)

    for item in sequence:
        display.show(item)
        sleep(500)
        display.clear()
        sleep(500)

'''
------------WHAT SHOULD THE PLAYER DO?------------
'''
def reaction():
    global response, sequence
    response = []
    while 0 <= len(response) < len(sequence):

        if button_a.was_pressed():
            response.append("A")
        if button_b.was_pressed():
            response.append("B")

'''
------------WHAT HAPPENS AT THE END?------------
'''
def correct():
    display.show(Image.YES)

def incorrect():
    global sequence
    sequence = []
    display.show(Image.NO)

'''
------------THE MAIN GAME CODE------------
'''
while True:
    action()
    reaction()
    if sequence == response:
        correct()
    else:
        incorrect()
    sleep(1000)


How does the game work?

The last section of the code is where all the good stuff happens - inside the main loop.

explanation of the main game loop
  1. The micro:bit creates a random sequence
  2. The micro:bit lets the player respond
  3. If the player's response is the same as the micro:bit's sequence...
  4. The micro:bit knows the player has done it correctly.
  5. If the player's reponse is not the same as the micro:bit's sequence...
  6. The micro:bit knows the player has done it incorrectly
  7. The micro:bit pauses for a second...
  8. Then starts again, and takes another action!

The things that happen when the micro:bit takes a turn, and how it understands inputs from the player, are defined by the functions further up the code.

Each important section is labelled with a comment, like this:

'''
------------WHAT DOES THE MICROBIT DO?------------
'''

What next?

When deciding what to add to, or change, in the code, think careful about where your changes need to happen.

The links below give you some ideas and help to improve your game.

Use the print-outs of the code around the room to help you - think about where your changes need to happen!


Do you want to...