Python Rock, Paper, and Scissor Game – Where Coding Meets Fun!

The game of rock paper scissors is sometimes known as stone paper scissors. It is a two-person hand game in which each player can randomly make one of three shapes from their hand.

About Python Rock, Paper, and Scissor Game

We’re playing rock, paper, scissors with the computer. As a result, we have two players: the user and the computer. We can play it whenever and wherever we choose. In the Python programming language, we used Tkinter and a random module to create this game.

RULES: A player who chooses rock will win over another player who selects scissors but lose to the player who selects paper; a player who selects paper will lose to the player who selects scissors. If both players make the same choice, the game is a tie.

Prerequisites for Rock, Paper, and Scissor Game using Python

  • Basic knowledge of the Python programming language and how functions are defined in it.
  • How the GUI window is made using Tkinter
  • How to work with the Random library

Download Python Rock, Paper, and Scissor Game Project

Please download the source code of Python Rock, Paper, and Scissor Game Project from the following link: Python Rock, Paper, and Scissor Game Code

Steps to Create Rock, Paper, and Scissor Game using Python

Following are the steps for developing the Python Rock, Paper, and Scissor Game Project:

Step 1: Importing the necessary modules

Step 2: Making a window for our project

Step 3: Functions

Step 4: The main game loop

Step 1: Importing the necessary modules

To use Tkinter, we need to import the Tkinter module to make our GUI window. We are also going to import the random module.

Code

# import library
import random
from tkinter import *

Step 2: Making a window for our project

This code sets the title of the window as ‘TechVidvan’, and sets its dimensions.

Code

# Set up the window
root = Tk()
root.geometry('600x600')
root.title('TechVidvan')
root.config(bg='#f7f1ea')
global value

Step 3: Functions

We make three functions (for rock, paper, and scissors) for each player and computer.

  • rock() to display rock when selected by the player.
  • paper() to display paper when selected by the player.
  • scissor() to display scissor when selected by the player.

rock_1(), paper_1(), and scissor_1() to display the results by matching them with the value selected by the user.

def game(): for matching the user-selected value with the random value generated by the computer using the random.randint(1, 3) function and displaying that random value by configuring the text of the label widget.

Code

# function
def rock():
   l1.config(text="Rock")
   global value
   value = 1
   game()




def paper():
   l1.config(text="Paper")
   global value
   value = 2
   game()
def scissor():
   l1.config(text="Scissor")
   global value
   value = 3
   game()




def rock_1():
   if value == 1:
       l0.config(text="Game Tie")
   elif value == 2:
       l0.config(text="Player Win")
   elif value == 3:
       l0.config(text="Computer Win")




def paper_1():
   if value == 1:
       l0.config(text="Computer Win")
   elif value == 2:
       l0.config(text="Game Tie")
   elif value == 3:
       l0.config(text="Player Win")




def scissor_1():
   if value == 1:
       l0.config(text="Player Win")
   elif value == 2:
       l0.config(text="Computer Win")
   elif value == 3:
       l0.config(text="Game Tie")




def game():
   computer_value = random.randint(1, 3)
   if computer_value == 1:
       l2.config(text="Rock")
       rock_1()


   elif computer_value == 2:
       l2.config(text="Paper")
       paper_1()


   elif computer_value == 3:
       l2.config(text="Scissor")
       scissor_1()

Step 4: The main game loop

We give the heading to our window “Rock, Paper, and Scissors” and create two frames (frame and frame 2): one to display the value selected by the user and a random value generated by the computer, and the other to display options to the player from which he or she can select their value.

Make a label to display the result of the game, which is in frame3.

Code

Label(root, text='Rock, Paper, and Scissors', font='arial 15 bold', bg='#f7f1ea', fg='#136577').pack()
frame = Frame(root, bg='#679ba7')
frame.pack(anchor=CENTER, pady=20)
frame0 = Frame(frame, bg='#679ba7')
frame0.pack(side=LEFT, pady=20)
Label(frame0, text="Player's move", bg='#679ba7', font=('Arial', 15, 'bold')).pack(padx=20, pady=20)
l1 = Label(frame0, bg='#f7f1ea', font=('Arial', 15, 'normal'), relief="ridge")
l1.pack(padx=20, pady=20)


frame1 = Frame(frame, bg='#679ba7')
frame1.pack(side=LEFT, pady=20)
Label(frame1, text="Computer's move", bg='#679ba7', font=('Arial', 15, 'bold')).pack(padx=20, pady=20)
l2 = Label(frame1, bg='#f7f1ea', font=('Arial', 15, 'normal'), relief="ridge")
l2.pack(padx=20, pady=20)


frame2 = Frame(root, bg='#679ba7')
frame2.pack(anchor=CENTER, pady=20)
Label(frame2, text='Select your move:', bg='#679ba7', font=('Arial', 15, 'bold')).pack()
Button(frame2, text="Rock", bg='#f7f1ea', font=('Arial', 15, 'normal'), command=rock, relief="ridge").pack(side=LEFT, padx=20, pady=20)
Button(frame2, text="Paper", bg='#f7f1ea', font=('Arial', 15, 'normal'), command=paper, relief="ridge").pack(side=LEFT, padx=20, pady=20)
Button(frame2, text="Scissor", bg='#f7f1ea', font=('Arial', 15, 'normal'), command=scissor, relief="ridge").pack(side=LEFT, padx=20, pady=20)


frame3 = Frame(root, bg='#679ba7')
frame3.pack(anchor=CENTER, pady=20)
l0 = Label(frame3, bg='#679ba7', font=('Arial', 15, 'bold'))
l0.pack()
root.mainloop()

Full Code

# import library
import random
from tkinter import *


root = Tk()
root.geometry('600x600')
root.title('TechVidvan')
root.config(bg='#f7f1ea')
global value




# function
def rock():
   l1.config(text="Rock")
   global value
   value = 1
   game()




def paper():
   l1.config(text="Paper")
   global value
   value = 2
   game()




def scissor():
   l1.config(text="Scissor")
   global value
   value = 3
   game()




def rock_1():
   if value == 1:
       l0.config(text="Game Tie")
   elif value == 2:
       l0.config(text="Player Win")
   elif value == 3:
       l0.config(text="Computer Win")




def paper_1():
   if value == 1:
       l0.config(text="Computer Win")
   elif value == 2:
       l0.config(text="Game Tie")
   elif value == 3:
       l0.config(text="Player Win")




def scissor_1():
   if value == 1:
       l0.config(text="Player Win")
   elif value == 2:
       l0.config(text="Computer Win")
   elif value == 3:
       l0.config(text="Game Tie")




def game():
   computer_value = random.randint(1, 3)
   if computer_value == 1:
       l2.config(text="Rock")
       rock_1()


   elif computer_value == 2:
       l2.config(text="Paper")
       paper_1()


   elif computer_value == 3:
       l2.config(text="Scissor")
       scissor_1()




Label(root, text='Rock, Paper, and Scissors', font='arial 15 bold', bg='#f7f1ea', fg='#136577').pack()
frame = Frame(root, bg='#679ba7')
frame.pack(anchor=CENTER, pady=20)
frame0 = Frame(frame, bg='#679ba7')
frame0.pack(side=LEFT, pady=20)
Label(frame0, text="Player's move", bg='#679ba7', font=('Arial', 15, 'bold')).pack(padx=20, pady=20)
l1 = Label(frame0, bg='#f7f1ea', font=('Arial', 15, 'normal'), relief="ridge")
l1.pack(padx=20, pady=20)


frame1 = Frame(frame, bg='#679ba7')
frame1.pack(side=LEFT, pady=20)
Label(frame1, text="Computer's move", bg='#679ba7', font=('Arial', 15, 'bold')).pack(padx=20, pady=20)
l2 = Label(frame1, bg='#f7f1ea', font=('Arial', 15, 'normal'), relief="ridge")
l2.pack(padx=20, pady=20)


frame2 = Frame(root, bg='#679ba7')
frame2.pack(anchor=CENTER, pady=20)
Label(frame2, text='Select your move:', bg='#679ba7', font=('Arial', 15, 'bold')).pack()
Button(frame2, text="Rock", bg='#f7f1ea', font=('Arial', 15, 'normal'), command=rock, relief="ridge").pack(side=LEFT, padx=20, pady=20)
Button(frame2, text="Paper", bg='#f7f1ea', font=('Arial', 15, 'normal'), command=paper, relief="ridge").pack(side=LEFT, padx=20, pady=20)
Button(frame2, text="Scissor", bg='#f7f1ea', font=('Arial', 15, 'normal'), command=scissor, relief="ridge").pack(side=LEFT, padx=20, pady=20)


frame3 = Frame(root, bg='#679ba7')
frame3.pack(anchor=CENTER, pady=20)
l0 = Label(frame3, bg='#679ba7', font=('Arial', 15, 'bold'))
l0.pack()
root.mainloop()

Python Rock, Paper, and Scissor Game Output

rock paper scissors game output

rock paper scissors output

Summary

Using the Graphical user Interface (GUI), we successfully constructed a Python Rock Paper Scissors Game project. I hope you had fun building with us.