Create a Rock-Paper-Scissors game with Python and micro:bit [Part 2]
In this project you will learn basic programming concepts by creating a “Rock-Paper-Scissors” game using Python and micro:bit.
In this project you will learn basic programming concepts by creating a Rock-Paper-Scissors game using Python and micro:bit.
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 |
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 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.
Define the function on_gesture_shake()
. This function will contain all the game’s code and execute when you shake the micro:bit.
|
|
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
.
|
|
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.Use the command:
|
|
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.
Use the if
statement.
|
|
Write the same commands for the second player. Don’t forget to change the hand1
variable to hand2
.
|
|
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.
|
|
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.
|
|
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.
In the next tutorial, you will learn how to make variables for storing the current game round and players’ score. Stay tuned!
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.