Rock Paper Scissors Python Program – Develop Simple Python Game

Rock Paper Scissors is a GUI-based game project built using Python Tkinter module and random module. It’s also called the stone paper scissor game which is most popular among kids. Here we are creating a simple yet fun and popular game called Rock, Paper, Scissors using graphical user interface (GUI) in python programming language.

What is the Rock Paper Scissors Game?

The game of Rock, Paper, Scissors is a handy game usually played with two people (although technically it can have many players). Hand movements are like stone (with a closed fist), scissors (with V-shaped fingers), or paper (with a flat hand). Paper beats rock, rock beats scissors and scissors beats paper. Two or more players will simultaneously select the touch of their hand (called a rock, paper, or scissors) and the winner is determined by the touch of the other. If the touch is the same it means it is equal and no player wins.

Python Rock Paper Scissors Game Project Details

Here we are playing this rock paper scissor game with the computer. So we have two players: user and computer. We can play it anytime, anywhere. We are using Tkinter and a random module for creating this game in the python programming language.

There are some winning rules which are as follows:

  • Rock vs paper → Paper wins
  • Rock vs scissor → Rock wins
  • Scissor vs paper → Scissor wins.

In this python project users can click on a particular button they want i.e rock paper or scissors and the game will start.

Python Rock Paper Scissors Game- Project Prerequisite

This project requires good knowledge of python and the Tkinter library. Tkinter is the python binding to the Tk toolkit which is used across many programming languages for building the Graphical user interface which is a GUI.

Download Rock Paper Scissors Game Python Code

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

Steps to Build a Python Rock Paper Scissors Game

  1. Import modules
  2. Initializing the window
  3. Setting images
  4. Function for making rock paper scissors
  5. Function for making computer rock paper scissor
  6. Function for matching
  7. Exit function
  8. Define buttons and labels widget

Step 1- Importing Modules

#PythonGeeks - import library
import random
from tkinter import *

Code Explanation-

  • Tkinter module – Tkinter is the standard interface in python for creating a GUI that is Graphical User Interface.
  • tkinter import * – import everything from the module.
  • Random module – Random module is an in-built module of Python which is used to generate random words from list[].

Step 2- Initializing the window

#PythonGeeks - Initialize window
root = Tk()
root.title("ROCK, PAPER, SCISSORS GAME USING PYTHONGEEKS")
width = 650
height = 580
window_width = root.winfo_screenwidth()
window_height = root.winfo_screenheight()
x = (window_width / 2) - (width / 2)
y = (window_height / 2) - (height / 2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
root.resizable(0, 0)
root.config(bg="#e3f4f1")

Code Explanation-

  • root – Initializing the root window of Rock paper scissor game in python.
  • .title – Use to set title to window.
  • Width and height variable setting the width of the window.
  • winfo_screenwidth() – It is tkinter tool which returns the width of screen in pixels
  • winfo_screenheight() – It is tkinter tool which returns the height of the screen in pixels.
  • .geometry – For setting dimensions of a window in pixels.
  • resizable() – Used to change the size of the tkinter root window according to the user’s need.
  • .config – Used to configure attributes to the window, such as background color.

Step 3- Setting images

Blank_img = PhotoImage(file="resources/blank.png")
Player_Rock = PhotoImage(file="resources/rock_player.png")
Player_Rock_ado = Player_Rock.subsample(3, 3)
Player_Paper = PhotoImage(file="resources/paper_player.png")
Player_Paper_ado = Player_Paper.subsample(3, 3)
Player_Scissor = PhotoImage(file="resources/scissor_player.png")
Player_Scissor_ado = Player_Scissor.subsample(3, 3)
Computer_Rock = PhotoImage(file="resources/rock_computer.png")
Computer_Paper = PhotoImage(file="resources/paper_computer.png")
Computer_Scissor = PhotoImage(file="resources/scissor_computer.png")

Code Explanation-

  • Here we are creating different variables and storing images in it.
  • PhotoImage() – This tkinter tool is used for displaying an image on a label or button widget.

Step 4- Function for making rock paper scissor

#PythonGeeks- Function for making rock paper scissor
def Rock():
    global player_option
    player_option = 1
    Image_Player.configure(image=Player_Rock)
    Matching()
 
def Paper():
    global player_option
    player_option = 2
    Image_Player.configure(image=Player_Paper)
    Matching()
 
def Scissor():
    global player_option
    player_option = 3
    Image_Player.configure(image=Player_Scissor)
    Matching()

Code Explanation-

  • Rock() – Function for creating rock on the game window.
  • Paper() – Function for creating paper on the game window.
  • Scissor() – Function for creating scissor on the game window.
  • Declare global variable player_option as 1, 2 or 3.
  • Setting the particular image using the configure method.
  • Call matching function at the end.

Step 5- Function for making computer rock paper scissor

def Comp_Rock():
    if player_option == 1:
        Label_Status.config(text="Game Tie")
    elif player_option == 2:
        Label_Status.config(text="Player Win")
    elif player_option == 3:
        Label_Status.config(text="Computer Win")
 
def Comp_Paper():
    if player_option == 1:
        Label_Status.config(text="Computer Win")
    elif player_option == 2:
        Label_Status.config(text="Game Tie")
    elif player_option == 3:
        Label_Status.config(text="Player Win")
 
def Comp_Scissor():
    if player_option == 1:
        Label_Status.config(text="Player Win")
    elif player_option == 2:
        Label_Status.config(text="Computer Win")
    elif player_option == 3:
        Label_Status.config(text="Game Tie")

Code Explanation-

  • Comp_Rock() – Function for creating rock for computer on the game window.
  • Here we are using the if-elif condition. In the case of Comp_Rock function if the player option is equal to 1 then the game will tie because the user and computer both choose rock. And if equal to 2, players will win and if it is equal to 3, computer will win.
  • Comp_Paper() – Function for creating paper for computer on the game window.
  • Here we are using the if-elif condition. In the case of Comp_Paper function if the player option is equal to 1 then the computer will win and if equal to 2 game will tie and equals to 3 player will win.
  • Comp_Scissor() – Function for creating scissor for computer on the game window.
  • Here we are using the if-elif condition. In the case of Comp_Scissor function if the player option is equal to 1 then the players will win and if equal to 2 computer will win and equals to 3 game will tie.

Step 6- Function for matching

def Matching():
    computer_option = random.randint(1, 3)
    if computer_option == 1:
        Image_Computer.configure(image=Computer_Rock)
        Comp_Rock()
 
    elif computer_option == 2:
        Image_Computer.configure(image=Computer_Paper)
        Comp_Paper()
 
    elif computer_option == 3:
        Image_Computer.configure(image=Computer_Scissor)
        Comp_Scissor()

Code Explanation-

  • Matching() – Function for matching users and computer options.
  • randint() – This method returns an integer from the specified range that is 1, 3.
  • We are using the if elif condition, if computer_option is equal to 1 then we will configure the rock image and call Comp_Rock function.
  • If computer_option is equal to 2 then we will configure the paper image and call Comp_Paper function.
  • If computer_option is equal to 3 then we will configure the scissor image and call the Comp_Scissor function.

Step 7- Exit function

def Exit():
    root.destroy()
    exit()

Code Explanation-

  • Exit() – Function for exiting game window.

Step 8- Define buttons and labels widget

Image_Player = Label(root, image=Blank_img)
Image_Computer = Label(root, image=Blank_img)
Label_Player = Label(root, text="PLAYER")
Label_Player.grid(row=1, column=1)
Label_Player.config(bg="#e8c1c7", fg="black", font=('Times New Roman', 18, 'bold'))
Label_Computer = Label(root, text="COMPUTER")
Label_Computer.grid(row=1, column=3)
Label_Computer.config(bg="#e8c1c7", fg="black", font=('Times New Roman', 18, 'bold'))
Label_Status = Label(root, text="", font=('Times New Roman', 12))
Label_Status.config(fg="black", font=('Times New Roman', 20, 'bold','italic'))
Image_Player.grid(row=2, column=1, padx=30, pady=20)
Image_Computer.grid(row=2, column=3, pady=20)
Label_Status.grid(row=3, column=2)
 
rock = Button(root, image=Player_Rock_ado, command=Rock)
paper = Button(root, image=Player_Paper_ado, command=Paper)
scissor = Button(root, image=Player_Scissor_ado, command=Scissor)
button_quit = Button(root, text="Quit", bg="red", fg="white", font=('Times New Roman', 25, 'bold'), command=Exit)
rock.grid(row=4, column=1, pady=30)
paper.grid(row=4, column=2, pady=30)
scissor.grid(row=4, column=3, pady=30)
button_quit.grid(row=5, column=2)
 
if __name__ == '__main__':
    root.mainloop()

Code Explanation-

  • label – In this variable, we use the label widget for displaying the box in which we give text. Then call configure to set font, font size, and background color.
  • Creating button for rock paper and scissor.

Python Rock Paper Scissors Game Output

python rock paper scissors game output

Summary

We have successfully created a python Rock Paper Scissors Game project using the Graphical user Interface(GUI). We have learned about the Tkinter module.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google | Facebook


1 Response

  1. J says:

    Why did we use import random and not import NumPy?
    Also what is the size of blank image?

Leave a Reply

Your email address will not be published. Required fields are marked *