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

Rock-Paper-Scissors Game

In this lesson, you'll build an interactive Rock-Paper-Scissors game with your Micro:bit as the opponent. Follow step-by-step instructions to code button inputs, random choices, and display results. Start creating your game now in the Micro:bit Python editor.
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 create a Rock-Paper-Scissors game using your Micro:bit and the Micro:bit will act as your opponent. You'll use the buttons to make your choice, generate a random choice for the computer using the random module, compare the outcomes with if-else statements, and display the results on the LED screen with messages like 'You win!'. We'll store the possible choices in a list to practise list handling.

    By the end of this lesson, you'll have a fully interactive game. 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 and the random module to generate the computer's choice.

    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.
    If you see any import errors, check your spelling and ensure you're using the online editor.

    3 - Define Choices List

    Next, we'll create a list to store the possible choices: rock, paper, and scissors. Lists are great for holding multiple related items, and we'll use this to select the computer's choice randomly.

    Update your code to the following complete version:

    from microbit import *
    import random
    
    choices = ["rock", "paper", "scissors"]
    display.scroll("Ready")
    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 'Ready' scroll on the display. This confirms the list is set up. The list 'choices' now holds three string items. We'll use it later for random selection.
    Try adding display.scroll(choices[0]) after the list to show 'rock'. This practises accessing list elements.

    4 - Get User Choice

    Now, let's handle user input using the buttons. We'll decide: press button A for rock, button B for paper, and both buttons for scissors. We'll use a loop to wait for a button press.

    The while True: creates an infinite loop that keeps checking for button presses until one is detected. Inside the loop, we use if-elif statements to check which button(s) are pressed. When a condition is true, we assign the user's choice to a variable and use the break statement. The break statement exits the loop immediately, stopping the continuous checking and allowing the program to continue with the next lines of code. The sleep(100) adds a short delay to prevent the loop from running too quickly and using too much processing power.

    Update your code to this complete version:

    from microbit import *
    import random
    
    choices = ["rock", "paper", "scissors"]
    
    while True:
        if button_a.is_pressed() and button_b.is_pressed():
            user_choice = "scissors"
            break
        elif button_a.is_pressed():
            user_choice = "rock"
            break
        elif button_b.is_pressed():
            user_choice = "paper"
            break
        sleep(100)
    
    display.scroll(user_choice)
    Run the code. Click the virtual buttons: A for 'rock', B for 'paper', or A+B for 'scissors'. It should scroll your choice. The loop waits for input, assigns the choice to a variable, and displays it.
    Test all three options in the simulator. This uses if-else for control.

    5 - Generate Computer Choice

    Let's make the Micro:bit choose randomly from the list. We'll use random.choice() to pick one item.

    Update your code to this complete version, adding the computer's choice after the user's:

    from microbit import *
    import random
    
    choices = ["rock", "paper", "scissors"]
    
    while True:
        if button_a.is_pressed() and button_b.is_pressed():
            user_choice = "scissors"
            break
        elif button_a.is_pressed():
            user_choice = "rock"
            break
        elif button_b.is_pressed():
            user_choice = "paper"
            break
        sleep(100)
    
    computer_choice = random.choice(choices)
    
    display.scroll("You: " + user_choice)
    sleep(1000)
    display.scroll("Comp: " + computer_choice)
    Run the code, make a choice, and see both yours and the random computer choice scroll. Notice the string concatenation with '+' for display.
    Run it multiple times; the computer should pick differently due to the random choice.
    Tip: When testing, use the # symbol to comment out lines you wish to bypass temporarily such as display.scroll("You: " + user_choice). This can help if you want to just quickly test something.

    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