-
Notifications
You must be signed in to change notification settings - Fork 419
/
Copy pathGUI_Rock_Paper_Scissors.py
170 lines (133 loc) · 3.48 KB
/
GUI_Rock_Paper_Scissors.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# Import Required Library
from tkinter import *
import random
# Create Object
root = Tk()
# Set geometry
root.geometry("400x400")
# Set title
root.title("Rock Paper Scissor Game")
# Computer Value
computer_value = {
"0": "Rock",
"1": "Paper",
"2": "Scissor"
}
user_score = 0
comp_score = 0
# Reset The Game
def reset_game():
b1["state"] = "active"
b2["state"] = "active"
b3["state"] = "active"
l1.config(text="Player")
l3.config(text="Computer")
l4.config(text="")
# Disable the Button
def score():
global user_score
global comp_score
s = "Player:" + str(user_score) + " " + "Computer:" + str(comp_score)
l4.config(text=s)
def button_disable():
b1["state"] = "disable"
b2["state"] = "disable"
b3["state"] = "disable"
# If player selected rock
def isrock():
c_v = computer_value[str(random.randint(0, 2))]
global user_score
global comp_score
if c_v == "Rock":
match_result = "Match Draw"
elif c_v == "Scissor":
match_result = "Player Win"
user_score = user_score + 1
else:
match_result = "Computer Win"
comp_score = comp_score + 1
l4.config(text=match_result)
l1.config(text="Rock ")
l3.config(text=c_v)
button_disable()
# If player selected paper
def ispaper():
c_v = computer_value[str(random.randint(0, 2))]
global user_score
global comp_score
if c_v == "Paper":
match_result = "Match Draw"
elif c_v == "Scissor":
match_result = "Computer Win"
comp_score = comp_score + 1
else:
match_result = "Player Win"
user_score = user_score + 1
l4.config(text=match_result)
l1.config(text="Paper ")
l3.config(text=c_v)
button_disable()
# If player selected scissor
def isscissor():
c_v = computer_value[str(random.randint(0, 2))]
global user_score
global comp_score
if c_v == "Rock":
match_result = "Computer Win"
comp_score = comp_score + 1
elif c_v == "Scissor":
match_result = "Match Draw"
else:
match_result = "Player Win"
user_score = user_score + 1
l4.config(text=match_result)
l1.config(text="Scissor ")
l3.config(text=c_v)
button_disable()
# Add Labels, Frames and Button
Label(root,
text="Rock Paper Scissor",
font="normal 20 bold",
fg="blue").pack(pady=20)
frame = Frame(root)
frame.pack()
l1 = Label(frame,
text="Player ",
font=10)
l2 = Label(frame,
text="VS ",
font="normal 10 bold")
l3 = Label(frame, text="Computer", font=10)
l1.pack(side=LEFT)
l2.pack(side=LEFT)
l3.pack()
l4 = Label(root,
text="",
font="normal 20 bold",
bg="white",
width=20,
borderwidth=2,
relief="solid")
l4.pack(pady=20)
frame1 = Frame(root)
frame1.pack()
b1 = Button(frame1, text="Rock",
font=10, width=7,
command=isrock)
b2 = Button(frame1, text="Paper ",
font=10, width=7,
command=ispaper)
b3 = Button(frame1, text="Scissor",
font=10, width=7,
command=isscissor)
b1.pack(side=LEFT, padx=10)
b2.pack(side=LEFT, padx=10)
b3.pack(padx=10)
Button(root, text="Play Another Round",
font=10, fg="red",
bg="black", command=reset_game).pack(pady=10)
Button(root, text="Score",
font=10, fg="red",
bg="black", command=score).pack(pady=10)
# Execute Tkinter
root.mainloop()