Rock Paper Scissors Game in Python

SpyroAI Avatar
\"Rock

The popular game of Rock, Paper, Scissors was

already repeatedly played by people of all ages. The game is simple to pick up but entertaining, and it only requires two players. If you are a Python developer,

you might want to make a Python version of the game Rock, Paper, Scissors that you can play with your friends or use to hone your coding abilities. This article will provide you a step-by-step tutorial on how to make a Python Rock Paper Scissors game that is entertaining to play and can help you become a due to the technological.

Step 1: Installing Python

Make sure Python is downloaded to your machine before you begin developing your Rock, Paper, Scissors game in Python. Python is a popular programming language that is open-source and free to use and is used to create projects for artificial intelligence, computer science, and web applications. From the official website, you may download a most recent version of Python and install it on your computer by completing the installation instructions.

Step 2: Setting up the Game Environment

The following procedure involves generating a new Python file and configure the game environment after Python has been

downloaded and installed on your machine. To write your Python code, you can use

any text editor or integrated development environment (IDE). We advise using an IDE like PyCharm or Visual Studio Code

because they offer a number of features that are beneficial for Python developers.

You must import the random package into your new Python file

in order to produce random values for the computer\’s decision. The three game alternatives, rock, paper, and scissors, additionally have to be

defined as variables. The last step is to build a loop that will run continuously

until the player chooses to end the game.

Step 3: Creating the Game Logic

Defining the game logic is the following stage in constructing your Rock Paper Scissors game in Python. You must build a function that will accept player input and produce a random value for the computer to select from. The round winner must then be established by comparing the two values.

You can include a scoring system that keeps track of the player\’s and computer\’s scores

to make the experience more engaging. A feature that lets the player select how many rounds they wish to play may additionally add.

Complete Code for the Rock Paper Scissors Game in Python

from tkinter import *
from PIL import Image,ImageTk
import random

userscore=0
pcscore=0

def enter(event):
rock.config(bg=\’black\’,fg=\’white\’)
def enter1(event):
paper.config(bg=\’black\’,fg=\’white\’)
def enter2(event):
scissor.config(bg=\’black\’,fg=\’white\’)
def leave(event):
rock.config(bg=\’white\’,fg=\’black\’)
def leave1(event):
paper.config(bg=\’white\’,fg=\’black\’)
def leave2(event):
scissor.config(bg=\’white\’,fg=\’black\’)
def entergame(event):
maingame()

def maingame():
global userscore,pcscore
global nameinp
global rock,paper,scissor

root.geometry(\'650x750\')

n
name.destroy()
f1.destroy()
inpname.destroy()
sub.destroy()

L2=Label(text=f\"{nameinp.get()} Score: {userscore}\",bg=\'#4834DF\',fg=\'#ffffff\',borderwidth=5,relief=RAISED,font=\'Rockwell 13 bold\',padx=4,pady=2)
L2.grid(row=6,column=0,pady=15)
L3=Label(text=f\"PC Score: {pcscore}\",bg=\'#4834DF\',fg=\'white\',borderwidth=5,relief=RAISED,font=\'Rockwell 13 bold\',padx=4,pady=2)
L3.grid(row=7,column=0,pady=15)


def click(event):

    global userscore,pcscore

    global L1     

    global pcchose  

    L1.grid_forget() 

    pcchose.destroy()   


    val=event.widget.cget(\'text\')


    x=random.randint(0,2)
    l1=[\'Rock\',\'Paper\',\'Scissor\']

    pc_opt=l1[x] 

    pcchose=Label(text=f\'PC Opted: {pc_opt}\',font=\'lucida 15 bold\',bg=\'black\',fg=\'red\')

    pcchose.grid(row=6,column=1,pady=15)


    if val==\'Rock\' and pc_opt==\'Paper\':
        L1=Label(text=\'PC Won\',font=\'lucida 15 bold\',bg=\'black\',fg=\'gold\')
        L1.grid(row=7,column=1,pady=15)
        pcscore+=1


    elif val==\'Rock\' and pc_opt==\'Scissor\':
        L1=Label(text=f\'{nameinp.get()} Won\',font=\'lucida 15 bold\',bg=\'black\',fg=\'gold\')
        L1.grid(row=6,column=1,pady=15)
        userscore+=1


    elif val==\'Paper\' and pc_opt==\'Scissor\':
        L1=Label(text=\'PC Won\',font=\'lucida 15 bold\',bg=\'black\',fg=\'gold\')
        L1.grid(row=7,column=1,pady=15)
        pcscore+=1


    elif val==\'Paper\' and pc_opt==\'Rock\':
        L1=Label(text=f\'{nameinp.get()} Won\',font=\'lucida 15 bold\',bg=\'black\',fg=\'gold\')
        L1.grid(row=7,column=1,pady=15)
        userscore+=1


    elif val==\'Scissor\' and pc_opt==\'Rock\':
        L1=Label(text=\'PC Won\',font=\'lucida 15 bold\',bg=\'black\',fg=\'gold\')
        L1.grid(row=7,column=1,pady=15)
        pcscore+=1


    elif val==\'Scissor\' and pc_opt==\'Paper\':
        L1=Label(text=f\'{nameinp.get()} Won\',font=\'lucida 15 bold\',bg=\'black\',fg=\'gold\')
        L1.grid(row=7,column=1,pady=15)
        userscore+=1


    elif val==pc_opt:
        L1=Label(text=f\"It\'s A Tie\",font=\'lucida 15 bold\',bg=\'black\',fg=\'gold\')
        L1.grid(row=7,column=1,pady=15)
    maingame()




head=Label(text=\'Rock Paper Scissor\',font=\'arial 35 bold\',bg=\'black\',fg=\'white\')
head.grid(columnspan=2,row=0,ipadx=70,padx=33,pady=10)
head1=Label(text=\'CopyAssignment\',font=\'arial 35 bold\',bg=\'red\',fg=\'white\')
head1.grid(columnspan=2,row=1,ipadx=70,padx=33,pady=10)
playerone=Label(text=f\'Player 1 : {nameinp.get()}\',font=\'lucida 16\')
playerone.grid(row=2,column=0)
playertwo=Label(text=f\'Player 2 : Computer\',font=\'lucida 16\')
playertwo.grid(row=2,column=1)


rock=Button(text=\'Rock\',font=\'comicsansms 14 bold\',height=1,width=7)
rock.grid(row=3,column=0,pady=15)

rock.bind(\'<Enter>\',enter)
rock.bind(\'<Leave>\',leave)
rock.bind(\'<Button-1>\',click)
paper=Button(text=\'Paper\',font=\'comicsansms 14 bold\',height=1,width=7)
paper.grid(row=4,column=0)
paper.bind(\'<Enter>\',enter1)
paper.bind(\'<Leave>\',leave1)
paper.bind(\'<Button-1>\',click)
scissor=Button(text=\'Scissor\',font=\'comicsansms 14 bold\',height=1,width=7)
scissor.grid(row=5,column=0,pady=15)
scissor.bind(\'<Enter>\',enter2)
scissor.bind(\'<Leave>\',leave2)
scissor.bind(\'<Button-1>\',click)


rock1=Button(text=\'Rock\',font=\'comicsansms 14 bold\',height=1,width=7)
rock1.grid(row=3,column=1,pady=15)
paper1=Button(text=\'Paper\',font=\'comicsansms 14 bold\',height=1,width=7)
paper1.grid(row=4,column=1)
scissor1=Button(text=\'Scissor\',font=\'comicsansms 14 bold\',height=1,width=7)
scissor1.grid(row=5,column=1,pady=15)


btnclose=Button(text=\'Close Game\',command=root.destroy,bg=\'green\',font=\'arial 10 bold\')
btnclose.place(x=300,y=410)

root=Tk()
root.title(\’Rock Paper Scissor – CopyAssignment\’)
root.wm_iconbitmap(\”play.png\”)

root.geometry(\’650×750\’)

root.maxsize

root.minsize(650,450)

rock=Button()
paper=Button()
scissor=Button()

L1=Label()

pcchose=Label()

f1=Frame(root)
img=Image.open(\’symbols.png\’)
img=img.resize((650,450),Image.ANTIALIAS)
pic=ImageTk.PhotoImage(img)
Lab=Label(f1,image=pic)
Lab.pack()
f1.pack()

name=Label(root,text=\’Enter Your Name :\’,font=\’arial 15 bold\’)
name.place(x=262,y=250)

nameinp=StringVar()
inpname=Entry(root,textvar=nameinp,font=\’arial 10 bold\’)

inpname.bind(\’\’,entergame)
inpname.place(x=275,y=290)

once the username is added
sub=Button(root,text=\”Let\’s Play\”,font=\’lucida 10 bold\’,bg=\’black\’,fg=\’white\’,command=maingame)
sub.place(x=305,y=350)

root.mainloop()