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

In this project you will learn basic programming concepts by creating a Rock-Paper-Scissors game using Python and micro:bit.

Project goalGet started with python and micro:bit by creating a Rock-Paper-Scissors game
What you’ll learnBasic programming concepts (variables, functions, if statements)
Tools you’ll needA modern browser
Time needed to complete30 minutes

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.

1
2
3
def on_gesture_shake():
    pass
input.on_gesture(Gesture.SHAKE, on_gesture_shake)
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.

1
2
hand1 = randint(1, 3)
hand2 = randint(1, 3)
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:

1
basic.show_number(hand1)

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.

1
2
3
4
5
6
if hand1 == 1:
    basic.show_icon(IconNames.SMALL_SQUARE)
elif hand1 == 2:
    basic.show_icon(IconNames.SQUARE)
else:
    basic.show_icon(IconNames.SCISSORS)

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

1
2
3
4
5
6
if hand2 == 1:
    basic.show_icon(IconNames.SMALL_SQUARE)
elif hand2 == 2:
    basic.show_icon(IconNames.SQUARE)
else:
    basic.show_icon(IconNames.SCISSORS)

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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)

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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)

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!

You May Also Like