Rock Paper Scissors Game Development in Python

Python is often used for game development. There are various built-in libraries available in python that helps in game development. In this article, we will learn to develop a simple game of rock paper scissors using python.

At first let us understand the rules of Rock Paper Scissors in Python:

Rock paper scissors is a hand game played between at least two people, where different gestures indicate either of the three: rock, paper, or scissors.

The rules are that:

  • Rock beats scissors
  • Scissors beats paper
  • Paper beats rock
  • If both the players choose the same gesture, then the game is tied.

 In this article we will develop the game of rock paper scissors which will be played by a single player against the computer.

Perquisites for developing rock paper scissors in python:

To develop the game of rock paper scissors in python the following two modules are required:

  • Tkinter – easy to use the library of python, used to develop GUI application
  • Random – module in python that generates random numbers

Now we will start doing our code step by step:

Installing the packages:

pip install tkinter
pip install random

Using pip one can install any package in python. If the package is already installed it displays the message “Package already exists” otherwise it will install the specific package.

Importing the libraries:

from tkinter import *
import random

After the package’s installation is complete, we will move on to import the libraries required for our program. Once a library is installed, we can access all the functions available in that library.

Here, we will use the tkinter module for GUI application development.

The random module generates random numbers.

Designing the window for Rock Paper Scissors in Python:

win = Tk()
win.geometry('400x400')
win.title('Rock-Paper-Scissors')
win.config(bg ='#80dfff')
Label(win, text = 'Rock, Paper ,Scissors' ).pack()

In this part, we have basically designed what our output screen will look like.

  • Tk() – initializes the Tkinter
  • geometry() – gives dimension to the output window
  • title() – used to give a title to the window
  • config() – helps in customizing the window. Here we have used it to set a background color
  • Label() – it is basically a widget that contains text which the user can’t modify

user input:

choices = StringVar()
Label(win, text = 'Rock-Paper-Scissors!!! Choose One!!' ).place(x = 20,y=70)
Entry(win, textvariable = choices ).place(x=90 , y = 130)

Here, choices is a string type variable which will store the value entered by the user (rock, paper or scissors)

  • Entry() – another widget used to create input field
  • place() – places the widget at a specified position

computer input Rock Paper Scissors Python:

computer = random.randint(1,3)
if computer == 1:
    computer = 'rock'
elif computer ==2:
    computer = 'paper'
else:
    computer = 'scissors'

For computer input, we have to use the random.randint() function, which will choose a random value among 1,2, or 3 and will take the input corresponding to the integer.

  • 1 – rock
  • 2 – paper
  • 3 – scissors

Design the game of rock paper scissors Python:

Result = StringVar()
def play():
    ch = choices.get()
    if ch == computer:
        Result.set('Its a Tie')
    elif ch == 'paper' and computer == 'rock':
        Result.set('Computer selected Rock!!You Win!!')
    elif ch == 'paper' and computer == 'scissors':
        Result.set('Computer selected Scissors!!You Lose!!')
    elif ch == 'rock' and computer == 'paper':
        Result.set('Computer selected Paper!! You Lose!!')
    elif ch == 'rock' and computer == 'scissors':
        Result.set('Computer selected Scissors!!You Win!!')
    elif ch == 'scissors' and computer == 'rock':
        Result.set('Computer selected Rock!!You Lose!!')
    elif ch == 'scissors' and computer == 'paper':
        Result.set('Computer selected Paper!!You Win!!')
    else:
        Result.set('Invalid Entry: Choose: rock, paper, scissors')

In this part, we have used the if-elif-else statement to check various conditions as per the rock paper scissor rules and display the output message accordingly.

Defining Different Functions:

Reset Function:

def Reset():
    Result.set("") 
    choices.set("")

This function will reset the Result value and the choices value for next round

Exit Function():

def Exit():
    win.destroy()

This function will terminate the game window.

define buttons:

Entry(win, textvariable = Result,width = 50,).place(x=25, y = 250)

Button(win, text = 'PLAY'  ,padx =5, command = play).place(x=150,y=190)

Button(win, text = 'RESET' ,padx =5, command = Reset).place(x=70,y=310)

Button(win, text = 'EXIT'  ,padx =5, command = Exit).place(x=230,y=310)


win.mainloop()

This part will create clickable buttons which will carry out different functions

  • Button() creates buttons
  • command – calls a specific function
  • mainloop() – runs the program continually till the exit button is pressed

output:

 This is the output of our code which can be used by a user to play rock paper scissor python
Rock Paper Scissors
  • The first text box takes the input
  • To display the result, the user will select on PLAY
  • If a user wants to play again, he will have to select on RESET
  • If a user wants to exit, he will have to select on EXIT

Conclusion:

So, this was an easy and fun way to create a rock paper scissors game. It is customizable, as per a developer’s personal preference. Not just rock paper scissors, but many more games can be developed easily in Python using various tools and libraries available.

However, if you have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments