import random import tkinter as tk from tkinter import messagebox class ColorPredictionGame: def __init__(self, master): self.master = master self.master.title("Color Prediction Game") self.colors = ["Red", "Green", "Blue", "Yellow", "Orange", "Purple", "Pink"] self.score = 0 self.rounds = 5 self.current_round = 0 self.label = tk.Label(master, text="Predict the color!", font=("Helvetica", 16)) self.label.pack(pady=20) self.color_button = tk.Button(master, text="Pick a Color", command=self.pick_color) self.color_button.pack(pady=10) self.score_label = tk.Label(master, text=f"Score: {self.score}", font=("Helvetica", 14)) self.score_label.pack(pady=20) self.quit_button = tk.Button(master, text="Quit", command=master.quit) self.quit_button.pack(pady=10) def pick_color(self): if self.current_round < self.rounds: self.current_color = random.choice(self.colors) self.label.config(text=f"Is the color {self.current_color}? (Yes/No)") self.master.bind("", self.check_prediction) self.current_round += 1 else: messagebox.showinfo("Game Over", f"Game Over! Your final score is: {self.score}") self.master.quit() def check_prediction(self, event): answer = self.label.cget("text").split()[-1] if answer == "Yes": self.score += 1 self.score_label.config(text=f"Score: {self.score}") self.pick_color() if __name__ == "__main__": root = tk.Tk() game = ColorPredictionGame(root) root.mainloop()