Rock Paper Scissors in Python using GUI Tkinter

Rock Paper Scissors in Python using GUI Tkinter

Hey folks, This project will help you play and create Rock Paper Scissors GUI game in Python, a very renowned game we have all swayed our hands to since our childhood. So for this project, we are going to use some in-built libraries of python namely: Tkinter and Random.

Using:

from tkinter import *
from tkinter import messagebox
import random as r

Rock Paper Scissors GUI using Python

We’ll kick off with learning the functions used in this project:

The P1_roll function:

def p1_roll():
    global p1_choice
    p1_choice=r.choice(['rock','scissors','paper'])
    p1.configure(image=moves[p1_choice])
    b1['text']=p1_choice.title()
    b1['state']=DISABLED
    check()

The above function randomly chooses a move for Player 1 while also changing the state and text of the button associated and changes the image to match the button text.

The P2_roll function:

def p2_roll():
    global p2_choice
    p2_choice=r.choice(['rock','scissors','paper'])
    p2.configure(image=moves[p2_choice])
    b2['text']=p2_choice.title()
    b2['state']=DISABLED
    check()

This function is the same as the above but it does all the above-mentioned configurations for Player 2.

You might have noticed the check() function being called in both the p1_roll and p2_roll, it goes like this:

The Check() function:

def check():
    if(b1['state']==b2['state']==DISABLED):
        compare()

The check() function checks if the states of both the buttons are the same and is set to DISABLED and finally calls the compare() function.

The Compare() function:

def compare():
    if((b1['text'][0]=='R' and b2['text']=='S') or (b1['text'][0]=='S' and b2['text']=='P') or (b1['text'][0]=='P' and b2['text']=='R')):
        messagebox.showinfo('Victory','Player 1 has Won.\n\tHurrah!!')
    elif(b1['text']==b2['text']):
        messagebox.showinfo('Draw',"Well that's a tie.\n\tBetter luck next time!!")
    else:
        messagebox.showinfo('Victory','Player 2 has Won.\n\tHurrah!!')
    reset()

This function does the basic comparing of the moves of the players based on the Rock-Paper-Scissor rules and displays prompt for each result and resets the application by calling the reset() function.

The Reset() function:

def reset():
    global p1_choice,p2_choice
    b1['text']='Player 1 Roll'
    b1['state']=NORMAL
    p1.configure(image=general['player_1'])
    p1_choice=None
    b2['text']='Player 2 Roll'
    b2['state']=NORMAL
    p2.configure(image=general['player_2'])
    p2_choice=None

The reset() function returns all the configuration of the application to its initial state.

In the main program we follow the following steps:

  1. We define the tk window using Tk() as the root variable.
  2. Then we create the title of the window.
  3. Next, we define a variable each for storing the initial images (general)and moves images(moves).
  4. Then we create two frames:
    • The first frame contains image displays.
    • The second frame contains the buttons required.
  5. Also, we set the various arguments of the button and the frames.
  6. Note: We insert a frame in the second frame between the two buttons to create some space between them.
  7. Lastly, we pack all the widgets together in their respective positions and define the variables that store choice for each player.
  8. We end by a root.mainloop() function to keep the events on the main window active and all widgets interactive.

Also, we use a messagebox class of Tkinter to display prompts during the game for the results

The Complete program looks like so:

from tkinter import *
from tkinter import messagebox
import random as r
def reset():
    global p1_choice,p2_choice
    b1['text']='Player 1 Roll'
    b1['state']=NORMAL
    p1.configure(image=general['player_1'])
    p1_choice=None
    b2['text']='Player 2 Roll'
    b2['state']=NORMAL
    p2.configure(image=general['player_2'])
    p2_choice=None
def compare():
    if((b1['text'][0]=='R' and b2['text']=='S') or (b1['text'][0]=='S' and b2['text']=='P') or (b1['text'][0]=='P' and b2['text']=='R')):
        messagebox.showinfo('Victory','Player 1 has Won.\n\tHurrah!!')
    elif(b1['text']==b2['text']):
        messagebox.showinfo('Draw',"Well that's a tie.\n\tBetter luck next time!!")
    else:
        messagebox.showinfo('Victory','Player 2 has Won.\n\tHurrah!!')
    reset()
def check():
    if(b1['state']==b2['state']==DISABLED):
        compare()
def p1_roll():
    global p1_choice
    p1_choice=r.choice(['rock','scissors','paper'])
    p1.configure(image=moves[p1_choice])
    b1['text']=p1_choice.title()
    b1['state']=DISABLED
    check()
def p2_roll():
    global p2_choice
    p2_choice=r.choice(['rock','scissors','paper'])
    p2.configure(image=moves[p2_choice])
    b2['text']=p2_choice.title()
    b2['state']=DISABLED
    check()
#############################Main Program############################
root=Tk()
root.title("Rock-Paper-Scissors")
font=(('Times New Roman','bold'),'20')
general={'player_1':PhotoImage(file='Assets/player1.png'),'player_2':PhotoImage(file='Assets/player2.png'),'vs':PhotoImage(file='Assets/vs.png')}
moves={'rock':PhotoImage(file='Assets/rock.png'),'paper':PhotoImage(file='Assets/paper.png'),'scissors':PhotoImage(file='Assets/scissor.png')}
f1=Frame(root)
p1=Label(f1,image=general['player_1'])
p1.pack(side='left')
vs=Label(f1,image=general['vs'])
vs.pack(side='left')
p2=Label(f1,image=general['player_2'])
p2.pack(side='left')
f1.pack()
f2=Frame(root)
b1=Button(f2,text='Player 1 Roll',width=20,font=font,command=p1_roll)
b1.pack(side='left')
f_space=Frame(f2,width=150)
f_space.pack(side='left')
b2=Button(f2,text='Player 2 Roll',width=20,font=font,command=p2_roll)
b2.pack(side='left')
f2.pack(pady=10)
p1_choice=None
p2_choice=None
root.mainloop()

To learn more about Random:

random() module in Python

How to create rock paper scissors two players game in Python

Leave a Reply

Your email address will not be published. Required fields are marked *