-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5.py
More file actions
188 lines (167 loc) · 5.22 KB
/
Copy path5.py
File metadata and controls
188 lines (167 loc) · 5.22 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
"""
Coin Flip Simulator
Simple & fun GUI using tkinter
"""
import tkinter as tk
import random
import time
class CoinFlip:
def __init__(self, root):
self.root = root
self.root.title("Coin Flip")
self.root.geometry("400x550")
self.root.configure(bg='#1a1a2e')
self.root.resizable(False, False)
self.heads = 0
self.tails = 0
self.flipping = False
# Coin frames for animation
self.coin_frames = ["🪙", "⬜", "🟡", "⬜", "🪙"]
# Title
tk.Label(
root,
text="🪙 Coin Flip",
font=('Arial', 26, 'bold'),
bg='#1a1a2e',
fg='white'
).pack(pady=20)
# Coin display
self.coin_label = tk.Label(
root,
text="🪙",
font=('Arial', 100),
bg='#1a1a2e',
fg='white'
)
self.coin_label.pack(pady=10)
# Result label
self.result_label = tk.Label(
root,
text="Press flip!",
font=('Arial', 22, 'bold'),
bg='#1a1a2e',
fg='#f1c40f'
)
self.result_label.pack(pady=10)
# Score frame
score_frame = tk.Frame(root, bg='#1a1a2e')
score_frame.pack(pady=15)
self.heads_label = tk.Label(
score_frame,
text="Heads: 0",
font=('Arial', 14, 'bold'),
bg='#16213e',
fg='#2ecc71',
width=12,
pady=8,
relief=tk.RAISED
)
self.heads_label.pack(side=tk.LEFT, padx=10)
self.tails_label = tk.Label(
score_frame,
text="Tails: 0",
font=('Arial', 14, 'bold'),
bg='#16213e',
fg='#e74c3c',
width=12,
pady=8,
relief=tk.RAISED
)
self.tails_label.pack(side=tk.LEFT, padx=10)
# Flip button
self.flip_btn = tk.Button(
root,
text="🪙 FLIP!",
font=('Arial', 18, 'bold'),
bg='#f39c12',
fg='white',
width=15,
height=2,
cursor='hand2',
bd=0,
command=self.flip
)
self.flip_btn.pack(pady=15)
# Flip 10 times button
self.multi_btn = tk.Button(
root,
text="Flip 10x",
font=('Arial', 12),
bg='#8e44ad',
fg='white',
width=15,
cursor='hand2',
bd=0,
command=self.flip_ten
)
self.multi_btn.pack(pady=5)
# Reset button
tk.Button(
root,
text="Reset",
font=('Arial', 11),
bg='#555',
fg='white',
width=15,
cursor='hand2',
bd=0,
command=self.reset
).pack(pady=5)
def flip(self):
if self.flipping:
return
self.flipping = True
self.flip_btn.config(state='disabled')
self.multi_btn.config(state='disabled')
self.animate(0, random.choice(['Heads', 'Tails']))
def animate(self, step, final_result):
frames = ['🌑', '🌘', '🌗', '🌖', '🌕', '🌔', '🌓', '🌒']
if step < 12:
self.coin_label.config(text=frames[step % len(frames)])
self.result_label.config(text="Flipping...", fg='#aaa')
self.root.after(80, lambda: self.animate(step + 1, final_result))
else:
# Show result
if final_result == 'Heads':
self.coin_label.config(text='😊')
self.result_label.config(text="HEADS!", fg='#2ecc71')
self.heads += 1
self.heads_label.config(text=f"Heads: {self.heads}")
else:
self.coin_label.config(text='🌟')
self.result_label.config(text="TAILS!", fg='#e74c3c')
self.tails += 1
self.tails_label.config(text=f"Tails: {self.tails}")
self.flipping = False
self.flip_btn.config(state='normal')
self.multi_btn.config(state='normal')
def flip_ten(self):
if self.flipping:
return
results = [random.choice(['Heads', 'Tails']) for _ in range(10)]
h = results.count('Heads')
t = results.count('Tails')
self.heads += h
self.tails += t
self.heads_label.config(text=f"Heads: {self.heads}")
self.tails_label.config(text=f"Tails: {self.tails}")
last = results[-1]
if last == 'Heads':
self.coin_label.config(text='😊')
self.result_label.config(text=f"10x: {h} Heads, {t} Tails", fg='#2ecc71')
else:
self.coin_label.config(text='🌟')
self.result_label.config(text=f"10x: {h} Heads, {t} Tails", fg='#e74c3c')
def reset(self):
self.heads = 0
self.tails = 0
self.heads_label.config(text="Heads: 0")
self.tails_label.config(text="Tails: 0")
self.coin_label.config(text="🪙")
self.result_label.config(text="Press flip!", fg='#f1c40f')
def main():
root = tk.Tk()
CoinFlip(root)
root.mainloop()
if __name__ == "__main__":
main()