-
Notifications
You must be signed in to change notification settings - Fork 5
/
rock.py
75 lines (60 loc) · 2.43 KB
/
rock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# rock paper and scissor game
# import the modules we need, for creating a GUI...
from tkinter import *
from tkinter import PhotoImage
import random
def computerRandom():
options = ["Rock", "Paper", "Scissors"]
randomChoice = random.randint(0, 2)
computer_choice.set(options[randomChoice]) # added into the program
return options[randomChoice]
def comparison(humanChoice, computerChoice):
if humanChoice == computerChoice:
return ("Draw")
if humanChoice == "Rock" and computerChoice == "Paper":
return ("Computer Wins")
if humanChoice == "Paper" and computerChoice == "Scissors":
return ("Computer Wins")
if humanChoice == "Scissors" and computerChoice == "Rock":
return ("Computer Wins")
else:
return ("Human Wins")
def play():
humanChoice = player_choice.get() # Modified this line
computerChoice = computerRandom()
result = comparison(humanChoice, computerChoice)
if result == "Draw":
result_set.set("Its a draw")
elif result == "Computer Wins":
result_set.set("Unlucky you lost!")
else:
result_set.set("Well done you won!")
# Setup of main GUI
root = Tk()
root.title('Rock Paper Scissors')
root.geometry("300x170+100+200")
image = PhotoImage(file=r"rock.png")
Label(root, image=image).pack(fill="both")
# Variables
player_choice = StringVar()
computer_choice = StringVar()
result_set = StringVar()
# player_choice.set("Rock") ## Required to set player as "Rock" by default
# Layout of GUI
# Player
Label(root, text='Player',font=("",15)).place(x=10,y=5)
Radiobutton(root, text='Rock', variable=player_choice, value='Rock',font=("",12)).place(x=10,y=40)
Radiobutton(root, text='Paper', variable=player_choice, value='Paper',font=("",12)).place(x=10,y=80)
Radiobutton(root, text='Scissors', variable=player_choice,
value='Scissors',font=("",12)).place(x=10,y=120)
# Computer
Label(root, text='Computer',font=("",15)).place(x=160,y=5)
Radiobutton(root, text='Rock', variable=computer_choice, value='Rock',font=("",12)).place(x=170,y=40)
Radiobutton(root, text='Paper', variable=computer_choice, value='Paper',font=("",12)).place(x=170,y=80)
Radiobutton(root, text='Scissors', variable=computer_choice,
value='Scissors',font=("",12)).place(x=170,y=120)
# Play
Button(root, text="Play", command=play,font=("",10)).place(x=100,y=60)
# Result
Label(root, textvariable=result_set,font=("",10)).place(x=30,y=150)
root.mainloop()