1
+ from tkinter import *
2
+ import random
3
+
4
+ root = Tk ()
5
+ root .geometry ("350x300" )
6
+ root .title ("Rock Paper Scissors" )
7
+
8
+ computer_value = {"0" :"Rock" ,"1" :"Paper" ,"2" :"Scissor" }
9
+
10
+ def reset_game ():
11
+ b1 ["state" ]= "active"
12
+ b2 ["state" ]= "active"
13
+ b3 ["state" ]= "active"
14
+ l1 .config (text = "Player" )
15
+ l3 .config (text = "Computer" )
16
+ l4 .config (text = "" )
17
+
18
+ def button_disable ():
19
+ b1 ["state" ]= "disable"
20
+ b2 ["state" ]= "disable"
21
+ b3 ["state" ]= "disable"
22
+
23
+ def player_rock ():
24
+ c_v = computer_value [str (random .randint (0 , 2 ))]
25
+ if c_v == "Paper" :
26
+ match_result = "COMPUTER WINS!"
27
+ elif c_v == "Scissor" :
28
+ match_result = "PLAYER WINS!"
29
+ elif c_v == "Rock" :
30
+ match_result = "DRAW"
31
+ l1 .config (text = "Rock" )
32
+ l3 .config (text = c_v )
33
+ l4 .config (text = match_result )
34
+ button_disable ()
35
+
36
+ def player_paper ():
37
+ c_v = computer_value [str (random .randint (0 , 2 ))]
38
+ if c_v == "Rock" :
39
+ match_result = "PLAYER WINS!"
40
+ elif c_v == "Scissor" :
41
+ match_result = "COMPUTER WINS!"
42
+ elif c_v == "Paper" :
43
+ match_result = "DRAW"
44
+ l1 .config (text = "Paper" )
45
+ l3 .config (text = c_v )
46
+ l4 .config (text = match_result )
47
+ button_disable ()
48
+
49
+ def player_scissor ():
50
+ c_v = computer_value [str (random .randint (0 , 2 ))]
51
+ if c_v == "Paper" :
52
+ match_result = "PLAYER WINS!"
53
+ elif c_v == "Rock" :
54
+ match_result = "COMPUTER WINS!"
55
+ elif c_v == "Scissor" :
56
+ match_result = "DRAW"
57
+ l1 .config (text = "Scissor" )
58
+ l3 .config (text = c_v )
59
+ l4 .config (text = match_result )
60
+ button_disable ()
61
+
62
+ Label (root ,text = "Rock Paper Scissor" ,font = "normal 20 bold" ,fg = "blue" ).pack (pady = 20 )
63
+
64
+ frame = Frame (root )
65
+ frame .pack ()
66
+
67
+ l1 = Label (frame ,text = "Player" ,font = 10 )
68
+ l1 .pack (side = LEFT )
69
+
70
+ l2 = Label (frame ,text = "VS" ,font = 10 )
71
+ l2 .pack (side = LEFT )
72
+
73
+ l3 = Label (frame , text = "Computer" , font = 10 )
74
+ l3 .pack ()
75
+
76
+ l4 = Label (root ,text = "" ,font = "normal 20 bold" ,bg = "white" ,width = 15 ,borderwidth = 2 ,relief = "solid" )
77
+ l4 .pack (pady = 20 )
78
+
79
+ frame1 = Frame (root )
80
+ frame1 .pack ()
81
+
82
+ b1 = Button (frame1 ,text = "Rock" ,font = 10 ,width = 7 ,command = player_rock )
83
+ b1 .pack (side = LEFT ,padx = 10 )
84
+
85
+ b2 = Button (frame1 ,text = "Paper" ,font = 10 ,width = 7 ,command = player_paper )
86
+ b2 .pack (side = LEFT ,padx = 10 )
87
+
88
+ b3 = Button (frame1 ,text = "Scissor" ,font = 10 ,width = 7 ,command = player_scissor )
89
+ b3 .pack (padx = 10 )
90
+
91
+ Button (root ,text = "Reset Game" ,font = 10 ,fg = "red" ,bg = "black" ,command = reset_game ).pack (pady = 20 )
92
+
93
+ root .mainloop ()
0 commit comments