Rock Paper Scissors is a test of destiny to settle on all manner of topics. If it's washing dishes or ordering pizza. Rock Paper Scissors is also used as a form of an equal choice between two individuals. It's still the most rewarding toy to win a game. It is played around the world with different names such as "Ro-Sham-Bo;" janken; "Bato, Bato, Pick;" and "Scissors, Paper, Stone."
Let's create a Rock Paper Scissor game.
What is ASCII art?
ASCII art, also known as ANSI art, text art, or word art, forms images or art from the characters of the ASCII chart. Here is an example for better understanding:
$$\ $$\ $$$$$$$$\ $$\ $$\ $$$$$$\
$$ | $$ |$$ _____|$$ | $$ | $$ __$$\
$$ | $$ |$$ | $$ | $$ | $$ / $$ |
$$$$$$$$ |$$$$$\ $$ | $$ | $$ | $$ |
$$ __$$ |$$ __| $$ | $$ | $$ | $$ |
$$ | $$ |$$ | $$ | $$ | $$ | $$ |
$$ | $$ |$$$$$$$$\ $$$$$$$$\ $$$$$$$$\ $$$$$$ |
\__| \__|\________|\________|\________|\______/
Stuff Required for the project
- Python 3.8
- Code Editor / IDE or Online IDE like Repl
Lets Start Coding
First of all, let's create a directory name for Rock Paper Scissor and copy-paste this code to the terminal.
mkdir Rock_Paper_Scissor
Getting into the project directory
cd Rock_Paper_Scissor
Let's create the python script named app.py.
touch app.py
Open the Python script to the Code Editor or IDE that you like. Here I am using the Visual Studio Code.
We need the ASCII art of rock, paper, and scissors to make this game. Go to the ASCII Art Archive website and copy Veronica Karlsson's Rock, Paper, and Art Scissors. Or from here you can copy and paste this code.
We assign this art to a variable.
# ASCII Arts for rock, paper, and scissors by Veronica Karlsson
rock = '''
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
______)
__________)
(____)
---.__(___)
'''
We're going to play Rock, Paper, and Scissor on the computer. And every time a user chooses an option, the computer needs to choose a random choice. To do this we are importing random packages that are built-in with Python.
import random
We will add ASCII art to the list; it will help us to easily print the art.
# Adding Game Images into a list
game_images = [rock, paper, scissors]
Take user input from the terminal and print the ASCII art. Here we're going to assign.
- 0 for Rock
- 1 for Paper
- 2 for Scissor
# Taking input from user choice
user_choice = int(input(
"What do you choose? Type 0 for Rock, 1 for Paper, 2 for scissor. \n => "))
print("User Choice: ")
# print game image by user choice
print(game_images[user_choice])
Now open the terminal and copy and paste this code to the terminal.
python app.py
The outcome is going to be like this below. Here I select Paper, and it prints the ASCII Art Paper.
What do you choose? Type 0 for Rock, 1 for Paper, 2 for scissor.
=> 1
user choice:
_______
---' ____)____
______)
_______)
_______)
---.__________)
Compute needs to pick a random number from 0 to 2. Often, we need to see what the option of the machine is and print it out on the terminal. Next, apply those lines to the app.py
.
# random computer choice
computer_chocie = random.randint(0, 2)
print(" Computer Choice: ")
# print game image by computer choice
print(game_images[computer_chocie])
Open the terminal and review the result again.
python app.py
What do you choose? Type 0 for Rock, 1 for Paper, 2 for scissor.
=> 1
user choice:
_______
---' ____)____
______)
_______)
_______)
---.__________)
computer choice:
_______
---' ____)____
______)
__________)
(____)
---.__(___)
Here I choose Paper and computer to choose Scissor.
Now time to apply the Rock, Paper, and Scissor Rules. Those rules are
- Rock wins against the scissors.
- Scissors win against the paper.
- Paper wins against the rock.
Fig: Game rules
Let's transform the rules into the logic of the machine. Here we represent rock, paper, and scissors in numbers.
0 => Rock
1 => Paper
2 => Scissor
- If the User chooses 0 [rock] and the computer chooses 2 [scissor], then the User will win.
- If the computer chooses 0 [rock] and the user chooses 2 [scissor], then the User will lose, the computer wins.
- If computer choice is greater than user choice, the user will lose, computer win.
- If user choice is greater than computer choice, then the user will win.
- If user choice and computer choice are the same, then It's a draw.
To prevent mistakes, if the user selects more than 3 or some kind of number, the user will lose by default.
We will use the if-elif-else condition for the above rules. Let's apply these conditions to our code list.
# rules in logic
if user_choice == 0 and computer_chocie == 2:
print("You win! 🎉")
elif computer_choice == 0 and user_chocie == 2:
print("You lose. ☠")
elif computer_chocie > user_choice:
print("You lose. ☠")
elif user_choice > computer_chocie:
print("You win!🎉 ")
elif computer_chocie == user_choice:
print("It's a draw.")
elif user_choice >= 3 or user_choice < 0:
print("You typed an invalid number, You Lose. ☠")
Our portion of the coding is complete. Here's our complete Rock, Paper, and Scissor game code.
# rock, paper, and scissors games
# import random package
import random
# ASCII Arts for rock, paper, and scissors
# Adding ASCII art into a variable
rock = '''
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
'''
paper = '''
_______
---' ____)____
______)
_______)
_______)
---.__________)
'''
scissors = '''
_______
---' ____)____
______)
__________)
(____)
---.__(___)
'''
# Adding Game Images into a list
game_images = [rock, paper, scissors]
# Taking input from user choice
user_choice = int(input(
"What do you choose? Type 0 for Rock, 1 for Paper, 2 for scissor. \n => "))
print("User Choice: ")
# print game image by user choice
print(game_images[user_choice])
# random computer choice
computer_chocie = random.randint(0, 2)
print("Computer Choice: ")
# print game image by computer choice
print(game_images[computer_chocie])
# rules in logic
if user_choice == 0 and computer_chocie == 2:
print("You win! 🎉")
elif computer_choice == 0 and user_chocie == 2:
print("You lose. ☠")
elif computer_chocie > user_choice:
print("You lose. ☠")
elif user_choice > computer_chocie:
print("You win!🎉 ")
elif computer_chocie == user_choice:
print("It's a draw.")
elif user_choice >= 3 or user_choice < 0:
print("You typed an invalid number, You Lose. ☠")
Let's launch a game on the terminal.
python app.py
What do you choose? Type 0 for Rock, 1 for Paper, 2 for scissor.
=> 0
User Choice:
_______
---' ____)
(_____)
(_____)
(____)
---.__(___)
Computer Choice:
_______
---' ____)____
______)
__________)
(____)
---.__(___)
You win! 🎉
Hurray, We complete making our game in under 5 minutes.🎉🎉. Show your friends and play with it.
Here's the complete code of this game, but first try to code yourself and improve your abilities.
%[https://github.com/kmhmubin/Python-Projects/blob/master/rock_paper_scissor.py]
🚩👉 If it was helpful to you, please Like/Share to reach out to others as well. Please press the Subscribe button at the top of the page to get an email update from my most recent articles.
Let's talk about web development and UI design on Twitter kmhmubin, feel free to talk with me there!
Comments (0)