Saturday, December 26, 2020

Rock Paper Scissors in Tkinter

 







RPS.png is 446 x 446
Other 3 images are 58 x 46


from tkinter import *

from random import randint

import time


win = Tk()

win.title("ROCK - PAPER - SCISSORS")

win.geometry("800x560+120+0")

win.configure(bg='black')


def Rock():

  pic = PhotoImage(file="Rock.gif")

  CChoice = randint(1, 3)

  if CChoice == 1:

    lblResult.config(text="Both chose Rock - Draw", bg="black", fg="white")

  elif CChoice == 2:

    lblResult.config(text="Computer's Paper covers Your Rock - Computer Wins", bg="red", fg="white")

    I1.set(I1.get() + 1)

  else:

    lblResult.config(text="Your Rock smashes Computer's Scissors - You Win", bg="green", fg="white")

    I2.set(I2.get() + 1)

      

def Paper():

  pic = PhotoImage(file="Paper.gif")

  CChoice = randint(1, 3)

  if CChoice == 1:

    lblResult.config(text="Your Paper covers Computer's Rock - You Win", bg="green", fg="white")

    I2.set(I2.get() + 1)

  elif CChoice == 2:

    lblResult.config(text="Both chose Paper - Draw", bg="black", fg="white")

  else:

    lblResult.config(text="Computer's Scissors cuts Your Paper - Computer Wins", bg="red", fg="white")

    I1.set(I1.get() + 1)

    

def Scissors():

  pic = PhotoImage(file="Scissors.gif")

  CChoice = randint(1, 3)

  if CChoice == 1:

    lblResult.config(text="Computer's Rock smashes Your Scissors - Computer Wins", bg="red", fg="white")

    I1.set(I1.get() + 1)

  elif CChoice == 2:

    lblResult.config(text="Your Scissors cuts Computer's Paper - You Win", bg="green", fg="white")

    I2.set(I2.get() + 1)

  else:

    lblResult.config(text="Both chose Scissors - Draw", bg="black", fg="white")


pic1 = PhotoImage(file="RPS.gif")

lblMain = Label(win, image=pic1, bg="black");  lblMain.place(x=185, y=0)


I1, I2 = IntVar(), IntVar()

lblCompScore1 = Label(win, text="Computer score", bg="black", fg="white", font=("Helvetica", 16, "bold"))

lblCompScore1.place(x=20, y=30)

lblCompScore2 = Label(win, textvariable=I1, bg="black", fg="white", font=("Helvetica", 16, "bold"))

lblCompScore2.place(x=100, y=70)

lblPlayerScore1 = Label(win, text="Player score", bg="black", fg="white", font=("Helvetica", 16, "bold"))

lblPlayerScore1.place(x=630, y=30)

lblPlayerScore2 = Label(win, textvariable=I2, bg="black", fg="white", font=("Helvetica", 16, "bold"))

lblPlayerScore2.place(x=690, y=70)


lblResult = Label(win, bg="white", width=44, font=("Helvetica", 16, "bold"), text="Hello")

lblResult.place(x=120, y=460)


pic2     = PhotoImage(file="Rock.gif")

rock     = Button(win, image=pic2, bg="black", command=Rock)

rock.place(x=300, y=500)

pic3     = PhotoImage(file="Paper.gif")

paper    = Button(win, image=pic3, bg="black", command=Paper)

paper.place(x=380, y=500)

pic4     = PhotoImage(file="Scissors.gif")

scissors = Button(win, image=pic4, bg="black", command=Scissors)

scissors.place(x=450, y=500)


win.mainloop()


No comments:

Post a Comment