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

In the second part of the Python and micro:bit project you will improve the Rock-Paper-Scissors game by creating variables for storing the current game round and players’ score.

Project goalGet started with python and micro:bit by creating a Rock-Paper-Scissors game
What you’ll learnGlobal variables and lists in Python
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, choose the micro:bit and then select the previous Rock-Paper-Scissors project.

If you followed my instructions from the first part of the project, you should have a function called on_gesture_shake. This function contains all the game code that runs when you shake the micro:bit. You also defined two variables hand1 and hand2 for the first and second player respectively and you used the function randint() to make a random choice for each player. Finally, you showed players’ selected objects and the number of the winner by using multiple if statements.

Create a variable named rounds to store the current round

Create the variable rounds with a value 0 outside of the function (before starting the game the number of round is 0). The variable rounds is a global variable.

Variables that are created outside of all functions, are called global variables. In order to change a global variable inside a function, you should use the global keyword.

In the beginning of every round, increase the rounds value by 1.

1
2
3
4
5
6
rounds = 0
def on_gesture_shake():
    global rounds
    rounds += 1
    
input.on_gesture(Gesture.SHAKE, on_gesture_shake)

The expression rounds += 1 is equal to rounds = rounds + 1. This expression means that we get the current value of rounds, we add 1 and then we store the new value in the rounds variable.

Modify the winner selection code

In the part 1, we used multiple if statements (nested if statements) in order to determine the winner and the function basic.show_number() to show the number of the player that won. In this project, you will modify this code snippet by creating a variable named winner in which you will store the number of the player that won (or 0 if none of the players won).

For instance, suppose that the first player wins. In this case, you use the function basic.show_number(1) in order to print the number 1. Replace this function with the expression winner = 1. Similarly, if the second player wins, use the expression winner = 2 and if it’s a tie, use the expression winner = 0.

Finally, use the command basic.show_number(winner) to show the number that is stored in the winner variable.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
if hand1 == 1: # Rock
    if hand2 == 1: # Rock
        winner = 0
    elif hand2 == 2: # Paper
        winner = 2
    else: # Scissors
        winner = 1
elif hand1 == 2: # Paper
    if hand2 == 1: # Rock
        winner = 1
    elif hand2 == 2: # Paper
        winner = 0
    else: # Scissors
        winner = 2
else: # Scissors
    if hand2 == 1: # Rock
        winner = 2
    elif hand2 == 2: # Paper
        winner = 1
    else: # Scissors
        winner = 0
basic.show_number(winner)

Create a variable named score to store the player’s score

You can create two variables, score1 and score2, to store the score of the first and the second player, respectively. In this project, we are going to use a list instead.

A list is a data structure that allows you to store various data in it. This means that you can store may elements or values in a list. In Python, lists are created by placing the items inside of a square bracket ([ ]) separated by comma (,). For example, the following command creates a list named numbers filled with numbers in the range of 1 to 5:

1
numbers = [1, 2, 3, 4, 5]

Create the list score with a value [0, 0] outside of the function (before starting the game the players’ score is 0). The first list item corresponds to the first player’s score and the second list item to the second player’s score.

1
score = [0, 0]

Update the score in every round

Use if statements and the variable winner (in which you have stored the number of the player that won) in order to increase by 1 the score of the first or the second player, at the end of every round.

How to access list item? Every list item is assigned a number in range 0 to n-1, where n is the total number of the list items. These numbers are called indexes. You access a list item by referring to its index number. For example, suppose that you create a list named list1. In order to access the first item, you use list1[0], and for the third item you use list1[2].

1
2
3
4
if winner == 1:
    score[0] += 1
elif winner == 2:
    score[1] += 1

You will print the players score in the form of “1-3”. The first number corresponds to the first player’s score and the second one to the second player’s score. In order to print the score in that form use the following command:

1
basic.show_string(str(score[0]) + "-" + str(score[1]))

The function basic.show_string() is used for printing characters. However, the values score[0] and score[1] are numbers, and thus you have to convert them first into characters using the str() function. The plus (+) symbol is used for string concatenation. For example, the following code snippet:

1
2
3
4
a = "Hello, "
b = "John!"
result = a + b
print(result)

will print: “Hello, John!”.

Improve user experience

Use pause() and clear_screen() functions exactly the same way you used them in the first part of the project, in order to make a smooth transition between images.

Congratulations! You have developed a 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. You can download the code from my GitHub repository.

You May Also Like