Microbit Python Computer Science
Intermediate
40 mins
Teacher/Student led
What you need:
Chromebook/Laptop/PC or Microbit

Reaction Time Tester Game

In this lesson, you'll create a fun game to test your reaction speed using a Micro:bit. Follow step-by-step instructions to build the game, measure your response time, store high scores, and display results with engaging challenges.
Learning Goals Learning Outcomes Teacher Notes Lesson Files

Live Class Feed

This is a live feed of the latest activity by your students on this lesson. It will update in real-time as they work on the lesson.
Load previous activity

    1 - Introduction to the Game

    In this lesson, you'll build a Reaction Time Tester game using your Micro:bit. The game will measure how quickly you press a button after a random LED lights up. You'll use timers within loops to calculate reaction time, store high scores in a list, compare times using if statements inside functions, and display results with string formatting. This reinforces core Python concepts in a fun, competitive challenge.

    By the end of this lesson, you'll have a game that tracks and displays your best reaction times. Open the Micro:bit Python editor at python.microbit.org and start a new project.

    Remember to test your code frequently by running it in the simulator. The game will respond to button presses on the virtual Micro:bit.

    2 - Import Modules

    In this step, we'll import the necessary modules. We need the microbit module for hardware access, the random module for random delays, and we'll use time for timing, but since Micro:bit uses running_time() for timers, we'll handle that later.

    Start by adding the following complete code to your editor:

    from microbit import *
    import random
    This imports everything from microbit and the random module. Run the code; nothing will happen yet, but it should run without errors.

    3 - High Scores List and Game Loop

    Next, we'll create a list to store high scores (initially empty) and display a 'Press A to start' message to start the game. Lists are perfect for holding multiple scores, and we'll add to it later. We'll also add in a loop for the code for the game.

    Update your code to the following complete version:

    from microbit import *
    import random
    
    high_scores = []
    display.scroll("Press A to start", delay=50)
    
    while True:
        while not button_a.is_pressed():
            sleep(100)
    Avoid copy-pasting the code. Type each line to master the code’s logic and structure. This hands-on approach will pay off when you’re tackling exams and real-world coding challenges.
    Run the code. You should see 'Press A to start' scroll on the display. The list high_scores is now set up to store times.
    The inner while loop waits for button A to be pressed, checking every 100 milliseconds to avoid constant CPU usage.

    4 - Add a Random Delay

    Now, let's add a random delay before lighting up an LED (by showing an image). This sets up the 'wait' period before the signal to react.

    We'll use sleep(random.randint(1000, 5000)) for a random wait between 1-5 seconds. Then show Image.YES as the 'go' signal.

    Update your code to this complete version:

    from microbit import *
    import random
    
    high_scores = []
    display.scroll("Press A to start", delay=50)
    
    while True:
        while not button_a.is_pressed():
            sleep(100)
    
    	display.clear()
        sleep(random.randint(1000, 5000))
        display.show(Image.YES)
    
    Run the code. Press A to start, the display clears, and after a random delay, it shows the YES image.
    This adds a random delay using random.randint and displays Image.YES as the go signal.

    5 - Get Reaction Time

    Now, let's measure the reaction time. After showing the image, we'll start a timer, wait for button B to be pressed, calculate the reaction time, and display it.

    We'll use running_time() to get the current time in milliseconds, and subtract to find the difference.

    Update your code to this complete version:

    from microbit import *
    import random
    
    high_scores = []
    display.scroll("Press A to start", delay=50)
    
    while True:
        while not button_a.is_pressed():
            sleep(100)
            
        display.clear()
        sleep(random.randint(1000, 5000))
        display.show(Image.YES)
        
        start_time = running_time()
        while not button_b.is_pressed():
            sleep(20)
        
        reaction_time = running_time() - start_time
        display.scroll("Time: {} ms".format(reaction_time))
    Run the code. Press A to start, wait for the image after a random delay, then press B quickly. It should display your reaction time in milliseconds using string formatting.
    This uses loops for waiting, running_time() for timing, and format() for displaying the result.

    Unlock the Full Learning Experience

    Get ready to embark on an incredible learning journey! Get access to this lesson and hundreds more in our Digital Skills Curriculum.

    Copyright Notice
    This lesson is copyright of DigitalSkills.org. Unauthorised use, copying or distribution is not allowed.
    πŸͺ Our website uses cookies to make your browsing experience better. By using our website you agree to our use of cookies. Learn more