import tkinter as tk import random import time from tkinter import messagebox
class ShadowMemoryGame: def init(self, root): self.root = root self.root.title("Shadow Memory")
self.buttons = []
self.sequence = []
self.user_input = []
self.round = 1
self.create_grid()
self.start_button = tk.Button(root, text="Start Game", command=self.start_game)
self.start_button.grid(row=3, column=0, columnspan=3)
def create_grid(self):
for i in range(3):
for j in range(3):
btn = tk.Button(self.root, text="", width=10, height=5,
command=lambda idx=3*i+j: self.user_click(idx))
btn.grid(row=i, column=j)
self.buttons.append(btn)
def flash_button(self, index, color="gray"):
self.buttons[index].config(bg=color)
self.root.update()
time.sleep(0.5)
self.buttons[index].config(bg="SystemButtonFace")
self.root.update()
def show_sequence(self):
self.sequence = [random.randint(0, 8) for _ in range(self.round)]
for index in self.sequence:
self.flash_button(index)
def user_click(self, index):
self.user_input.append(index)
self.flash_button(index, color="lightblue")
if self.user_input != self.sequence[:len(self.user_input)]:
messagebox.showinfo("Game Over", f"Wrong sequence! You reached round {self.round}")
self.reset_game()
elif len(self.user_input) == len(self.sequence):
self.round += 1
self.user_input = []
self.root.after(1000, self.start_game)
def start_game(self):
self.sequence = []
self.user_input = []
self.show_sequence()
def reset_game(self):
self.sequence = []
self.user_input = []
self.round = 1
root = tk.Tk() game = ShadowMemoryGame(root) root.mainloop() this is the code of the game