-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRPS_game_task.py
More file actions
238 lines (204 loc) · 7.46 KB
/
RPS_game_task.py
File metadata and controls
238 lines (204 loc) · 7.46 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import tkinter as tk
from tkinter import messagebox
import random
class RockPaperScissorsGame:
def __init__(self, root):
self.root = root
self.root.title("Rock Paper Scissors")
self.root.geometry("500x500")
self.root.resizable(False, False)
# Game variables
self.user_score = 0
self.computer_score = 0
self.round_count = 1
# Colors
self.bg_color = "#f0f0f0"
self.button_color = "#e1e1e1"
self.text_color = "#333333"
self.highlight_color = "#4CAF50"
# Configure styles
self.root.configure(bg=self.bg_color)
# Create widgets
self.create_widgets()
def create_widgets(self):
# Header
self.header = tk.Label(
self.root,
text="Rock Paper Scissors",
font=("Arial", 24, "bold"),
bg=self.bg_color,
fg=self.text_color
)
self.header.pack(pady=20)
# Instructions
self.instructions = tk.Label(
self.root,
text="Choose your weapon:",
font=("Arial", 14),
bg=self.bg_color,
fg=self.text_color
)
self.instructions.pack()
# Button frame
self.button_frame = tk.Frame(self.root, bg=self.bg_color)
self.button_frame.pack(pady=20)
# Choice buttons
self.rock_btn = tk.Button(
self.button_frame,
text="Rock",
font=("Arial", 14),
width=10,
bg=self.button_color,
command=lambda: self.play_round("rock")
)
self.rock_btn.grid(row=0, column=0, padx=10)
self.paper_btn = tk.Button(
self.button_frame,
text="Paper",
font=("Arial", 14),
width=10,
bg=self.button_color,
command=lambda: self.play_round("paper")
)
self.paper_btn.grid(row=0, column=1, padx=10)
self.scissors_btn = tk.Button(
self.button_frame,
text="Scissors",
font=("Arial", 14),
width=10,
bg=self.button_color,
command=lambda: self.play_round("scissors")
)
self.scissors_btn.grid(row=0, column=2, padx=10)
# Results frame
self.results_frame = tk.Frame(self.root, bg=self.bg_color)
self.results_frame.pack(pady=20)
# Round display
self.round_label = tk.Label(
self.results_frame,
text=f"Round: {self.round_count}",
font=("Arial", 12),
bg=self.bg_color,
fg=self.text_color
)
self.round_label.grid(row=0, column=0, columnspan=2, pady=5)
# Choices display
self.user_choice_label = tk.Label(
self.results_frame,
text="Your choice: -",
font=("Arial", 12),
bg=self.bg_color,
fg=self.text_color
)
self.user_choice_label.grid(row=1, column=0, padx=10)
self.computer_choice_label = tk.Label(
self.results_frame,
text="Computer's choice: -",
font=("Arial", 12),
bg=self.bg_color,
fg=self.text_color
)
self.computer_choice_label.grid(row=1, column=1, padx=10)
# Result display
self.result_label = tk.Label(
self.results_frame,
text="",
font=("Arial", 14, "bold"),
bg=self.bg_color,
fg=self.highlight_color
)
self.result_label.grid(row=2, column=0, columnspan=2, pady=10)
# Score display
self.score_frame = tk.Frame(self.root, bg=self.bg_color)
self.score_frame.pack(pady=20)
self.user_score_label = tk.Label(
self.score_frame,
text=f"You: {self.user_score}",
font=("Arial", 14),
bg=self.bg_color,
fg=self.text_color
)
self.user_score_label.grid(row=0, column=0, padx=20)
self.computer_score_label = tk.Label(
self.score_frame,
text=f"Computer: {self.computer_score}",
font=("Arial", 14),
bg=self.bg_color,
fg=self.text_color
)
self.computer_score_label.grid(row=0, column=1, padx=20)
# Rules button
self.rules_btn = tk.Button(
self.root,
text="Show Rules",
font=("Arial", 10),
bg=self.button_color,
command=self.show_rules
)
self.rules_btn.pack(pady=10)
# Reset button
self.reset_btn = tk.Button(
self.root,
text="Reset Game",
font=("Arial", 10),
bg=self.button_color,
command=self.reset_game
)
self.reset_btn.pack(pady=5)
def play_round(self, user_choice):
# Computer selection
computer_choice = random.choice(['rock', 'paper', 'scissors'])
# Update display
self.user_choice_label.config(text=f"Your choice: {user_choice.capitalize()}")
self.computer_choice_label.config(text=f"Computer's choice: {computer_choice.capitalize()}")
# Determine winner
if user_choice == computer_choice:
result = "It's a tie!"
color = "blue"
elif (user_choice == 'rock' and computer_choice == 'scissors') or \
(user_choice == 'scissors' and computer_choice == 'paper') or \
(user_choice == 'paper' and computer_choice == 'rock'):
result = "You win this round!"
self.user_score += 1
color = "green"
else:
result = "Computer wins this round!"
self.computer_score += 1
color = "red"
# Update result display
self.result_label.config(text=result, fg=color)
# Update scores
self.user_score_label.config(text=f"You: {self.user_score}")
self.computer_score_label.config(text=f"Computer: {self.computer_score}")
# Increment round
self.round_count += 1
self.round_label.config(text=f"Round: {self.round_count}")
# Check for game end (optional)
if self.user_score == 5 or self.computer_score == 5:
winner = "You" if self.user_score == 5 else "Computer"
messagebox.showinfo("Game Over", f"{winner} won the game!")
self.reset_game()
def show_rules(self):
rules = """Rock Paper Scissors Rules:
- Rock beats Scissors
- Scissors beat Paper
- Paper beats Rock
- Same choices result in a tie
First to 5 points wins the game!"""
messagebox.showinfo("Game Rules", rules)
def reset_game(self):
self.user_score = 0
self.computer_score = 0
self.round_count = 1
# Reset displays
self.user_score_label.config(text=f"You: {self.user_score}")
self.computer_score_label.config(text=f"Computer: {self.computer_score}")
self.round_label.config(text=f"Round: {self.round_count}")
self.user_choice_label.config(text="Your choice: -")
self.computer_choice_label.config(text="Computer's choice: -")
self.result_label.config(text="")
# Create and run the application
if __name__ == "__main__":
root = tk.Tk()
app = RockPaperScissorsGame(root)
root.mainloop()