import tkinter as tk
import random
from tkinter import messagebox

# 题目库：(内容,正确类型)
question_list = [
    ("123", "int"),
    ('"123"', "str"),
    ("3.14", "float"),
    ("True", "bool"),
    ("[1,2,3]", "list"),
    ("-50", "int"),
    ('"hello"', "str"),
    ("0.001", "float"),
    ("False", "bool"),
    ("['a','b']", "list"),
    ("999", "int"),
    ('""', "str"),
    ("2.5", "float"),
    ("[True,False]", "list"),
]

class HitMoleTypeGame:
    def __init__(self, root):
        self.root = root
        self.root.title("打地鼠 — Python数据类型识别")
        self.root.geometry("500x450")

        self.score = 0
        self.time_left = 30
        self.game_running = False

        # 顶部信息栏
        info_frame = tk.Frame(root)
        info_frame.pack(pady=5)
        self.score_label = tk.Label(info_frame, text=f"分数：{self.score}", font=("SimHei",14))
        self.score_label.grid(row=0,column=0,padx=20)
        self.time_label = tk.Label(info_frame, text=f"剩余时间：{self.time_left}", font=("SimHei",14))
        self.time_label.grid(row=0,column=1,padx=20)

        # 地鼠区域 3行3列
        self.mole_buttons = []
        mole_frame = tk.Frame(root)
        mole_frame.pack()
        for i in range(3):
            row_btn = []
            for j in range(3):
                btn = tk.Button(mole_frame, text="洞", width=8, height=3, font=("SimHei",12),
                                command=lambda r=i,c=j:self.hit_mole(r,c))
                btn.grid(row=i,column=j,padx=5,pady=5)
                row_btn.append(btn)
            self.mole_buttons.append(row_btn)

        # 开始按钮
        self.start_btn = tk.Button(root, text="开始游戏", font=("SimHei",14), command=self.start_game)
        self.start_btn.pack(pady=10)

        self.current_mole_pos = None
        self.mole_timer = None

    def start_game(self):
        if self.game_running:
            return
        self.score = 0
        self.time_left =30
        self.update_info()
        self.game_running = True
        self.start_btn.config(state="disabled")
        self.next_mole()
        self.countdown()

    def next_mole(self):
        if not self.game_running:
            return
        # 把上一个地鼠重置
        if self.current_mole_pos is not None:
            r,c = self.current_mole_pos
            self.mole_buttons[r][c].config(text="洞", bg="#f0f0f0")
        # 随机新地鼠位置
        r = random.randint(0,2)
        c = random.randint(0,2)
        self.current_mole_pos = (r,c)
        self.mole_buttons[r][c].config(text="🐹", bg="#ffb84d")
        # 地鼠停留0.7~1.3秒随机消失
        wait = random.uniform(0.7,1.3)
        self.mole_timer = self.root.after(int(wait*1000), self.next_mole)

    def hit_mole(self, r, c):
        if not self.game_running:
            return
        # 判断是否打到地鼠
        if self.current_mole_pos == (r,c):
            # 打中，弹出数据类型题目
            self.show_question()
            # 地鼠立刻消失
            self.mole_buttons[r][c].config(text="洞", bg="#f0f0f0")
            self.current_mole_pos = None
            self.root.after_cancel(self.mole_timer)
            self.next_mole()

    def show_question(self):
        q_text, ans_type = random.choice(question_list)
        popup = tk.Toplevel(self.root)
        popup.title("答题！判断数据类型")
        popup.geometry("350x400")
        tk.Label(popup, text=f"表达式：{q_text}", font=("SimHei",13)).pack(pady=10)
        tk.Label(popup, text="请选择类型：", font=("SimHei",12)).pack()

        def check_answer(user_ans):
            nonlocal popup
            if user_ans == ans_type:
                self.score +=10
                messagebox.showinfo("结果✅","答对啦！+10分")
            else:
                messagebox.showerror("结果❌",f"答错！正确是：{ans_type}")
            self.update_info()
            popup.destroy()

        btn_frame = tk.Frame(popup)
        btn_frame.pack(pady=10)
        types_list = ["int","str","float","bool","list"]
        for t in types_list:
            tk.Button(btn_frame, text=t, command=lambda x=t:check_answer(x)).grid(padx=3)

    def update_info(self):
        self.score_label.config(text=f"分数：{self.score}")
        self.time_label.config(text=f"剩余时间：{self.time_left}")

    def countdown(self):
        if not self.game_running:
            return
        self.time_left -=1
        self.update_info()
        if self.time_left <=0:
            self.game_over()
        else:
            self.root.after(1000, self.countdown)

    def game_over(self):
        self.game_running = False
        self.root.after_cancel(self.mole_timer)
        messagebox.showinfo("游戏结束", f"时间到！你的总分：{self.score}")
        self.start_btn.config(state="normal")
        # 重置所有按钮
        for i in range(3):
            for j in range(3):
                self.mole_buttons[i][j].config(text="洞", bg="#f0f0f0")

if __name__ == "__main__":
    win = tk.Tk()
    game = HitMoleTypeGame(win)
    win.mainloop()
