Rock Paper Scissors Game in Python

Rock Paper Scissors Game in Python

4 mins read10.7K Views Comment
Updated on Jan 27, 2023 12:53 IST

ROCK PAPER SCISSORS is a traditional two-player game. Each round, players begin by saying, “rock, paper, scissors, shoot!” Each player holds their fist for the rock, flat hand for the paper, or index and middle finger for scissors on “shoot.” The rock crushes the scissors, the scissors cut the paper, and the paper covers the rock.

2022_07_MicrosoftTeams-image-3.jpg

In this article, we are going to build a classic rock paper scissors game in Python. We will be writing two programs for this – 

So, without further ado, let’s get started! 

For the first program, we will be using the random module of Python to randomize the generated outputs simulating the throw of a dice. 

Basic Rock Paper Scissors Python Program 

To understand the program below, you should have a basic idea about the following Python concepts: 

  • Python Random module 
  • Python Loops 

Here, we will use the randint() method of the random module. This function will generate a random number in the specified range. Let’s look at the stepwise Python implementation below: 

  • 1 Step: Import the random module and create a list of play options:
 
#Import random module
import random
#Create a list of play options
play = ["Rock", "Paper", "Scissors"]
Copy code
  • 2 Step: Setup the player, i.e., the computer and you: 
 
#Assign a random play to the system
computer = play[random.randint(0,2)]
#Set the player to False
player = False
Copy code
  • 3 Step: Create the game using the while loop: 
 
while player == False:
#Set the player to True
player = input("Rock, Paper, Scissors?")
if player == computer:
print("Tie!")
elif player == "Rock":
if computer == "Paper":
print("You lose!", computer, "covers", player)
else:
print("You win!", player, "smashes", computer)
elif player == "Paper":
if computer == "Scissors":
print("You lose!", computer, "cuts", player)
else:
print("You win!", player, "covers", computer)
elif player == "Scissors":
if computer == "Rock":
print("You lose...", computer, "smashes", player)
else:
print("You win!", player, "cuts", computer)
else:
print("That's not a valid play. Check the spelling!")
#Player was set to True, but we want it to be False so the loop continues
player = False
computer = play[random.randint(0,2)]
Copy code

Output:

2022_07_image-102.jpg

What have we done here? 

  • We first import the random module of Python. 
  • Then we set up the game players, i.e., the computer and the user. Here, we use the random.randint() method to generate a random number between 0-2, corresponding to each of the three play options for the computer. 
  • Next, we create a while loop that runs every time the player chooses a play option – Rock, Paper, or Scissors. Inside, we use a nested if-elif-else loop to determine who wins each time. 

Thus, we have created a basic rock-paper-scissors game that will help beginners who are just delving into Python Programming think logically and practice their concepts while having fun! 

Next, we are going to build yet another rock-paper-scissors game focusing more on the learners who are a little experienced in Python. This time around, we will be making use of the Tkinter module of Python. 

Quick Intro to Tkinter 

Tkinter is the most commonly used Graphical User Interface (GUI) package in Python. Its ease of use and powerful object-oriented interface provides a seamless way of creating Python GUI applications. 

Let’s see how we will build our game application using Tkinter: 

Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

Rock Paper Scissors Game Application Using Tkinter 

o understand the program below, you should have a basic idea about the following Python concepts: 

  • Random package 
  • Tkinter package 
  • Python Loops 

Here, we will develop a computer application that will play rock-paper-scissors with us. 

  • 1 Step: Download and install the Tkinter package on your terminal: 
 
pip install tkinter
Copy code
  • 2 Step: Import the random and Tkinter packages: 
 
import random
import tkinter
Copy code
  • 3 Step: Initialize the window: 
 
#Create Object
root = tkinter.Tk()
#Set geometry
root.geometry("400x400")
#Set title
root.title("Shiksha Online Rock-Paper-Scissors Game")
Copy code

What have we done here? 

In this step, we have created a Tk root widget to build the main window of our game application. 

  1. We created a root object. 
  2. Set the dimensions of the application window using root.geometry()
  3. Set the tile of the application using root.title()
  • 4 Step: Define a dictionary that takes in the play options for the computer:
 
#Computer Value
computer_dict = {
"0":"Rock",
"1":"Paper",
"2":"Scissors"
}
Copy code
  • 5 Step: Create a function that resets the game: 
 
#Reset the game
def reset_game():
b1["state"] = "active"
b2["state"] = "active"
b3["state"] = "active"
l1.config(text = "Player ")
l3.config(text = "Computer")
l4.config(text = "")
Copy code
  • 6 Step: Create a function to disable the button: 
 
#Disable the button
def button_disable():
b1["state"] = "disable"
b2["state"] = "disable"
b3["state"] = "disable"
Copy code
  • 7 Step: Create three functions based on the three play options to select the game winner: 
 
#If player selects rock
def player_rock():
c_v = computer_dict[str(random.randint(0,2))]
if c_v == "Rock":
match_result = "Tie!"
elif c_v=="Scissors":
match_result = "Player Wins"
else:
match_result = "Computer Wins"
l4.config(text = match_result)
l1.config(text = "Rock ")
l3.config(text = c_v)
button_disable()
#If player selects paper
def player_paper():
c_v = computer_dict[str(random.randint(0, 2))]
if c_v == "Paper":
match_result = "Tie!"
elif c_v=="Scissors":
match_result = "Computer Wins"
else:
match_result = "Player Wins"
l4.config(text = match_result)
l1.config(text = "Paper ")
l3.config(text = c_v)
button_disable()
#If player selects scissors
def player_scissors():
c_v = computer_dict[str(random.randint(0,2))]
if c_v == "Rock":
match_result = "Computer Wins"
elif c_v == "Scissors":
match_result = "Tie!"
else:
match_result = "Player Wins"
l4.config(text = match_result)
l1.config(text = "Scissors ")
l3.config(text = c_v)
button_disable()
Copy code
  • 8 Step: Create the Labels, Frames, and Buttons for our GUI application: 
 
#Add Labels, Frames and Button
tkinter.Label(root,
text = 'Choose any one: Rock, Paper, Scissors',
font = "Consolas",
fg = "blue").pack(pady = 20)
frame = tkinter.Frame(root)
frame.pack()
l1 = tkinter.Label(frame,
text = "Player ",
font = 10)
l2 = tkinter.Label(frame,
text = "VS ",
font = "Consolas")
l3 = tkinter.Label(frame, text = "Computer", font = 10)
l1.pack(side = 'left')
l2.pack(side = 'left')
l3.pack()
l4 = tkinter.Label(root,
text = "",
font = "Consolas",
bg = "white",
width = 15 ,
borderwidth = 2,
relief = "solid")
l4.pack(pady = 20)
frame1 = tkinter.Frame(root)
frame1.pack()
b1 = tkinter.Button(frame1, text = "Rock",
font = 8, width = 7, bg = "light blue",
command = player_rock)
b2 = tkinter.Button(frame1, text = "Paper ",
font = 8, width = 7, bg = "light blue",
command = player_paper)
b3 = tkinter.Button(frame1, text = "Scissors",
font = 8, width = 7, bg = "light blue",
command = player_scissors)
b1.pack(side = 'left', padx = 10)
b2.pack(side = 'left', padx = 10)
b3.pack(padx = 10)
tkinter.Button(root, text = "Reset Game",
font = 10, fg = "red",
bg = "light grey", command = reset_game).pack(pady = 20)
#Execute Tkinter
root.mainloop()
Copy code

What have we done here? 

In this step, we will create multiple labels: 

  • A head label that displays the title of the game, we will set its font and properties. 
  • A player label that displays the play options selected by the player on the left. 
  • A computer label that displays the play options selected by the computer on the right. 
  • A label to show the text “vs” between the player and the computer label. 
  • A result label that displays the result, we will set its font and properties. 

Then we will create the buttons: 

  • Three push buttons for each play option – Rock, Paper, and Scissors.  
  • A reset button to reset the game. 

We will also use the mainloop() function to execute the GUI and wait for the events to happen. 

Output:

2022_07_image-105.jpg

As you can see above, we have created our very own rock-paper-scissors game application! Let’s click on the Reset Game button again: 

2022_07_image-106.jpg

Looks pretty cool, doesn’t it? 

Endnotes 

Hope this article proved helpful and fun in teaching you how to build a rock-paper-scissors game in Python. If you wish to learn more about Python and practice Python programming, you can explore related articles here

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio