Mubin's Odyssey

Mubin's Odyssey

Build a Python3 Rock Paper Scissor Game Using ASCII Art

Build a Python3 Rock Paper Scissor Game Using ASCII Art

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

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

  1. Rock wins against the scissors.
  2. Scissors win against the paper.
  3. Paper wins against the rock.

https://i.ibb.co/Jvm2vWs/game-rules.png

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
  1. If the User chooses 0 [rock] and the computer chooses 2 [scissor], then the User will win.
  2. If the computer chooses 0 [rock] and the user chooses 2 [scissor], then the User will lose, the computer wins.
  3. If computer choice is greater than user choice, the user will lose, computer win.
  4. If user choice is greater than computer choice, then the user will win.
  5. 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.


🚩👉 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!

 
Share this
Proudly part of