Rock Paper Scissor Game Using Python Tkinter

Rock Paper Scissor Game Using Python Tkinter

Most of us have played this game of Rock Paper Scissor and today we will make this Rock Paper Scissor Game Using Python Tkinter.

We will be using the random module which will be used here for the purpose of generating a random choice so that we can present it as a guess by the computer.

Tkinter is one of GUI libraries of Python and random module is used to generate random choices like here, it will be used to generate a random choice from stone, paper, and scissors.

Source Code


import random
import tkinter

stats = []


def get_winner(call):
    if random.random() <= (1 / 3):
        throw = "rock"
    elif (1 / 3) < random.random() <= (2 / 3):
        throw = "scissors"
    else:
        throw = "paper"

    if (throw == "rock" and call == "paper") or (throw == "paper" and call == "scissors") \
            or (throw == "scissors" and call == "rock"):
        stats.append('W')
        result = "You won!"
    elif throw == call:
        stats.append('D')
        result = "It's a draw"
    else:
        stats.append('L')
        result = "You lost!"

    global output
    output.config(text="Computer did: " + throw + "\n" + result)


def pass_s():
    get_winner("scissors")


def pass_r():
    get_winner("rock")


def pass_p():
    get_winner("paper")


window = tkinter.Tk()

scissors = tkinter.Button(window, text="Scissors", bg="#ff9999", padx=10, pady=5, command=pass_s, width=20)
rock = tkinter.Button(window, text="Rock", bg="#80ff80", padx=10, pady=5, command=pass_r, width=20)
paper = tkinter.Button(window, text="Paper", bg="#3399ff", padx=10, pady=5, command=pass_p, width=20)
output = tkinter.Label(window, width=20, fg="red", text="What's your call?")

scissors.pack(side="left")
rock.pack(side="left")
paper.pack(side="left")
output.pack(side="right")
window.mainloop()

for i in stats: print(i, end=" ")
if stats.count('L') > stats.count('W'):
    result = "\nYou loose the series."
elif stats.count('L') == stats.count('W'):
    result = "\nSeries ended in a draw."
else:
    result = "\nYou win the series."

print(result)

Output

Rock Paper Scissor Game Using Python Tkinter
Rock Paper Scissor Game Using Python Tkinter
Rock Paper Scissor Game Using Python Tkinter selected paper
Rock Paper Scissor Game Using Python Tkinter selected paper
selected rock
selected rock

Thanks for reading Rock Paper Scissor Game Using Python Tkinter

Keep Updated for more content like this


Also read:

Make money with Python in 2022

Did you know that data science was the fifth most common job on LinkedIn in 2017? Did you know that the demand for Python programmers is expected to grow by 19% from 2020-2021? Did you know that Python is one of the fastest-growing programming languages, and it will likely continue to expand as its adoption…

Continue Reading Make money with Python in 2022

Top 15 Python Libraries For Data Science in 2022

Introduction In this informative article, we look at the most important Python Libraries For Data Science and explain how their distinct features may help you develop your data science knowledge. Python has a rich data science library environment. It’s almost impossible to cover everything in a single article. As a consequence, we’ve compiled a list…

Continue Reading Top 15 Python Libraries For Data Science in 2022

Top 15 Python Libraries For Machine Learning in 2022

Introduction  In today’s digital environment, artificial intelligence (AI) and machine learning (ML) are getting more and more popular. Because of their growing popularity, machine learning technologies and algorithms should be mastered by IT workers. Specifically, Python machine learning libraries are what we are investigating today. We give individuals a head start on the new year…

Continue Reading Top 15 Python Libraries For Machine Learning in 2022



Share:
Avatar of Harry

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you

5 thoughts on “Rock Paper Scissor Game Using Python Tkinter

  1. Great Share! Oh my goodness! an amazing article dude. Thank you However I am experiencing issue with ur rss . Don’t know why Unable to subscribe to it. Is there anyone getting identical rss problem? Anyone who knows kindly respond. Thnkx

  2. Hi, I found your article by mistake when i was searching bing for this issue, I have to say your article is really helpful I also love the theme, its amazing!. I dont have that much time to read all your post at the moment but I have bookmarked it and also add your RSS feeds. I will be back in a day or two. thank you for a huge site.

Comments are closed.