import tkinter as tk
from tkinter import messagebox
import random

# 唐诗题库，可继续扩充
tang_poems = [
    {"author": "李白", "title": "静夜思",
     "up": "床前明月光", "down": "疑是地上霜"},
    {"author": "李白", "title": "静夜思",
     "up": "举头望明月", "down": "低头思故乡"},
    {"author": "孟浩然", "title": "春晓",
     "up": "春眠不觉晓", "down": "处处闻啼鸟"},
    {"author": "孟浩然", "title": "春晓",
     "up": "夜来风雨声", "down": "花落知多少"},
    {"author": "王之涣", "title": "登鹳雀楼",
     "up": "白日依山尽", "down": "黄河入海流"},
    {"author": "王之涣", "title": "登鹳雀楼",
     "up": "欲穷千里目", "down": "更上一层楼"},
    {"author": "杜甫", "title": "绝句",
     "up": "两个黄鹂鸣翠柳", "down": "一行白鹭上青天"},
    {"author": "王维", "title": "相思",
     "up": "红豆生南国", "down": "春来发几枝"},
    {"author": "王维", "title": "相思",
     "up": "愿君多采撷", "down": "此物最相思"},
    {"author": "贺知章", "title": "咏柳",
     "up": "碧玉妆成一树高", "down": "万条垂下绿丝绦"},
]


class TangPoemGame:
    def __init__(self, root):
        self.root = root
        self.root.title("唐诗三百首答题游戏")
        self.root.geometry("550x400")
        self.root.resizable(False, False)

        self.score = 0  # 得分
        self.total = 0  # 总答题数
        self.current_poem = None

        # 标题
        tk.Label(root, text="📜 唐诗三百首答题游戏", font=("微软雅黑", 20)).pack(pady=10)

        # 分数显示
        self.score_label = tk.Label(root, text=f"得分：{self.score} / 总题：{self.total}", font=("微软雅黑", 12))
        self.score_label.pack()

        # 题目区域
        self.question_label = tk.Label(root, text="", font=("微软雅黑", 16), wraplength=500)
        self.question_label.pack(pady=30)

        # 输入框
        tk.Label(root, text="请填写下一句：", font=("微软雅黑",12)).pack()
        self.answer_entry = tk.Entry(root, font=("微软雅黑",14), width=35)
        self.answer_entry.pack(pady=8)

        # 按钮行
        frame_btn = tk.Frame(root)
        frame_btn.pack(pady=15)

        tk.Button(frame_btn, text="提交答案", command=self.check_answer, font=("微软雅黑",12), width=10).grid(row=0, column=0, padx=8)
        tk.Button(frame_btn, text="下一题", command=self.next_question, font=("微软雅黑",12), width=10).grid(row=0, column=1, padx=8)
        tk.Button(frame_btn, text="重新开始", command=self.restart_game, font=("微软雅黑",12), width=10).grid(row=0, column=2, padx=8)

        # 提示信息
        self.msg_label = tk.Label(root, text="", font=("微软雅黑",12), fg="#d03030")
        self.msg_label.pack(pady=10)

        self.next_question()

    def next_question(self):
        """随机抽取一题"""
        self.current_poem = random.choice(tang_poems)
        up_sentence = self.current_poem["up"]
        author = self.current_poem["author"]
        title = self.current_poem["title"]
        self.question_label.config(text=f"【{title}】{author}\n上句：{up_sentence}")
        self.answer_entry.delete(0, tk.END)
        self.msg_label.config(text="")

    def check_answer(self):
        """核对答案"""
        user_ans = self.answer_entry.get().strip()
        right_ans = self.current_poem["down"]
        self.total += 1

        if user_ans == right_ans:
            self.score +=1
            self.msg_label.config(text="✅回答正确！", fg="green")
        else:
            self.msg_label.config(text=f"❌回答错误，正确答案：{right_ans}", fg="#d03030")

        self.score_label.config(text=f"得分：{self.score} / 总题：{self.total}")

    def restart_game(self):
        """重置游戏"""
        self.score = 0
        self.total = 0
        self.score_label.config(text=f"得分：{self.score} / 总题：{self.total}")
        self.msg_label.config(text="")
        self.next_question()


if __name__ == "__main__":
    win = tk.Tk()
    app = TangPoemGame(win)
    win.mainloop()
