-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathDiceRollAnimated.py
More file actions
62 lines (51 loc) · 2.04 KB
/
DiceRollAnimated.py
File metadata and controls
62 lines (51 loc) · 2.04 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
import tkinter as tk
import random
from PIL import Image, ImageTk
import time
import os
class DiceApp:
def __init__(self, root):
self.root = root
self.root.title("🎲 Animated Dice Roller")
self.root.config(bg="#111")
# Load dice images (ensure they exist in same folder)
base_path = os.path.dirname(__file__)
self.dice_images = [
ImageTk.PhotoImage(Image.open(os.path.join(base_path, f"dice{i}.png")).resize((120, 120)))
for i in range(1, 7)
]
self.title_label = tk.Label(
root, text="🎲 Animated Dice Roller",
font=("Poppins", 20, "bold"), fg="white", bg="#111"
)
self.title_label.pack(pady=20)
self.dice_label = tk.Label(root, image=self.dice_images[0], bg="#111")
self.dice_label.pack(pady=20)
self.result_label = tk.Label(
root, text="Click below to roll!",
font=("Poppins", 14), fg="#ccc", bg="#111"
)
self.result_label.pack(pady=10)
self.roll_button = tk.Button(
root, text="Roll Dice 🎲",
font=("Poppins", 14, "bold"), bg="#4CAF50", fg="white",
relief="flat", padx=20, pady=10, command=self.animate_roll
)
self.roll_button.pack(pady=20)
def animate_roll(self):
self.roll_button.config(state="disabled") # disable during animation
# Animate by showing random dice faces quickly
for _ in range(15): # number of animation frames
face = random.choice(self.dice_images)
self.dice_label.config(image=face)
self.root.update_idletasks()
time.sleep(0.08) # control animation speed
# Final result
result = random.randint(1, 6)
self.dice_label.config(image=self.dice_images[result - 1])
self.result_label.config(text=f"You rolled a {result}!")
self.roll_button.config(state="normal") # re-enable button
if __name__ == "__main__":
root = tk.Tk()
app = DiceApp(root)
root.mainloop()