|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import ttk |
| 3 | +import threading |
| 4 | +import random |
| 5 | +import winsound # Windows; replace with playsound for Mac/Linux |
| 6 | + |
| 7 | +# ==================== |
| 8 | +# Quiz Data |
| 9 | +# ==================== |
| 10 | +quiz_categories = { |
| 11 | + "General Knowledge": [ |
| 12 | + {"question": "What is the capital of France?", "options": ["Paris", "London", "Berlin", "Madrid"], "answer": "Paris"}, |
| 13 | + {"question": "Which planet is known as the Red Planet?", "options": ["Earth", "Mars", "Jupiter", "Venus"], "answer": "Mars"} |
| 14 | + ], |
| 15 | + "Literature": [ |
| 16 | + {"question": "Who wrote 'Romeo and Juliet'?", "options": ["Charles Dickens", "William Shakespeare", "Mark Twain", "Jane Austen"], "answer": "William Shakespeare"}, |
| 17 | + {"question": "Which novel begins with 'Call me Ishmael'?", "options": ["Moby Dick", "1984", "Great Expectations", "The Odyssey"], "answer": "Moby Dick"} |
| 18 | + ] |
| 19 | +} |
| 20 | + |
| 21 | +# ==================== |
| 22 | +# Main Game Class |
| 23 | +# ==================== |
| 24 | +class MiniGameShow: |
| 25 | + def __init__(self, root): |
| 26 | + self.root = root |
| 27 | + self.root.title("🎉 Mini Game Show 🎉") |
| 28 | + self.root.geometry("800x700") |
| 29 | + self.root.resizable(False, False) # Disable resizing |
| 30 | + self.root.configure(bg="#1E1E2F") |
| 31 | + |
| 32 | + self.score = 0 |
| 33 | + self.current_question = 0 |
| 34 | + self.selected_category = None |
| 35 | + self.questions = [] |
| 36 | + self.time_left = 15 |
| 37 | + self.timer_running = False |
| 38 | + self.animating = False |
| 39 | + |
| 40 | + self.create_category_screen() |
| 41 | + |
| 42 | + # -------------------- |
| 43 | + # Category Selection |
| 44 | + # -------------------- |
| 45 | + def create_category_screen(self): |
| 46 | + self.stop_animations() |
| 47 | + self.clear_screen() |
| 48 | + title = tk.Label(self.root, text="Choose Your Category", font=("Helvetica", 28, "bold"), fg="#FFD700", bg="#1E1E2F") |
| 49 | + title.pack(pady=50) |
| 50 | + for category in quiz_categories: |
| 51 | + btn = tk.Button(self.root, text=category, font=("Helvetica", 20), width=25, bg="#4B0082", fg="white", |
| 52 | + command=lambda c=category: self.start_quiz(c)) |
| 53 | + btn.pack(pady=15) |
| 54 | + |
| 55 | + # -------------------- |
| 56 | + # Start Quiz |
| 57 | + # -------------------- |
| 58 | + def start_quiz(self, category): |
| 59 | + self.selected_category = category |
| 60 | + self.questions = quiz_categories[category] |
| 61 | + self.current_question = 0 |
| 62 | + self.score = 0 |
| 63 | + self.show_question() |
| 64 | + |
| 65 | + # -------------------- |
| 66 | + # Show Question |
| 67 | + # -------------------- |
| 68 | + def show_question(self): |
| 69 | + if self.current_question >= len(self.questions): |
| 70 | + self.show_result() |
| 71 | + return |
| 72 | + |
| 73 | + self.stop_animations() |
| 74 | + self.clear_screen() |
| 75 | + self.time_left = 15 |
| 76 | + self.timer_running = True |
| 77 | + question_data = self.questions[self.current_question] |
| 78 | + |
| 79 | + # Question label |
| 80 | + self.question_label = tk.Label(self.root, text=question_data["question"], font=("Helvetica", 22, "bold"), |
| 81 | + fg="#00FFFF", bg="#1E1E2F", wraplength=750) |
| 82 | + self.question_label.pack(pady=50) |
| 83 | + |
| 84 | + # Timer progress bar |
| 85 | + self.progress = ttk.Progressbar(self.root, orient='horizontal', length=500, mode='determinate', maximum=15) |
| 86 | + self.progress.pack(pady=10) |
| 87 | + self.update_timer() |
| 88 | + |
| 89 | + # Option buttons |
| 90 | + self.option_buttons = [] |
| 91 | + for option in question_data["options"]: |
| 92 | + btn = tk.Button(self.root, text=option, font=("Helvetica", 18), width=30, bg="#6A5ACD", fg="white", |
| 93 | + command=lambda o=option: self.check_answer(o)) |
| 94 | + btn.pack(pady=10) |
| 95 | + self.option_buttons.append(btn) |
| 96 | + |
| 97 | + # -------------------- |
| 98 | + # Update Timer |
| 99 | + # -------------------- |
| 100 | + def update_timer(self): |
| 101 | + if self.time_left >= 0 and self.timer_running: |
| 102 | + self.progress['value'] = 15 - self.time_left |
| 103 | + self.time_left -= 1 |
| 104 | + self.root.after(1000, self.update_timer) |
| 105 | + elif self.time_left < 0: |
| 106 | + self.timer_running = False |
| 107 | + self.flash_color("#FF4500") # Time up |
| 108 | + self.root.after(800, self.next_question) |
| 109 | + |
| 110 | + # -------------------- |
| 111 | + # Check Answer |
| 112 | + # -------------------- |
| 113 | + def check_answer(self, selected_option): |
| 114 | + if not self.timer_running: |
| 115 | + return |
| 116 | + self.timer_running = False |
| 117 | + |
| 118 | + correct_answer = self.questions[self.current_question]["answer"] |
| 119 | + if selected_option == correct_answer: |
| 120 | + self.score += 1 |
| 121 | + self.flash_color("#00FF00") |
| 122 | + threading.Thread(target=self.play_sound, args=("correct.wav",)).start() |
| 123 | + else: |
| 124 | + self.flash_color("#FF4500") |
| 125 | + threading.Thread(target=self.play_sound, args=("wrong.wav",)).start() |
| 126 | + |
| 127 | + self.root.after(800, self.next_question) |
| 128 | + |
| 129 | + # -------------------- |
| 130 | + # Next Question |
| 131 | + # -------------------- |
| 132 | + def next_question(self): |
| 133 | + self.current_question += 1 |
| 134 | + self.show_question() |
| 135 | + |
| 136 | + # -------------------- |
| 137 | + # Flash Background |
| 138 | + # -------------------- |
| 139 | + def flash_color(self, color): |
| 140 | + original = self.root["bg"] |
| 141 | + self.root.configure(bg=color) |
| 142 | + self.root.after(500, lambda: self.root.configure(bg=original)) |
| 143 | + |
| 144 | + # -------------------- |
| 145 | + # Play Sound |
| 146 | + # -------------------- |
| 147 | + def play_sound(self, filename): |
| 148 | + try: |
| 149 | + winsound.PlaySound(filename, winsound.SND_FILENAME) |
| 150 | + except: |
| 151 | + pass |
| 152 | + |
| 153 | + # -------------------- |
| 154 | + # Show Result with Dynamic Confetti & Animated Text |
| 155 | + # -------------------- |
| 156 | + def show_result(self): |
| 157 | + self.stop_animations() |
| 158 | + self.clear_screen() |
| 159 | + self.animating = True |
| 160 | + |
| 161 | + self.canvas = tk.Canvas(self.root, width=800, height=600, bg="#1E1E2F", highlightthickness=0) |
| 162 | + self.canvas.pack() |
| 163 | + |
| 164 | + # Dynamic confetti based on score |
| 165 | + particles_count = 50 + self.score * 50 # Higher score = more particles |
| 166 | + self.confetti_particles = [] |
| 167 | + self.create_confetti(particles_count) |
| 168 | + |
| 169 | + # Animated text |
| 170 | + self.result_text_id = self.canvas.create_text(400, 100, |
| 171 | + text=f"🏆 Game Over! Your Score: {self.score}/{len(self.questions)} 🏆", |
| 172 | + font=("Helvetica", 24, "bold"), fill="#FFD700") |
| 173 | + self.text_blink = True |
| 174 | + self.animate_text() |
| 175 | + |
| 176 | + # Play celebratory sound |
| 177 | + threading.Thread(target=self.play_sound, args=("celebration.wav",)).start() |
| 178 | + |
| 179 | + # Confetti animation |
| 180 | + self.animate_confetti() |
| 181 | + |
| 182 | + # Buttons container frame |
| 183 | + button_frame = tk.Frame(self.root, bg="#1E1E2F") |
| 184 | + button_frame.pack(side="bottom", pady=30) |
| 185 | + |
| 186 | + # Play Again button |
| 187 | + restart_btn = tk.Button(button_frame, text="Play Again", font=("Helvetica", 20), width=25, bg="#4B0082", fg="white", |
| 188 | + command=self.create_category_screen) |
| 189 | + restart_btn.pack(side="left", padx=10) |
| 190 | + |
| 191 | + # Exit button |
| 192 | + exit_btn = tk.Button(button_frame, text="Exit", font=("Helvetica", 20), width=25, bg="#FF0000", fg="white", |
| 193 | + command=self.root.destroy) |
| 194 | + exit_btn.pack(side="left", padx=10) |
| 195 | + |
| 196 | + # -------------------- |
| 197 | + # Stop animations safely |
| 198 | + # -------------------- |
| 199 | + def stop_animations(self): |
| 200 | + self.animating = False |
| 201 | + self.timer_running = False |
| 202 | + self.text_blink = False |
| 203 | + |
| 204 | + # -------------------- |
| 205 | + # Create Confetti |
| 206 | + # -------------------- |
| 207 | + def create_confetti(self, count=150): |
| 208 | + for _ in range(count): |
| 209 | + x = random.randint(0, 800) |
| 210 | + y = random.randint(-600, 0) |
| 211 | + size = random.randint(5, 12) |
| 212 | + color = random.choice(["#FF0000", "#00FF00", "#0000FF", "#FFFF00", "#FF00FF", "#00FFFF"]) |
| 213 | + particle = {"x": x, "y": y, "size": size, "color": color, "speed": random.randint(3, 8), "shape": None} |
| 214 | + self.confetti_particles.append(particle) |
| 215 | + |
| 216 | + # -------------------- |
| 217 | + # Animate Confetti safely |
| 218 | + # -------------------- |
| 219 | + def animate_confetti(self): |
| 220 | + if self.animating and hasattr(self, 'canvas') and self.canvas.winfo_exists(): |
| 221 | + self.canvas.delete("confetti") |
| 222 | + for p in self.confetti_particles: |
| 223 | + p["y"] += p["speed"] |
| 224 | + if p["y"] > 600: |
| 225 | + p["y"] = random.randint(-100, 0) |
| 226 | + p["x"] = random.randint(0, 800) |
| 227 | + p["shape"] = self.canvas.create_oval(p["x"], p["y"], p["x"] + p["size"], p["y"] + p["size"], |
| 228 | + fill=p["color"], outline="", tags="confetti") |
| 229 | + self.root.after(50, self.animate_confetti) |
| 230 | + |
| 231 | + # -------------------- |
| 232 | + # Animate Blinking Text safely |
| 233 | + # -------------------- |
| 234 | + def animate_text(self): |
| 235 | + if self.animating and hasattr(self, 'canvas') and self.canvas.winfo_exists(): |
| 236 | + if self.text_blink: |
| 237 | + self.canvas.itemconfig(self.result_text_id, fill=random.choice( |
| 238 | + ["#FFD700", "#FF69B4", "#00FFFF", "#00FF00", "#FF4500"])) |
| 239 | + self.root.after(500, self.animate_text) |
| 240 | + |
| 241 | + # -------------------- |
| 242 | + # Clear Screen Utility |
| 243 | + # -------------------- |
| 244 | + def clear_screen(self): |
| 245 | + for widget in self.root.winfo_children(): |
| 246 | + widget.destroy() |
| 247 | + |
| 248 | + |
| 249 | +# ==================== |
| 250 | +# Run the Game |
| 251 | +# ==================== |
| 252 | +if __name__ == "__main__": |
| 253 | + root = tk.Tk() |
| 254 | + game = MiniGameShow(root) |
| 255 | + root.mainloop() |
0 commit comments