Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

rock paper scissors game in python gui

from tkinter import *
import random
import time
window = Tk()
window.geometry('900x700')
window.config(bg='#212120')
window.title('Rock,paper and scissors game')
#main game
def start():
    la = Label(window,text='choose one',font=(50),fg='black')
    la.pack()
    def rock():
        a = ['rock', 'paper', 'scissors']
        b = random.choice(a)
        if b == 'scissors':
            global l
            l = Label(window,text='you win',fg='#46ff03',font=(40))
            l.pack()
        elif b == 'paper':
            l = Label(window,text='you lose',fg='#ff0f03',font=(40))
            l.pack()
        elif b == 'rock':
            l= Label(window,text='tie',fg='#2c03fc',font=(40))
            l.pack()
        but1.destroy()
        but2.destroy()
        but3.destroy()
        global c
        c = Label(window, text='computer: ' + b, fg='black')
        c.pack()
        c.place(x=600, y=50)
        time.sleep(1)
        la.destroy()
        play_again()
    def scissors():
        a = ['rock', 'paper', 'scissors']
        b = random.choice(a)
        if b == 'paper':
            global l
            l = Label(window, text='you win', fg='#46ff03',font=(40))
            l.pack()
        elif b == 'rock':
            l = Label(window, text='you lose', fg='#ff0f03',font=(40))
            l.pack()
        elif b == 'scissors':
            l = Label(window, text='tie', fg='#2c03fc',font=(40))
            l.pack()
        but1.destroy()
        but2.destroy()
        but3.destroy()
        global c
        c = Label(window, text='computer: ' + b, fg='black')
        c.pack()
        c.place(x=600, y=50)
        time.sleep(1)
        la.destroy()
        play_again()
    def paper():
        a = ['rock', 'paper', 'scissors']
        b = random.choice(a)
        if b == 'rock':
            global l
            l = Label(window, text='you win', fg='#46ff03',font=(40))
            l.pack()
        elif b == 'scissors':
            l = Label(window, text='you lose', fg='#ff0f03',font=(40))
            l.pack()
        elif b == 'paper':
            l = Label(window, text='tie', fg='#2c03fc',font=(40))
            l.pack()
        but1.destroy()
        but2.destroy()
        but3.destroy()
        global c
        c = Label(window, text='computer: ' + b, fg='black')
        c.pack()
        c.place(x=600,y=50)
        time.sleep(1)
        la.destroy()
        play_again()
    but1 = Button(window,text="Rock",command=rock,fg='#1c1f1f')
    but2 = Button(window,text="scissors",command=scissors,fg='#1c1f1f')
    but3 = Button(window,text="paper",command=paper,fg='#1c1f1f')
    but1.pack()
    but3.pack()
    but2.pack()
    but1.place(x=100,y=100)
    but2.place(x=800,y=100)
    but3.place(x=450,y=100)
    button.destroy()
def play_again():
    def yes():
        start()
        c.destroy()
        l.destroy()
        label1.destroy()
        button1.destroy()
        button2.destroy()
    def no():
        exit()

    label1 = Label(window,text='play again?',padx=20,pady=20)
    button1 = Button(window,text='yes',command=yes)
    button2= Button(window,text='no',command=no)
    label1.pack()
    button1.pack()
    button2.pack()
    button2.place(x=450,y=100)
    button1.place(x=400,y=100)
button = Button(window,text='start the game',command=start)
button.pack()
window.mainloop()
window.destroy()
 
PREVIOUS NEXT
Tagged: #rock #paper #scissors #game #python #gui
ADD COMMENT
Topic
Name
8+9 =