DEV Community

Cover image for Create a Rock-Paper-Scissors game with Python and micro:bit [Part 1]
Foteini Savvidou
Foteini Savvidou

Posted on • Originally published at foteinisavvidou.codes

Create a Rock-Paper-Scissors game with Python and micro:bit [Part 1]

Project goal Get started with python and micro:bit by creating a Rock-Paper-Scissors game
What you’ll learn Basic programming concepts (variables, functions, if statements)
Tools you’ll need A modern browser
Time needed to complete 30 minutes

What You'll Build

In this project you will learn basic programming concepts by creating a “Rock-Paper-Scissors” game using Python and micro:bit. You can find the final project at Microsoft Make Code and you can download the code from my GitHub repository.

Instructions

Visit the Microsoft Make Code website and choose the micro:bit. Create a new project and give it a name that you like, then expand the code options and select Python only from the dropdown menu.
We are going to use the accelerometer and the micro:bit’s screen to build our game.

Add an on_shake event to run code when you shake the micro:bit

Define the function on_gesture_shake(). This function will contain all the game’s code and execute when you shake the micro:bit.

def on_gesture_shake():
    pass
input.on_gesture(Gesture.SHAKE, on_gesture_shake)
Enter fullscreen mode Exit fullscreen mode

In programming, functions are blocks of statements that perform specific tasks.

Create 2 players and choose a random object (either rock, paper, or scissors) for each player

Each player selects one of the 3 objects (either rock, paper, or scissors). In programming, we prefer to use numbers instead of words and thus we are going to assign the numbers 1, 2 and 3 to the objects rock, paper and scissors respectively. Therefore, you only have to choose a random number in the range of 1 to 3 for each player.
Inside the on_gesture_shake function, create two variables hand1 and hand2 for the first and the second player respectively. Use the randint() function in order to generate 2 random numbers and store them in the variables hand1 and hand2.

hand1 = randint(1, 3)
hand2 = randint(1, 3)
Enter fullscreen mode Exit fullscreen mode

Variables are used to store information and data. This data can be numbers, letters, words.

The randint() function is used to generate random integer numbers in a specified range. We use two parameters in order to specify the lower and higher limit. For example, randint(1, 10) will return random integer numbers between 1 to 10.

Show players’ choice

Use the command:

basic.show_number(hand1)
Enter fullscreen mode Exit fullscreen mode

to show first player’s choice on the LEDs. What do you notice? A number in the range of 1 to 3 appeared and not the actual player’s choice (rock, paper, or scissors). This is happening because the computer doesn’t know that we symbolize the objects with numbers.

We are going to use the random numbers that are stored in hand1 and hand2 to select a picture to show on the LEDs. If the number is 1, show a picture of a rock. If the number is 2, show a picture of a piece of paper. If the number is not 1 nor 2 (the number is 3), show a picture of scissors.

An if statement is a programming conditional statement that performs different actions depending on whether a given condition is evaluated to true or false.

Use the if statement.

if hand1 == 1:
    basic.show_icon(IconNames.SMALL_SQUARE)
elif hand1 == 2:
    basic.show_icon(IconNames.SQUARE)
else:
    basic.show_icon(IconNames.SCISSORS)
Enter fullscreen mode Exit fullscreen mode

Write the same commands for the second player. Don’t forget to change the hand1 variable to hand2.

if hand2 == 1:
    basic.show_icon(IconNames.SMALL_SQUARE)
elif hand2 == 2:
    basic.show_icon(IconNames.SQUARE)
else:
    basic.show_icon(IconNames.SCISSORS)
Enter fullscreen mode Exit fullscreen mode

Determine the winner

Show number 1, if first player wins. Show number 2, if second player wins and show 0, if it is a tie. Use multiple if statements (nested if statements) in order to determine the winner.

if hand1 == 1: # Rock
    if hand2 == 1: # Rock
        basic.show_number(0)
    elif hand2 == 2: # Paper
        basic.show_number(2)
    else: # Scissors
        basic.show_number(1)
elif hand1 == 2: # Paper
    if hand2 == 1: # Rock
        basic.show_number(1)
    elif hand2 == 2: # Paper
        basic.show_number(0)
    else: # Scissors
        basic.show_number(2)
else: # Scissors
    if hand2 == 1: # Rock
        basic.show_number(2)
    elif hand2 == 2: # Paper
        basic.show_number(1)
    else: # Scissors
        basic.show_number(0)
Enter fullscreen mode Exit fullscreen mode

Improve user experience

As you may consider, players’ icons (choices) appear and disappear quickly. Use the command pause(500) to stop the game for 0.5 seconds (this means that the micro:bit shows the icon for 0.5 seconds). Then use the commands clear_screen() and pause(500) to stop the game for 0.5 seconds before showing the next icon.

def on_gesture_shake():

    hand1 = randint(1, 3)
    hand2 = randint(1, 3)

    if hand1 == 1: # Rock
        basic.show_icon(IconNames.SMALL_SQUARE)
    elif hand1 == 2: # Paper
        basic.show_icon(IconNames.SQUARE)
    else: # Scissors
        basic.show_icon(IconNames.SCISSORS)

    basic.pause(500)
    basic.clear_screen()
    basic.pause(500)

    if hand2 == 1: # Rock
        basic.show_icon(IconNames.SMALL_SQUARE)
    elif hand2 == 2: # Paper
        basic.show_icon(IconNames.SQUARE)
    else: # Scissors
        basic.show_icon(IconNames.SCISSORS)

    basic.pause(500)
    basic.clear_screen()
    basic.pause(500)

    if hand1 == 1: # Rock
        if hand2 == 1: # Rock
            basic.show_number(0)
        elif hand2 == 2: # Paper
            basic.show_number(2)
        else: # Scissors
            basic.show_number(1)
    elif hand1 == 2: # Paper
        if hand2 == 1: # Rock
            basic.show_number(1)
        elif hand2 == 2: # Paper
            basic.show_number(0)
        else: # Scissors
            basic.show_number(2)
    else: # Scissors
        if hand2 == 1: # Rock
            basic.show_number(2)
        elif hand2 == 2: # Paper
            basic.show_number(1)
        else: # Scissors
            basic.show_number(0)

input.on_gesture(Gesture.SHAKE, on_gesture_shake)
Enter fullscreen mode Exit fullscreen mode

Congratulations! You have developed a simple Rock-Paper-Scissors game with Python and micro:bit. If you want to share the project with your friends, you can select the Share button.

What’s next?

In the next tutorial, you will learn how to make variables for storing the current game round and players’ score. Stay tuned!


More articles: foteinisavvidou.codes

Hello, my name is Foteini Savvidou and I am an Electrical and Computer Engineering student who loves writting code and creating beautiful things. I am interested in computer science, AI and especially in applications of technology in healthcare and education. I want to use technology to improve people's everyday life, and create a more sustainable 🌱, accessible and inclusive 🧑‍🤝‍🧑 world.

Blog | LinkedIn | Twitter | GitHub

Top comments (0)