paint-brush
How to Build a Rock, Paper, Scissors Game in Pythonby@terieyenike
5,162 reads
5,162 reads

How to Build a Rock, Paper, Scissors Game in Python

by TeriSeptember 5th, 2022
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Rock paper scissors is a popular game played using hand gestures. Two individuals make shapes with their hands, and each shape has a specific degree of power that decides the winner of the game. This article will teach you how to build a Python game, with the symbols representing the hand gestures made from ASCII art. It will be an interactive game with user input and conditions to match the answer provided, if it is correct. The program follows the structured pattern for a smooth, fun, and hitch-free experience.
featured image - How to Build a Rock, Paper, Scissors Game in Python
Teri HackerNoon profile picture

Rock paper scissors is a popular game played using hand gestures. Two individuals make shapes with their hands, and each shape has a specific degree of power that decides the winner of the game.


This article will teach you how to build a rock paper scissors game in Python, with the symbols representing the hand gestures made from ASCII art. Of course, rock paper scissors is an interactive game with user input and conditions to determine the winner and loser of each game.


Getting Started

This project will show you how to give users feedback through an interactive display in the terminal or command prompt and ensure that the program follows the structured pattern for a smooth, fun, and hitch-free experience.

Creating the Game

First, we will write the scripts for the Python rock paper scissors game.


As with every Python project, create a new file, game.py.

Copy and paste the following code:


# game.py

import random

rock = '''
    _______
---'   ____)
      (_____)
      (_____)
      (____)
---.__(___)
'''

paper = '''
    _______
---'   ____)____
          ______)
          _______)
         _______)
---.__________)
'''

scissors = '''
    _______
---'   ____)____
          ______)
       __________)
      (____)
---.__(___)
'''

game_images = [rock, paper, scissors]

user_choice = int(
    input(
        "What do you choose? Type 0 for Rock, 1 for Paper or 2 for Scissors.\n"
    ))

if user_choice >= 3 or user_choice < 0:
    print("You typed an invalid number, you lose!")
else:
    print(game_images[user_choice])

    computer_choice = random.randint(0, 2)
    print(f"Computer chose: {computer_choice}")
    print(game_images[computer_choice])

    if user_choice >= 3 or user_choice < 0:
        print("You typed an invalid number, you lose!")
    elif user_choice == 0 and computer_choice == 2:
        print("You win!")
    elif computer_choice == 0 and user_choice == 2:
        print("You lose")
    elif computer_choice > user_choice:
        print("You lose")
    elif user_choice > computer_choice:
        print("You win!")
    elif computer_choice == user_choice:
        print("It's a draw")


Let’s go over the code block above:

  • Import the in-built Python module, random, meant for making random numbers
  • Symbols representing each of the shapes assigned to variables, rock, paper, and scissors, respectively
  • A list is created in the game_images variable, and each item is defined as an index
  • An input to give a user the choice of selecting an integer in user_choice


Next is the logic for the Python game in conditionals if-elif block.


if user_choice >= 3 or user_choice < 0:
    print("You typed an invalid number, you lose!")
else:
    print(game_images[user_choice])


If a user types a number greater than or equal to 3, another less than 0, or a letter, the program notifies the user of invalid input in the code block above. Otherwise, in the else block, it prints out the symbol index from the variables, rock, paper, and scissors.


    computer_choice = random.randint(0, 2)
    print(f"Computer chose: {computer_choice}")
    print(game_images[computer_choice])


Here, the computer choice outputs a random number from a range of 0 and 2 with the .randint method. As we know, an index in Python starts from 0, not 1. When the computer resolves the selected number, it prints out the image as either rock, paper, or scissors.


if user_choice >= 3 or user_choice < 0:
        print("You typed an invalid number, you lose!")
    elif user_choice == 0 and computer_choice == 2:
        print("You win!")
    elif computer_choice == 0 and user_choice == 2:
        print("You lose")
    elif computer_choice > user_choice:
        print("You lose")
    elif user_choice > computer_choice:
        print("You win!")
    elif computer_choice == user_choice:
        print("It's a draw")


The code block above is the final validation for the conditions if specific user choice values are met. If the user choices meet the conditions, it prints out either a “You win”, “You lose”, or “It’s a draw” output.




Conclusion

This article taught you how to build a rock paper scissors game using Python. Would you try building or playing this Python game? Let me know your thoughts.


Learn More

Understand the concepts and official rules of the game. Check out this guide to play the game**.**