Rock paper scissors game, made with Tkinter (with Python)

RickA
4 min readDec 20, 2022

--

One of the most famous games to play if you have nothing to do or when you must decide something, but you can’t, is of course, rock paper scissors. I replicated this game myself with Python.

I used the library Tkinter. Recently, I discovered it and the possibilities are awesome. I would place a link at the end of this article if you want to learn more about Tkinter. So, I wanted to make something with Tkinter. I ended up making rock paper scissors and I want to share it with you. Let’s take a look at the code and the result.

The code isn’t long. It has all the things this game requires. First, we make the screen where we can play the game.

# Import Required Library
from tkinter import *
import random

# Create Object
root = Tk()

# Set geometry
root.geometry("300x300")

# Set title
root.title("Rock Paper Scissor Game")

Next, I assign the values for the computer, because during the game, you compete against the computer.

# Computer Value
computer_value = {
"0": "Rock",
"1": "Paper",
"2": "Scissor"
}

Then, I made some functions. Depending on what the player chooses, the computer or player wins. So, if the player chooses stone and the function also chooses stone, they tie. Of course, this also applies to scissors against scissors. If you don’t know this game, a short explanation in between:

stone > scissors
scissors > paper
paper > stone

This part provides the match result.

# Reset The Game
def reset_game():
b1["state"] = "active"
b2["state"] = "active"
b3["state"] = "active"
l1.config(text="Player ")
l3.config(text="Computer")
l4.config(text="")

# Disable the Button
def button_disable():
b1["state"] = "disable"
b2["state"] = "disable"
b3["state"] = "disable"

# If player selected rock
def isrock():
c_v = computer_value[str(random.randint(0, 2))]
if c_v == "Rock":
match_result = "Match Draw"
elif c_v == "Scissor":
match_result = "Player Win"
else:
match_result = "Computer Win"
l4.config(text=match_result)
l1.config(text="Rock ")
l3.config(text=c_v)
button_disable()

# If player selected paper
def ispaper():
c_v = computer_value[str(random.randint(0, 2))]
if c_v == "Paper":
match_result = "Match Draw"
elif c_v == "Scissor":
match_result = "Computer Win"
else:
match_result = "Player Win"
l4.config(text=match_result)
l1.config(text="Paper ")
l3.config(text=c_v)
button_disable()

# If player selected scissor
def isscissor():
c_v = computer_value[str(random.randint(0, 2))]
if c_v == "Rock":
match_result = "Computer Win"
elif c_v == "Scissor":
match_result = "Match Draw"
else:
match_result = "Player Win"
l4.config(text=match_result)
l1.config(text="Scissor ")
l3.config(text=c_v)
button_disable()

The last part of the code, packing everything together and writing the last details so it will work and finishing the code. This includes, making the buttons to choose between rock, paper or scissors. Also the text needs to be made.

# Add Labels, Frames and Button
Label(root,
text="Rock Paper Scissor",
font="normal 20 bold",
fg="blue").pack(pady=20)

frame = Frame(root)
frame.pack()

l1 = Label(frame,
text="Player ",
font=10)

l2 = Label(frame,
text="VS ",
font="normal 10 bold")

l3 = Label(frame, text="Computer", font=10)

l1.pack(side=LEFT)
l2.pack(side=LEFT)
l3.pack()

l4 = Label(root,
text="",
font="normal 20 bold",
bg="white",
width=15,
borderwidth=2,
relief="solid")
l4.pack(pady=20)

frame1 = Frame(root)
frame1.pack()

b1 = Button(frame1, text="Rock",
font=10, width=7,
command=isrock)

b2 = Button(frame1, text="Paper ",
font=10, width=7,
command=ispaper)

b3 = Button(frame1, text="Scissor",
font=10, width=7,
command=isscissor)

b1.pack(side=LEFT, padx=10)
b2.pack(side=LEFT, padx=10)
b3.pack(padx=10)

Button(root, text="Reset Game",
font=10, fg="red",
bg="black", command=reset_game).pack(pady=20)

# Execute Tkinter
root.mainloop()

Lastly, the most fun part, is playing the game. Therefore, I will show you some screenshots. Before you take a look, please make sure to follow me on Medium, read my stories and clap for them. I would really appreciate it, thanks!

Now, the screenshots:

The starting screen
Me and the computer choose rock, so a draw
Paper is stronger than rock, so the computer wins
I won, because scissors beat paper

Thanks for reading this story and if you haven’t done this already, follow me to get all my stories.

Here is the link for more information about Tkinter:

--

--

RickA

Computer Science: Python - Unity - Coding & Programming - Student - Follow me please :)