Home/Blog/Learn to Code/Python Tkinter tutorial: Build a Rock-Paper-Scissors game
Home/Blog/Learn to Code/Python Tkinter tutorial: Build a Rock-Paper-Scissors game

Python Tkinter tutorial: Build a Rock-Paper-Scissors game

8 min read
Jan 15, 2024

Become a Software Engineer in Months, Not Years

From your first line of code, to your first day on the job — Educative has you covered. Join 2M+ developers learning in-demand programming skills.

Python Tkinter Games Series#

  • Build a number guessing game#
  • Build a jumbled words game from scratch#
  • Build a Rock-Paper-Scissors game#
  • Create a love percentage calculator game#
  • Create a F.L.A.M.E.S. game from scratch#

This article is part of a Python Tkinter series written by Pratik Shukla. And stay tuned for more Tkinker articles from Pratik!

This series aims to introduce you to Tkinter, Python’s standard interface to the Tk GUI toolkit, and help you build interactive games you can add to your resume. To get up to speed on Tkinter, read our first article in this series, Python Tkinter Tutorial: build a number guessing game. Once you understand the basics, come back here to build a Rock-Paper-Scissors game from scratch!

Today, we will cover:


Rock-Paper-Scissors game walkthrough#

In this article, we’ll develop a modified version of the famous childhood game Rock-Paper-Scissors. If you have watched The Big Bang Theory (season 2, episode 8), Sheldon Cooper expands the Rock-Paper-Scissors game by adding two more objects: Lizard and Spock.

Let’s develop the same game using Python 3 and Tkinter. We can name our game Rock-Paper-Scissors-Lizard-Spock.


Rules and how to play#

  1. Rock crushes Scissors

  2. Rock crushes Lizard

  3. Paper covers Rock

  4. Paper disproves Spock

  5. Scissors cuts Paper

  6. Scissors decapitates Lizard

  1. Lizard poisons Spock

  2. Lizard eats Paper

  3. Spock smashes Scissors

  4. Spock vaporizes Rock

  5. Two same objects is a draw


Walkthrough of the program#

When the user runs the program, they have to click one of the five available objects:

  • Rock
  • Paper
  • Scissors
  • Lizard
  • Spock

When the user selects one of the objects, our program will randomly select one object. Then, it will go through the set of rules to declare whether the user won, lost, or drew the game. The result will be displayed on the second row of our application.

When the user presses any of the buttons, the game will restart. If the user wants to close the game, then they press the “X” button on the title bar. At the start of the game, we display hand symbols for the particular objects. When the user selects an object, it will transform the display into a single graphic image. Our program also selects an object, and displays the additional graphical image for the selected object.

Complete flowchart for the game
Complete flowchart for the game

Final result#

Start window (1)
1 of 4

Implementation in Python (10 steps)#

Now that we have a sense of what the Rock-Paper-Scissors game will entail, let’s walk step-by-step through the process in Python. If you don’t yet know how to set up Tkinter, check out our first article in this series for a brief tutorial.


1. Import required libraries#

#Import the required libraries :
from tkinter import *
import random
import simpleaudio as sa
  • tkinter: to add widgets in our application
  • random: to generate a random number
  • simpleaudio: to play the sound files

2. Create main Tkinter window#

root = Tk()
root.configure(bg="#000000")
root.geometry('+0+0')
root.iconbitmap("Game.ico")
root.title("Rock-Paper-Scissor-Lizard-Spock")
root.resizable(width=False,height=False)
  • root = Tk( ): This is used to initialize our Tkinter module.
  • root.configure( ): We use this to specify the background color for our application. In our case, the background color will be black.
  • root.geometry( ): We use this to specify at which location our application window will open. It will open at the top-left corner.
  • root.iconbitmap( ): We use it to set the icon in the title bar for our application window. This function only accepts .ico files.
  • root.title( ): We use it to set the title for our application.
  • root.resizable( ): Here, we are using it to prevent the users from resizing our main window.

3. Importing sound files#

#To play sound files :
start = sa.WaveObject.from_wave_file("Start.wav")
Win = sa.WaveObject.from_wave_file("Win.wav")
Lose = sa.WaveObject.from_wave_file("Lose.wav")
Draw = sa.WaveObject.from_wave_file("Draw.wav")
start.play()

Now, we will add some sound files to play at various events. When our program starts, it will play the Start.wav file. We’ll play the other three files when the user wins the game, loses the game, or draws the game, respectively.

One thing to notice is that it only accepts .wav files. First, we need to load the sound file in an object. Then, we can play it by using the .play( ) method.


4. Loading images in our application#

We will use various images in our application. Before we can use those images, we first need to load the images into our application by using the PhotoImage class.

#Hand images :
rockHandPhoto = PhotoImage(file="Rock_1.png")
paperHandPhoto = PhotoImage(file="Paper_1.png")
scissorHandPhoto = PhotoImage(file="Scissor_1.png")
lizardHandPhoto = PhotoImage(file="Lizard_1.png")
spockHandPhoto = PhotoImage(file="Spock_1.png")
#Graphical images :
rockPhoto = PhotoImage(file="Rock_P.png")
paperPhoto = PhotoImage(file="Paper_P.png")
scissorPhoto = PhotoImage(file="Scissor_P.png")
lizardPhoto = PhotoImage(file="Lizard_P.png")
spockPhoto = PhotoImage(file="Spock_P.png")
#Decision image :
decisionPhoto = PhotoImage(file="Decision_Final.png")
#Result images :
winPhoto = PhotoImage(file="G_WIN.png")
losePhoto = PhotoImage(file="G_LOST.png")
tiePhoto = PhotoImage(file="G_DRAW.png")

First, we have hand images for our objects. All five images will be shown to the user at the start of the game. The user has to choose one object from those images.

After the user clicks an image, our program will show us the graphical image for that object. The user has to select one object, and our program will also select one object. Our program will only show those two graphical images, and the rest will disappear.

Now, we display a simple decision image that will change based on the results of the game. We have different images for our results.

  • If user wins
  • If user loses
  • If there is a tie

5. Add Tkinter widgets#

#Initialize the button variables :
rockHandButton = " "
paperHandButton = " "
scissorHandButton = " "
lizardHandButton= " "
spockHandButton = " "
#Create the result button :
resultButton = Button(root,image=decisionPhoto)
#Set the variable to True
click = True
  • Initialize the variable for five buttons.
  • Here, we create the result button to show us the final result.
  • We set the click variable to True, so our program keeps running until it’s set to False (we will see more about this in the next sections).

6. Play( ) function#

def play():
global rockHandButton,paperHandButton,scissorHandButton,lizardHandButton,spockHandButton
#Set images and commands for buttons :
rockHandButton = Button(root,image = rockHandPhoto, command=lambda:youPick("Rock"))
paperHandButton = Button(root,image = paperHandPhoto, command=lambda:youPick("Paper"))
scissorHandButton = Button(root,image = scissorHandPhoto, command=lambda:youPick("Scissor"))
lizardHandButton = Button(root,image= lizardHandPhoto,command=lambda:youPick("Lizard"))
spockHandButton = Button(root,image= spockHandPhoto,command=lambda:youPick("Spock"))
#Place the buttons on the window :
rockHandButton.grid(row=0,column=0)
paperHandButton.grid(row=0,column=1)
scissorHandButton.grid(row=0,column=2)
lizardHandButton.grid(row=0,column=3)
spockHandButton.grid(row=0,column=4)
#Add space :
root.grid_rowconfigure(1, minsize=50)
#Place resultButton on the window :
resultButton.grid(row=2,column=0,columnspan=5)

Here, we create buttons for our objects. We are going to set images for the buttons, and when it’s pressed, it will go to the youPick( ) function with the string name of the object clicked.

Then, we arrange the buttons on the main window using the .grid( ) method. Here, we’re adding a blank space in the first row with .grid_rowconfigure( ). Then, we place our result button on the second row. We’re using columnspan to center our result button.


7. Computer’s turn#

Our computer will randomly select one of the five available objects and return a string value for that choice.

def computerPick():
choice = random.choice(["Rock","Paper","Scissor","Lizard","Spock"])
return choice

8. Main function: youPick( )#

In this function, our program will display the graphical images for the selected objects. It will delete the rest of the objects. It will also apply a set of rules to generate the result.

def youPick(yourChoice):
global click
compPick = computerPick()
if click==True:

We are storing the computer’s choice in the compPick variable. We’ll use it to decide the result.

User selects Rock:

This block of code is used if the user selects Rock. The command in the play( ) function sends along a string that represents the object selected by the user. We are storing it in the yourChoice variable. Now, there are five possibilities for the computer.

Now, we have to develop rules for each of them. Notice that when the user and computer have selected an object, they are not allowed to change it. That’s why we are changing the click variable to False.

Since the user has selected Rock, we want our first image to turn into the graphical image for Rock. Now, if the computer chooses Rock, then we want our second image to turn into the graphical image for Rock. To change the image of a button, we use .configure( ) method.

We want the rest of the three images to disappear. To make them disappear, we use the .grid_forget( ) method. Our program will also play the draw audio. Now, we develop similar rules for the rest of the objects.

if compPick == "Rock":
paperHandButton.configure(image=rockPhoto)
resultButton.configure(image=tiePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Draw.play()
click = False
elif compPick == "Paper":
paperHandButton.configure(image=paperPhoto)
scissorHandButton.grid_forget()
resultButton.configure(image=losePhoto)
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif compPick == "Scissor":
paperHandButton.configure(image=scissorPhoto)
scissorHandButton.grid_forget()
resultButton.configure(image=winPhoto)
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
elif compPick =="Lizard":
paperHandButton.configure(image=lizardPhoto)
scissorHandButton.grid_forget()
resultButton.configure(image=winPhoto)
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
else :
paperHandButton.configure(image=spockPhoto)
scissorHandButton.grid_forget()
resultButton.configure(image=losePhoto)
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False

User selects Paper:

See the rules from above to understand the rules for when the user selects Paper. Look at the code below, which follows the same rules as Rock.

elif yourChoice == "Paper":
rockHandButton.configure(image=paperPhoto)
if compPick == "Rock":
paperHandButton.configure(image=rockPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif compPick == "Paper":
paperHandButton.configure(image=paperPhoto)
resultButton.configure(image=tiePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Draw.play()
click = False
elif compPick == "Scissor":
paperHandButton.configure(image=scissorPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif compPick =="Lizard":
paperHandButton.configure(image=lizardPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
else :
paperHandButton.configure(image=spockPhoto)
resultButton.configure(image=winPhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False

User selects Scissors:

See the rules from above to understand the rules for when the user selects Scissors. Look at the code below, which follows the same rules as Rock and Paper.

elif yourChoice=="Scissor":
rockHandButton.configure(image=scissorPhoto)
if compPick == "Rock":
paperHandButton.configure(image=rockPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif compPick == "Paper":
paperHandButton.configure(image=paperPhoto)
resultButton.configure(image=winPhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
elif compPick=="Scissor":
paperHandButton.configure(image=scissorPhoto)
resultButton.configure(image=tiePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Draw.play()
click = False
elif compPick == "Lizard":
paperHandButton.configure(image=lizardPhoto)
resultButton.configure(image=winPhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
else:
paperHandButton.configure(image=spockPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False

User selects Lizard

See the rules from above to understand the rules for when the user selects Lizard. Look at the code below, which follows the same rules as the others.

elif yourChoice=="Lizard":
rockHandButton.configure(image=lizardPhoto)
if compPick == "Rock":
paperHandButton.configure(image=rockPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif compPick == "Paper":
paperHandButton.configure(image=paperPhoto)
resultButton.configure(image=winPhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
elif compPick=="Scissor":
paperHandButton.configure(image=scissorPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif compPick == "Lizard":
paperHandButton.configure(image=lizardPhoto)
resultButton.configure(image=tiePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Draw.play()
click = False
else:
paperHandButton.configure(image=spockPhoto)
resultButton.configure(image=winPhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False

User selects Spock:

See the rules from above to understand the rules for when the user selects Spock. Look at the code below, which follows the same rules as the others.

elif yourChoice=="Spock":
rockHandButton.configure(image=spockPhoto)
if compPick == "Rock":
paperHandButton.configure(image=rockPhoto)
resultButton.configure(image=winPhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
elif compPick == "Paper":
paperHandButton.configure(image=paperPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif compPick=="Scissor":
paperHandButton.configure(image=scissorPhoto)
resultButton.configure(image=winPhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
elif compPick == "Lizard":
paperHandButton.configure(image=lizardPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
else:
paperHandButton.configure(image=spockPhoto)
resultButton.configure(image=tiePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Draw.play()
click = False

9. Play again#

After we get our results, if we want to play again, we simply have to click any of the buttons. It will transform the display to the original five hand images. In order to do this, we have to display those hidden images. We will set the value of the click variable to True. Then, we will play the start sound file so that when the user enters a new game, audio will be played.

else:
#To reset the game :
if yourChoice=="Rock" or yourChoice=="Paper" or yourChoice=="Scissor" or yourChoice=="Lizard" or yourChoice=="Spock":
rockHandButton.configure(image=rockHandPhoto)
paperHandButton.configure(image=paperHandPhoto)
scissorHandButton.configure(image=scissorHandPhoto)
lizardHandButton.configure(image=lizardHandPhoto)
spockHandButton.configure(image=spockHandPhoto)
resultButton.configure(image=decisionPhoto)
#Get back the deleted buttons :
scissorHandButton.grid(row=0,column=2)
lizardHandButton.grid(row=0,column=3)
spockHandButton.grid(row=0,column=4)
#Set click = True :
click=True
#Play the sound file :
start.play()

10. Call the function#

Now we call the play function, and it’ll take care of the rest of the functions internally. To close the application, we press the “X” button on the title bar.

#Calling the play function :
play()
#Enter the main loop :
root.mainloop()

Putting it all together#

Take a look at the complete code for this Python Tkinter game.

Want to download the code and images? You can download all the necessary images and code from our Google Drive folder titled R_P_S_L_S.

#Import the required libraries :
from tkinter import *
import random
import simpleaudio as sa
root = Tk()
root.configure(bg="#000000")
root.geometry('+0+0')
root.iconbitmap("Game.ico")
root.title("Rock-Paper-Scissor-Lizard-Spock")
root.resizable(width=False,height=False)
#To play sound files :
start = sa.WaveObject.from_wave_file("Start.wav")
Win = sa.WaveObject.from_wave_file("Win.wav")
Lose = sa.WaveObject.from_wave_file("Lose.wav")
Draw = sa.WaveObject.from_wave_file("Draw.wav")
start.play()
#Hand images :
rockHandPhoto = PhotoImage(file="Rock_1.png")
paperHandPhoto = PhotoImage(file="Paper_1.png")
scissorHandPhoto = PhotoImage(file="Scissor_1.png")
lizardHandPhoto = PhotoImage(file="Lizard_1.png")
spockHandPhoto = PhotoImage(file="Spock_1.png")
#Graphical images :
rockPhoto = PhotoImage(file="Rock_P.png")
paperPhoto = PhotoImage(file="Paper_P.png")
scissorPhoto = PhotoImage(file="Scissor_P.png")
lizardPhoto = PhotoImage(file="Lizard_P.png")
spockPhoto = PhotoImage(file="Spock_P.png")
#Decision image :
decisionPhoto = PhotoImage(file="Decision_Final.png")
#Result images :
winPhoto = PhotoImage(file="G_WIN.png")
losePhoto = PhotoImage(file="G_LOST.png")
tiePhoto = PhotoImage(file="G_DRAW.png")
#Initialize the button variables :
rockHandButton = " "
paperHandButton = " "
scissorHandButton = " "
lizardHandButton= " "
spockHandButton = " "
#Create the result button :
resultButton = Button(root,image=decisionPhoto)
#Set the variable to True
click = True
def play():
global rockHandButton,paperHandButton,scissorHandButton,lizardHandButton,spockHandButton
#Set images and commands for buttons :
rockHandButton = Button(root,image = rockHandPhoto, command=lambda:youPick("Rock"))
paperHandButton = Button(root,image = paperHandPhoto, command=lambda:youPick("Paper"))
scissorHandButton = Button(root,image = scissorHandPhoto, command=lambda:youPick("Scissor"))
lizardHandButton = Button(root,image= lizardHandPhoto,command=lambda:youPick("Lizard"))
spockHandButton = Button(root,image= spockHandPhoto,command=lambda:youPick("Spock"))
#Place the buttons on window :
rockHandButton.grid(row=0,column=0)
paperHandButton.grid(row=0,column=1)
scissorHandButton.grid(row=0,column=2)
lizardHandButton.grid(row=0,column=3)
spockHandButton.grid(row=0,column=4)
#Add space :
root.grid_rowconfigure(1, minsize=50)
#Place result button on window :
resultButton.grid(row=2,column=0,columnspan=5)
def computerPick():
choice = random.choice(["Rock","Paper","Scissor","Lizard","Spock"])
return choice
def youPick(yourChoice):
global click
compPick = computerPick()
if click==True:
if yourChoice == "Rock":
rockHandButton.configure(image=rockPhoto)
if compPick == "Rock":
paperHandButton.configure(image=rockPhoto)
resultButton.configure(image=tiePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Draw.play()
click = False
elif compPick == "Paper":
paperHandButton.configure(image=paperPhoto)
scissorHandButton.grid_forget()
resultButton.configure(image=losePhoto)
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif compPick == "Scissor":
paperHandButton.configure(image=scissorPhoto)
scissorHandButton.grid_forget()
resultButton.configure(image=winPhoto)
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
elif compPick =="Lizard":
paperHandButton.configure(image=lizardPhoto)
scissorHandButton.grid_forget()
resultButton.configure(image=winPhoto)
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
else :
paperHandButton.configure(image=spockPhoto)
scissorHandButton.grid_forget()
resultButton.configure(image=losePhoto)
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif yourChoice == "Paper":
rockHandButton.configure(image=paperPhoto)
if compPick == "Rock":
paperHandButton.configure(image=rockPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif compPick == "Paper":
paperHandButton.configure(image=paperPhoto)
resultButton.configure(image=tiePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Draw.play()
click = False
elif compPick == "Scissor":
paperHandButton.configure(image=scissorPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif compPick =="Lizard":
paperHandButton.configure(image=lizardPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
else :
paperHandButton.configure(image=spockPhoto)
resultButton.configure(image=winPhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
elif yourChoice=="Scissor":
rockHandButton.configure(image=scissorPhoto)
if compPick == "Rock":
paperHandButton.configure(image=rockPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif compPick == "Paper":
paperHandButton.configure(image=paperPhoto)
resultButton.configure(image=winPhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
elif compPick=="Scissor":
paperHandButton.configure(image=scissorPhoto)
resultButton.configure(image=tiePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Draw.play()
click = False
elif compPick == "Lizard":
paperHandButton.configure(image=lizardPhoto)
resultButton.configure(image=winPhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
else:
paperHandButton.configure(image=spockPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif yourChoice=="Lizard":
rockHandButton.configure(image=lizardPhoto)
if compPick == "Rock":
paperHandButton.configure(image=rockPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif compPick == "Paper":
paperHandButton.configure(image=paperPhoto)
resultButton.configure(image=winPhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
elif compPick=="Scissor":
paperHandButton.configure(image=scissorPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif compPick == "Lizard":
paperHandButton.configure(image=lizardPhoto)
resultButton.configure(image=tiePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Draw.play()
click = False
else:
paperHandButton.configure(image=spockPhoto)
resultButton.configure(image=winPhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
elif yourChoice=="Spock":
rockHandButton.configure(image=spockPhoto)
if compPick == "Rock":
paperHandButton.configure(image=rockPhoto)
resultButton.configure(image=winPhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
elif compPick == "Paper":
paperHandButton.configure(image=paperPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
elif compPick=="Scissor":
paperHandButton.configure(image=scissorPhoto)
resultButton.configure(image=winPhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Win.play()
click = False
elif compPick == "Lizard":
paperHandButton.configure(image=lizardPhoto)
resultButton.configure(image=losePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Lose.play()
click = False
else:
paperHandButton.configure(image=spockPhoto)
resultButton.configure(image=tiePhoto)
scissorHandButton.grid_forget()
lizardHandButton.grid_forget()
spockHandButton.grid_forget()
Draw.play()
click = False
else:
#To reset the game :
if yourChoice=="Rock" or yourChoice=="Paper" or yourChoice=="Scissor" or yourChoice=="Lizard" or yourChoice=="Spock":
rockHandButton.configure(image=rockHandPhoto)
paperHandButton.configure(image=paperHandPhoto)
scissorHandButton.configure(image=scissorHandPhoto)
lizardHandButton.configure(image=lizardHandPhoto)
spockHandButton.configure(image=spockHandPhoto)
resultButton.configure(image=decisionPhoto)
#Get back the deleted buttons :
scissorHandButton.grid(row=0,column=2)
lizardHandButton.grid(row=0,column=3)
spockHandButton.grid(row=0,column=4)
#Set click = True :
click=True
#Play the sound file :
start.play()
#Calling the play function :
play()
#Enter the main loop :
root.mainloop()

Wrapping up and resources#

Congrats! You’ve reached the end and built a successful Python Tkinter game. Great work applying your Python knowledge. I hope you enjoyed this article and learned something new from it.

Python is such an intuitive programming language that the easiest way to master it is through hands-on practice like this. Keep up the work and keep building!

In this series on Tkinter, we’ll be developing a total of Five games using Tkinter. So, hang tight! If you have any doubts, questions, or thoughts regarding this article, feel free to contact me at shuklapratik22@gmail.com


Continue learning#

Frequently Asked Questions

How do you write an algorithm for rock, paper, scissors?

A high-level language like Python can be utilized to write an algorithm for any game. First, define the players and set up the game’s initial conditions. Employ if-else statements to establish the rules (e.g., if the player chooses scissors when the computer chooses paper, the player’s score increases). And finally, define the conditions for a victory, defeat, or draw. Check to make sure that the algorithm accurately determines the winner based on the chosen moves. For the game to successfully end, set up termination conditions, such as reaching a certain score.


Written By:
Pratik Shukla
 
Join 2.5 million developers at
Explore the catalog

Free Resources