Python Program Rock Paper Scissors Game

Video games have always been a fun part of our lives so let’s create a basic version of Rock Paper Scissors game using Python. This is a fun project with a lot of learning and after successfully completing this project boredom would never surround us as we’re going to have our very own game.

So let’s start with the project.

About the Python Rock Paper Scissors Project

We are going to create a Rock, Paper, Scissors game in which we would be playing against the computer and we would be able to maintain the scores as well as we are going to add the ability to play again and again.

We will be using Python and its libraries. The first library that we will be using is Tkinter which is a widely used GUI library offered by python, we do not have to install it separately, as it comes with python itself.

Next, we will be using the random library to generate random numbers in order to determine the computer’s choice.

Project Prerequisites

A basic understanding of Python programming language and Tkinter, especially Tkinter widgets would be helpful to complete this project with ease. But don’t worry as this article will provide an explanation of every line of code as we go about building the project. You are free to choose any IDE of your choice.

Download Rock Paper Scissors Python Program

Please download the source code of python rock paper scissors: Rock Paper Scissors Python Code

Steps to develop Rock Paper Scissors Project using Python

  1. Import important libraries
  2. Define variables and dictionaries
  3. Create the overall layout of the Python Rock Paper Scissors Project
  4. Create important functions

1. Import important libraries

Code

from tkinter import *
import random

Explanation

  • First, we import the tkinter library, a widely used GUI library, after that we import random.
  • random is basically used to generate a random number from a given range.

2. Define important variables and dictionaries

Code

Comp_dict={
         "0":"Rock",
         "1":"Paper",
         "2":"Scissor"
      }
     #defining Global Variables
     your_choice=""
     Comp_choice=""
     computer_score=0
     your_score=0

Explanation

  • Comp_dict is a dictionary from which we will select the value corresponding to the key “0”,”1” or “2” and this is determined by our random function.
  • Next, we have defined a set of global variables, your_choice stores the user’s choice, Comp_choice store’s computer’s choice, and then the next set of variables stores respective scores.

3. Create the overall layout of the Python Rock Paper Scissors Project

Code

#Defining main window and all it's widgets of Python Rock Paper Scissors Project
                 root=Tk()
root.title("ProjectGurukul Rock Paper Scissor")
root.geometry('270x200')
root.config(bg="sky blue")
#text widget to display choices 
text_to_display=Text(root,height=3,width=30)
text_to_display.grid(row=0,columnspan=5,pady=10)
#buttons defined accordingly 
bttn_rock=Button(root,text="Rock",width=6,command=rock)
bttn_rock.grid(row=2,column=0,padx=10)

bttn_paper=Button(root,text="Paper",width=6,command=paper)
bttn_paper.grid(row=2,column=1,padx=5)

bttn_Scissor=Button(root,text="Scissor",width=6,command=scissor)
bttn_Scissor.grid(row=2,column=2,padx=10)

#Widget to add label specified by text
label_scores=Label(root,text="Your score         vs        Computer's score")
label_scores.grid(row=3,columnspan=5,pady=8)



text_to_scores=Text(root,height=1,width=30)
text_to_scores.grid(row=4,columnspan=5,pady=5)

#play again button to start the game again 
Play_again=Button(root,text="Play Again",command=Playagain)
Play_again.grid(row=5,columnspan=3)



mainloop()

Explanation

  • Tk() is a top level widget that is used to create the main application window in which we will be building our project.
  • The title() method is used to give a name to our application which is displayed at the top.
  • root.geometry() is used to give dimension to our root window and root.config(bg=”sky blue”) specifies the background color of our window.
  • Text() widget can be used for multiple purposes, the basic ones would be to take multiline input from the user or to display multiline text to the user.
    We would be displaying the choices and scores by using this widget.
  • grid() widget is a geometry manager which organizes the widgets properly in a grid based fashion before placing it in the root window.
  • Button() widget is used to make buttons for our rock paper scissors applications. We have given many arguments out of them root basically specifies that the button should be placed in our root window. Then, text specifies the text to be displayed on the button and width gives the width to our button. At last, the command specifies the function to be executed when the button is clicked.
  • Label() widget is used to display something, it can either be some text or image.
  • The mainloop() method basically runs the Tkinter event loop, runs and displays everything we have written in our code.

4. Create important functions

Code

#function to clear the text area where choices are displayed 
def Playagain():
    text_to_display.delete("1.0","end")

#function to update points after every game 
def points():
    text_to_scores.delete("1.0","end")
    text_to_scores.insert(END,f"  {your_score}                   {computer_score}")
    

#function to define what happens when user select Rock 
def rock():
    global computer_score
    global your_score
    your_choice="Rock"
#choosing random variable from the above defined dictionary 
    Comp_choice=Comp_dict[str(random.randint(0,2))]
#to display choices 
    text_to_display.insert(END,f"Your Choice:          {your_choice}\nComputer's Choice:    {Comp_choice}"
                           
)
#to increase the scores accordingly 
    if Comp_choice=="Paper":
        computer_score+=1
    if Comp_choice=="Scissor":
        your_score+=1
    points()

#same as the above function   
def paper():
    global computer_score
    global your_score
    your_choice="Paper"
    Comp_choice=Comp_dict[str(random.randint(0,2))]
    text_to_display.insert(END,f"Your Choice:          {your_choice}\nComputer's Choice:    {Comp_choice}"
)
    if Comp_choice=="Scissor":
        computer_score+=1
    if Comp_choice=="Rock":
        your_score+=1
    points()
def scissor():
    global computer_score
    global your_score
    your_choice="Scissor"
    Comp_choice=Comp_dict[str(random.randint(0,2))]
    text_to_display.insert(END,f"Your Choice:          {your_choice}\nComputer's Choice:    {Comp_choice}"
)
    if Comp_choice=="Rock":
        computer_score+=1
    if Comp_choice=="Paper":
        your_score+=1
    points()

Explanation

  • Playagain() is called when the Play Again button is clicked, it clears the text area where the choices are displayed for the next round of the game.
  • points() function updates the scores after every round of the game, it does so by deleting the text area where the scores are displayed and then inserting back with the updated scores.
  • rock() is called when the Rock button is clicked. We have to specify that the variables computer_score and your_score are global variables by writing the keyword global in front of them. Now, random.randint(0,2) returns a random integer between 0 and 2 both inclusive. After this, we typecast it into a string using str() function. Then we look for the value corresponding to this key in our dictionary named Comp_choice. Next line basically displays the choices. After that we update the scores according to the choices, then we call points() function to display the updated scores.
  • paper() function is exactly the same as the rock function, just the variable your_choice has changed.
  • scissor() function has the same explanation as the above function.

Final output of Python Rock Paper Scissors Project

python rock paper scissors output

Summary

Congratulations! We have successfully created our very own Rock Paper and Scissors game using Python. Hope you had fun making this project with us.

Through this project, we learnt a lot of things about python and its libraries. The first one is the Tkinter library, a widely-used GUI library and various other widgets that it offers, then we learnt about python’s random() library used to generate a random number. We also learnt an important data structure used in python called dictionary.

Your opinion matters
Please write your valuable feedback about ProjectGurukul on Google | Facebook

Leave a Reply

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