Home » Rock Paper Scissors Game in Python

Rock Paper Scissors Game in Python

Rock paper scissors has three possible outcomes: a draw, a win or a loss. A player who decides to play rock will beat another player who has chosen scissors (“rock crushes scissors” or “breaks scissors” or sometimes “blunts scissors”, but will lose to one who has played paper (“paper covers rock”); a play of paper will lose to a play of scissors (“scissors cuts paper”).

If both players choose the same shape, the game is tied and is usually immediately replayed to break the tie.

This example will use tkinter and the random libraries

  1. $ pip install tkinter
  2. $ pip install random

Code

The basic concept can be split into various sections

Create the GUI
Get the user input
Generate a random entry for the computer
Work out who has won
Create buttons and functions

# importing the required libraries and modules  
from tkinter import *  
import random  
  
# creating the GUI  
guiWindow = Tk()  
guiWindow.title("Rock Paper Scissors")  
guiWindow.geometry("460x380")  
guiWindow.config(bg = "#588C7E")  
guiWindow.resizable(width = False, height = False)  
  
# adding a label to the window using the Label() widget  
heading = Label(  
    guiWindow,  
    text = 'Play Rock Paper Scissors',  
    font = 'arial 18 bold',  
    bg = '#588C7E',  
    fg = 'white'  
    ).pack()  
  
# creating column for user selection  
userInput = StringVar()  
  
subHeading = Label(  
    guiWindow,  
    text = 'Select any ONE from rock, paper, scissors',  
    font = 'calibri 14 bold',  
    bg = '#96CEB4'  
    ).place(  
        x = 35,  
        y = 110  
        )  
  
Entry(  
    guiWindow,  
    font = 'calibri 14',  
    textvariable = userInput,  
    bg = '#FBEFCC'  
    ).place(  
        x = 110,  
        y = 160  
        )  
  
# code for computer selection  
compSelection = random.randint(1, 3)  
  
if compSelection == 1:  
    compSelection = 'rock'  
elif compSelection == 2:  
    compSelection = 'paper'  
else:  
    compSelection = 'scissors'  
  
# creating function to begin the game  
res = StringVar()  
  
def letsPlay():  
    userSelection = userInput.get()  
    if userSelection == compSelection:  
        res.set("It's a Tie! You made a same choice as computer.")  
    elif userSelection == 'rock' and compSelection == 'paper':  
        res.set("You Lose. Computer selected Paper.")  
    elif userSelection == 'rock' and compSelection == 'scissors':  
        res.set("You Win. Computer selected Scissors.")  
    elif userSelection == 'paper' and compSelection == 'scissors':  
        res.set("You Lose. Computer selected Scissors.")  
    elif userSelection == 'paper' and compSelection == 'rock':  
        res.set("You Win. Computer selected Rock.")  
    elif userSelection == 'scissors' and compSelection == 'rock':  
        res.set("You Lose. Computer selected Rock.")  
    elif userSelection == 'scissors' and compSelection == 'paper':  
        res.set("You Win. Computer selected Paper.")  
    else:  
        res.set("Invalid input! rock, paper & scissors only")  
  
# defining a function to reset the game  
def resetGame():  
    res.set("")  
    userInput.set("")  
  
# defining a function to exit the game  
def exitGame():  
    guiWindow.destroy()  
  
displayResult = Label(  
    guiWindow,  
    textvariable = res,  
    font = 'calibri 12 bold',  
    bg = '#96CEB4'  
    ).place(  
        x = 20,  
        y = 240  
    )  
  
playButton = Button(  
    guiWindow,  
    font = 'calibri 10 bold',  
    text = 'PLAY',  
    padx = 5,  
    bg = 'white',  
    command = letsPlay  
    ).place(  
        x = 100,  
        y = 300  
        )  
  
resetButton = Button(  
    guiWindow,  
    font = 'calibri 10 bold',  
    text = 'RESET',  
    padx = 5,  
    bg = 'white',  
    command = resetGame  
    ).place(  
        x = 200,  
        y = 300  
        )  
  
exitButton = Button(  
    guiWindow,  
    font = 'calibri 10 bold',  
    text = 'EXIT',  
    padx = 5,  
    bg = 'white',  
    command = exitGame  
    ).place(  
        x = 300,  
        y = 300  
        )  
  
guiWindow.mainloop()

 

You may also like

Leave a Comment

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More