Rock Paper Scissor Game Using GUI in Python

‘Rock Paper Scissors’ is a very simple yet fun and popular game all over the world. Writing the code of this game in the Python language is very easy and convenient, compared to writing it in other programming languages.

This is a hand game usually played by two people (although you can technically have more players). In this, paper beats rock; rock beats scissors; and scissors beat paper. The players will simultaneously choose their hand gestures and a winner is determined depending on what beats another. If the gestures are the same, then it is a tie and neither player wins.

In this article, we will learn to create a ‘Rock Paper Scissor Game’ in python using GUI. This game will be played between the user/programmer and the system/computer itself.

Is Rock Paper Scissors Suitable For Your College Projects?

Yes. This application will serve to be an excellent project.  First, because of the utility of the application in real life. Second, because of the technology we are using to implement this application i.e. Python GUI along with Tinker library modules and widgets.

Moreover, this is an extremely interesting and regular game and thus, you will have fun and you will learn while coding this.

Creating a  GUI ‘ROCK PAPER SCISSORS GAME’ Using Python Language.

We will be creating GUI ‘PASSWORD GENERATOR’ using the Jetbrains Pycharm IDE. We use the Tkinter library to create this fun game. This is because we can then use its various widgets and functions.

Importing Tkinter Library and Starter Code

Import the Tkinter and random library as shown below. Create an instance of Tkinter’s window, Tk(). Set the title of the window as ’ Rock Papers Scissors’. Now, set the dimensions of the window to 500 x 600 pixels using the Geometry method.

Next, set the background colour of the window to white. Then, save any three pictures of rock, paper, and scissors and save them in your images folder, in the rps folder stored in the GUI (C Drive).

Further, initialise/define all the three i.e rock, paper, and scissors to their respective locations using the PhotoImage function, as shown below.

The Code Block


Picking Out Random Images Out Of The Three By The System

First, create a list consisting of all the three- rock, paper, and scissors ( as defined in the previous step). Here, the list is named ‘image_list’.

Next, we create a variable named pick_number to pick a random number between 0 and 2 (0- rock, 1-paper, 2- scissor).  To get a random number, we use the randint() function. For this, we include a random library and import randint at the beginning of the code. ( check code block of step 1). This will enable us to choose one out of the three, randomly and store it in the variable pick_number.

Next, we display the random image corresponding to the random number picked above using the label function. In this, pass the root and the image_list created above, with pick_number as the argument.

Add a little padding to push it a little down the window using the pack function. ( please check the code below).

The Code Block

SPIN Button And Function

Create a function named spin.

Pick another random number here in the range of 0 and 2 using the randint function, store it in pick number. Next, we set a new image to be displayed here using the config function. In the argument, we pass the randomly picked image i.e. the image_list and pick-number, as shown below.


Further, we create a Spin button using the Button function. We pass the root, the text=” Spin!” and the command i.e. the spin function we have created above. Add a little padding in this too.

Finally, this will ensure that a new image, randomly selected by the system, is displayed on the screen every time we click on the SPIN button.

The Code Block

Create a Combo Drop Down box for Users’ Choice

For this, we import “ to “ from the Tkinter library ( done in step 1, check the code block of step 1) so that we can use the ttk widgets, specifically the Combobox( for the dropdown).

Next, we create a variable user_choice and equate it to a ttk Combobox where we pass the root and values: rock, paper, and scissor.

Don’t forget to set the current value as 0(rock). This is our default choice, which will be displayed every time we start the program.

Finally, pack the box to give it padding.

The Code Block

Label Showing If You Have Won Or Not

Create a variable win_lose_label and set the Label using a function named Label. Pass the root and an empty text and set the font style to Helvetica and font size to 18. Set the background to White as well. Further, pack this too for padding along the y axis. This is the last part of the code. And the code ends here. However, we are left with the main logic to be written in the Spin function.

End the code with root. mainloop().

The Code Block

Logic to Determine If We Have Won or Not.

We go back to the spin function we created in STEP 3.

Note that the choices displayed randomly by the System are in the form of numbers and the user choices are values(text), so we can’t directly compare them.

Hence, to compare and decide the winner, we convert the dropdown choices/user choices into numbers, using if and assignment of the values( rock, paper, and scissors) to numbers ranging from 0 to 2 accordingly.

These numbers will be stored in the variable ‘user_choice_value’

Next, compare the user_choice_value with pick_number .(the random no. generated by the system)

We do this separately for each user_choice_value.

If user_choice_value is 0 i.e. rock, pick_number can be 0,1 or 2.

If it is 0, then display that ‘IT’S A TIE’.

If it is 1, then display that ‘ PAPER COVERS ROCK. U LOSE.’

If it is 2, then display that ‘ROCK SMASHES SCISSORS. U WIN.’

If user_choice_value is 1 i.e. paper, pick_number can be 0,1 or 2.

If it is 1, then display that ‘IT’S A TIE.’

If it is 0, then display that ‘PAPER COVERS ROCK. U WIN.

If it is 2, then display that ‘SCISSORS CUTS PAPER. U LOSE.’

If user_choice_value is 2 i.e. scissors, pick_number can be 0,1 or 2.

If it is 2, then display that ‘IT’S A TIE.’

If it is 0, then display that ‘ROCK SMASHES SCISSORS. U LOSE.’

If it’s 1, then display that ‘SCISSORS CUTS PAPER. U WIN.’

The Code Block

Now, our final program is ready for execution.

The Conclusion

You can create a Rock Paper Scissors Game as we have done here. But developing a Rock Paper Scissors Game using Python GUI makes it simple and easier to code and comprehend too.

Please note that this post is divided into parts for the maintainability of the code and your easy understanding.

About the author

Shalini Salotra