Rock Paper Scissors Code in Python with Source Code

Rock Paper Scissors Code in Python is design using Tkinter and graphical user interface(GUI). Rock Paper Scissor Game in Python is a simple desktop application developed using Python Programming Language.

The project system file contains resources files and a python script. The gameplay Graphics is smooth and the controls are easy for the users. 

Rock Paper Scissors Game in Python – Project Information’s

Project Name:Rock Paper Scissors Code in Python
Abstract:A e commerce in PHP website allows people to buy products through online.
Language Used:Python
Type:Game Application
Developer:Adones Evangelista
how to make rock paper scissors in python

In this project you can learn on how to make rock paper scissors in python and play against the computer.

This Rock Paper Scissors game in python, the first pick is going to the user and the user has to pick out anyone of these 3 shapes i.E. Rock (photo with a closed fist), Paper (photo with a flat hand), and Scissors (photo with a fist displaying the index finger and center finger extended, which appearance of a V shape).

Then the program/pc randomly selects any of those shapes and shows the winner’s name.

This Rock Paper Scissor Code Python also includes a downloadable source code for free, just find the downloadable source code below.

Anyway if you want level up your knowledge in programming especially games in python, try this new article I’ve made for you Code For Game in Python: Python Game Projects With Source Code

Before you start to create this Rock Paper Scissors Code in Python, make sure that you have PyCharm IDE installed in your computer.

By the way if you are new to python programming and you don’t know what would be the the Python IDE to use, I have here a list of Best Python IDE for Windows, Linux, Mac OS that will suit for you. I also have here How to Download and Install Latest Version of Python on Windows.

Steps on How To Make Rock Paper Scissors In Python.

Time needed: 5 minutes.

These are the Steps on How To Make Rock Paper Scissors In Python.

  • Step 1. Create a project name

    First when you finished installed the Pycharm IDE in your computer, open it and then create a “project name” after creating a project name click the “create” button.Creating project name in rock paper scissor in python

  • Step 2. Create a python file.

    Second after creating a project name, “right click” your project name and then click “new” after that click the “python file“.Creating python file name in rock paper scissor in python

  • Step 3. Name your python file.

    Third after creating a python file, Name your python file after that click “enter“.Naming a python file name in rock paper scissor in python

  • Step 4. The actual code.

    This is the actual coding on how to create Rock, Paper, Scissor Game in Python, and you are free to copy this code and download the full source code given below.

Code Explanations

1. Importing Tkinter Module

Code:

from tkinter import *

Explanation:

Tkinter is the standard GUI library for Python. Python when combined with Tkinter provides a fast and easy way to create GUI applications.

2. Importing Random Module

Code:

import random

Explanation:

In the code above, which is we import random module. This is how our computer opponent will play.

3. Module for the Design Main Screen Window

Code:

root = Tk()
root.title("ROCK, PAPER, SCISSOR GAME")
width = 690
height = 600
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (width / 2)
y = (screen_height / 2) - (height / 2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
root.resizable(0, 0)
root.config(bg="green")

Explanation:

In the code above , which is first of all you have to design the main screen window.

4. This Module is for the Image

Code:

blank_image = PhotoImage(file="resources/blank.png")
rock_player = PhotoImage(file="resources/rock_player.png")
rock_player_ado = rock_player.subsample(3, 3)
paper_player = PhotoImage(file="resources/paper_player.png")
paper_player_ado = paper_player.subsample(3, 3)
scissor_player = PhotoImage(file="resources/scissor_player.png")
scissor_player_ado = scissor_player.subsample(3, 3)
rock_computer = PhotoImage(file="resources/rock_computer.png")
paper_computer = PhotoImage(file="resources/paper_computer.png")
scissor_computer = PhotoImage(file="resources/scissor_computer.png")

Explanation:

In the code above , which is for the image we use in the game.

5. Module is for the Label

Code:

player_image = Label(root, image=blank_image)
computer_image = Label(root, image=blank_image)
player_label = Label(root, text="PLAYER")
player_label.grid(row=1, column=1)
player_label.config(bg="blue", fg="white", font=('Times New Roman', 12, 'bold'))
computer_label = Label(root, text="COMPUTER")
computer_label.grid(row=1, column=3)
computer_label.config(bg="blue", fg="white", font=('Times New Roman', 12, 'bold'))
status_label = Label(root, text="", font=('Times New Roman', 12))
status_label.config(bg="white", fg="red", font=('Times New Roman', 20, 'bold'))
player_image.grid(row=2, column=1, padx=30, pady=20)
computer_image.grid(row=2, column=3, pady=20)
status_label.grid(row=3, column=2)

Explanation:

In the code above, which is first of all you have to design the label. This display screen have a label of player, computer and status.

6. Module is for the Buttons

Code:

rock = Button(root, image=rock_player_ado, command=Rock)
paper = Button(root, image=paper_player_ado, command=Paper)
scissor = Button(root, image=scissor_player_ado, command=Scissor)
button_quit = Button(root, text="Quit", bg="Blue", fg="white", font=('Times New Roman', 18, 'bold'), command=ExitApplication)
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)

Explanation:

In the code above, which is first of all you have to design the buttons. This display screen have a buttons of rock, paper and scissor.

Complete Source Code

from tkinter import *
import random

root = Tk()
root.title("ROCK, PAPER, SCISSOR GAME")
width = 690
height = 600
screen_width = root.winfo_screenwidth()
screen_height = root.winfo_screenheight()
x = (screen_width / 2) - (width / 2)
y = (screen_height / 2) - (height / 2)
root.geometry("%dx%d+%d+%d" % (width, height, x, y))
root.resizable(0, 0)
root.config(bg="green")

# ++++++++++++++++++++IMAGES++++++++++++++++++++++++++
blank_image = PhotoImage(file="resources/blank.png")
rock_player = PhotoImage(file="resources/rock_player.png")
rock_player_ado = rock_player.subsample(3, 3)
paper_player = PhotoImage(file="resources/paper_player.png")
paper_player_ado = paper_player.subsample(3, 3)
scissor_player = PhotoImage(file="resources/scissor_player.png")
scissor_player_ado = scissor_player.subsample(3, 3)
rock_computer = PhotoImage(file="resources/rock_computer.png")
paper_computer = PhotoImage(file="resources/paper_computer.png")
scissor_computer = PhotoImage(file="resources/scissor_computer.png")


# ++++++++++++++++++++METHODS++++++++++++++++++++++++++
def Rock():
global player_option
player_option = 1
player_image.configure(image=rock_player)
MatchProcess()


def Paper():
global player_option
player_option = 2
player_image.configure(image=paper_player)
MatchProcess()


def Scissor():
global player_option
player_option = 3
player_image.configure(image=scissor_player)
MatchProcess()


def MatchProcess():
computer_option = random.randint(1, 3)
if computer_option == 1:
computer_image.configure(image=rock_computer)
RockCom()
elif computer_option == 2:
computer_image.configure(image=paper_computer)
PaperCom()

elif computer_option == 3:
computer_image.configure(image=scissor_computer)
ScissorCom()


def RockCom():
if player_option == 1:
status_label.config(text="Game Tie")
elif player_option == 2:
status_label.config(text="Player Win")
elif player_option == 3:
status_label.config(text="Computer Win")


def PaperCom():
if player_option == 1:
status_label.config(text="Computer Win")
elif player_option == 2:
status_label.config(text="Game Tie")
elif player_option == 3:
status_label.config(text="Player Win")


def ScissorCom():
if player_option == 1:
status_label.config(text="Player Win")
elif player_option == 2:
status_label.config(text="Computer Win")
elif player_option == 3:
status_label.config(text="Game Tie")


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


# ++++++++++++++++++++LABEL WIDGET++++++++++++++++++++++++++
player_image = Label(root, image=blank_image)
computer_image = Label(root, image=blank_image)
player_label = Label(root, text="PLAYER")
player_label.grid(row=1, column=1)
player_label.config(bg="blue", fg="white", font=('Times New Roman', 12, 'bold'))
computer_label = Label(root, text="COMPUTER")
computer_label.grid(row=1, column=3)
computer_label.config(bg="blue", fg="white", font=('Times New Roman', 12, 'bold'))
status_label = Label(root, text="", font=('Times New Roman', 12))
status_label.config(bg="white", fg="red", font=('Times New Roman', 20, 'bold'))
player_image.grid(row=2, column=1, padx=30, pady=20)
computer_image.grid(row=2, column=3, pady=20)
status_label.grid(row=3, column=2)

# ++++++++++++++++++++BUTTON WIDGET++++++++++++++++++++++++++
rock = Button(root, image=rock_player_ado, command=Rock)
paper = Button(root, image=paper_player_ado, command=Paper)
scissor = Button(root, image=scissor_player_ado, command=Scissor)
button_quit = Button(root, text="Quit", bg="Blue", fg="white", font=('Times New Roman', 18, 'bold'), command=ExitApplication)
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)

# ++++++++++++++++++++INITIALIZATION++++++++++++ ++++++++++++++
if __name__ == '__main__':
root.mainloop()

Output

Rock Paper Scissors code in python
Rock Paper Scissors code in python

How To Run The Rock Paper Scissors Code in Python?

To run this project, you must have installed a Pycharm on your PC (for Windows). This article about on How To Code Rock Paper Scissors in Python is for educational purposes only!

After downloading the project you must follow the steps below:

Step 1: Unzip the file or Extract the file

Step 2: Double click the rock_paper_scissor

Step 3: The project is ready to run

Downloadable Source Code 

I have here the list of Best Python Project with Source code free to download for free, I hope this can help you a lot.

Summary

Rock Paper Scissors Code in Python with Source Code is free to download, Using Python Programming Language we can also create very interesting game.

You can use Python for developing desktop GUI applications, websites and web applications. This simple Rock-Paper-Scissor game in python is using without any external game libraries like PyGame.

Inquiries

If you have any questions or suggestions about Rock Paper Scissors Code in Python with Source Code, please feel free to leave a comment below.

2 thoughts on “Rock Paper Scissors Code in Python with Source Code”

  1. i am facing this problem

    Traceback (most recent call last):
    File “C:/Users/P/AppData/Local/Programs/Python/Python39-32/rock,paper,scissorgame.py”, line 17, in
    blank_image = PhotoImage(file=”resources/blank.png”)
    File “C:\Users\P\AppData\Local\Programs\Python\Python39-32\lib\tkinter\__init__.py”, line 4063, in __init__
    Image.__init__(self, ‘photo’, name, cnf, master, **kw)
    File “C:\Users\P\AppData\Local\Programs\Python\Python39-32\lib\tkinter\__init__.py”, line 4008, in __init__
    self.tk.call((‘image’, ‘create’, imgtype, name,) + options)
    _tkinter.TclError: couldn’t open “resources/blank.png”: no such file or directory

Leave a Comment