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.
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 goal | Get started with python and micro:bit by creating a Rock-Paper-Scissors game |
---|---|
What you’ll learn | Global variables and lists in Python |
Tools you’ll need | A modern browser |
Time needed to complete | 30 minutes |
You can find the final project at Microsoft Make Code and you can download the code from my GitHub repository.
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 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.
In the beginning of every round, increase the rounds
value by 1.
|
|
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.
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.
|
|
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:
|
|
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.
|
|
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]
.
|
|
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:
|
|
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:
|
|
will print: “Hello, John!”.
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.
In this project you will learn basic programming concepts by creating a “Rock-Paper-Scissors” game using Python and micro:bit.
Σε αυτή την άσκηση θα βελτιώσεις το παιχνίδι «Πέτρα-Ψαλίδι-Χαρτί» που δημιούργησες στο πρώτο μέρος με Python και micro:bit.
Σε αυτή την άσκηση θα μάθεις βασικές έννοιες προγραμματισμού δημιουργώντας το παιχνίδι «Πέτρα-Ψαλίδι-Χαρτί» με Python και micro:bit.
This post will take you through the newest Read OCR API of Azure Computer Vision, which is used for extracting text from images.