python自创小游戏

用AI写了一个小游戏程序,经过正面反面的不断提问与探索!始终解决不了的问题!
无法实现重复运行!而且在游戏的过渡阶段还出现了要求意外的数字闪烁!

import tkinter as tk
from tkinter import messagebox
import random


class MemoryGame:
    def __init__(self, root, size=3):
        self.root = root
        self.size = size
        self.buttons = []
        self.selected = []
        self.correct_count = 0
        self.wrong_count = 0
        self.blinks = 0
        self.sequence = []
        self.create_widgets()
        self.blinking_buttons = set()

    def create_widgets(self):
        # 创建游戏次数和错误次数布局
        self.total_label = tk.Label(self.root, text=f'Total: 0', font=('Arial', 16))
        self.total_label.grid(row=0, column=0, padx=40, pady=10)
        self.correct_label = tk.Label(self.root, text=f'Correct: 0', font=('Arial', 16))
        self.correct_label.grid(row=0, column=1, pady=10)
        self.wrong_label = tk.Label(self.root, text=f'Wrong: 0', font=('Arial', 16))
        self.wrong_label.grid(row=0, column=2, padx=40, pady=10)

        # 创建九宫格布局
        self.create_buttons()

        # 创建下方按钮布局
        start_button = tk.Button(self.root, text='Start game', font=('Arial', 16), width=10, command=self.start_game)
        start_button.grid(row=self.size+2, column=0, padx=20, pady=10)

        quit_button = tk.Button(self.root, text='Quit game', font=('Arial', 16), width=10, command=self.on_quit)
        quit_button.grid(row=self.size+2, column=2, padx=20, pady=10)

    def create_buttons(self):
        for button in sum(self.buttons, []):
            button.destroy()
        self.buttons = []
        self.nums = random.sample(range(10), self.size**2)
        for i in range(self.size):
            row = []
            for j in range(self.size):
                num = self.nums[i*self.size+j]
                button = tk.Button(self.root, text=num, font=('Arial', 24), width=4, height=2, command=lambda num=num: self.on_button_click(num))
                button.grid(row=i+1, column=j, padx=8, pady=8, ipadx=10, ipady=10)
                row.append(button)
            self.buttons.append(row)

    def start_game(self):
        # 重置状态
        for row in self.buttons:
            for button in row:
                button.config(bg='SystemButtonFace', fg='black')
        self.selected = []
        self.correct_count = 0
        self.wrong_count = 0
        self.sequence = random.sample(self.nums, 3)
        self.total_label.config(text=f'Total: {self.correct_count + self.wrong_count}')
        self.correct_label.config(text=f'Correct: {self.correct_count}')
        self.wrong_label.config(text=f'Wrong: {self.wrong_count}')
        self.show_sequence()

    def show_sequence(self):
        # 通过改变按钮颜色来提示出现顺序
        if self.blinks < len(self.sequence):
            nums = self.sequence[self.blinks]
            for row in self.buttons:
                for button in row:
                    if int(button['text']) == nums and button not in self.blinking_buttons:
                        button.config(bg='white')
                        self.blinking_buttons.add(button)
            self.root.after(500, self.resume_button, nums)
        else:
            # 显示完成,开始接受用户输入
            self.blinking_buttons.clear()

    def resume_button(self, num):
        # 恢复按钮颜色
        for row in self.buttons:
            for button in row:
                if int(button['text']) == num:
                    button.config(bg='SystemButtonFace')
                    self.blinking_buttons.remove(button)
        if self.blinks < len(self.sequence) - 1:
            self.blinks += 1
            self.root.after(500, self.show_sequence)

    def check_answer(self):
        if self.selected == self.sequence:
            # 点击正确
            self.correct_count += 1
            messagebox.showinfo('Correct', 'Good job!')
            self.update_score()
            self.selected = []
            self.blinks = 0
            self.blink_random_buttons(3)
        else:
            # 点击错误
            self.wrong_count += 1
            messagebox.showinfo('Wrong', 'Wrong answer!')
            self.update_score()
            self.selected = []
            self.blinks = 0
            self.blink_random_buttons(3)

    def update_score(self):
        # 更新积分
        self.total_label.config(text=f'Total: {self.correct_count + self.wrong_count}')
        self.correct_label.config(text=f'Correct: {self.correct_count}')
        self.wrong_label.config(text=f'Wrong: {self.wrong_count}')

    def on_button_click(self, num):
        # 检查选择的数字是否正确
        self.selected.append(num)
        if len(self.selected) == 3:
            self.check_answer()

    def on_quit(self):
        # 退出游戏
        messagebox.showinfo('Game over', 'You quit the game!')
        self.root.quit()

    def blink(self, button):
        button.config(bg='white')
        self.blinking_buttons.add(button)

    def unblink_buttons(self, buttons):
        for button in buttons:
            button.config(bg='SystemButtonFace')
            self.blinking_buttons.remove(button)

    def blink_random_buttons(self, remaining):
        if remaining <= 0:
            return
        buttons = random.sample(sum(self.buttons, []), 3)
        for button in buttons:
            self.blink(button)
        self.root.after(1500, self.unblink_buttons, buttons)
        self.root.after(2000, self.show_sequence)

if __name__ == '__main__':
    root = tk.Tk()
    root.title('Memory Game')
    root.resizable(False, False)
    game = MemoryGame(root)

    root.mainloop()