找回密码
 中文实名注册
查看: 79|回复: 0

人机五子棋--韩务

[复制链接]

2

主题

2

帖子

377

积分

中级会员

Rank: 3Rank: 3

积分
377
发表于 2025-2-9 16:42:47 | 显示全部楼层 |阅读模式
[Python] 纯文本查看 复制代码
import tkinter as tk
from tkinter import messagebox
import random

# 100 道爱国主义教育题目及答案
questions = [
    {
        "question": "中华人民共和国国歌的原名是什么?",
        "options": ["《大路歌》", "《毕业歌》", "《义勇军进行曲》", "《前进歌》"],
        "answer": "《义勇军进行曲》"
    },
    {
        "question": "新中国成立的时间是?",
        "options": ["1948 年 10 月 1 日", "1949 年 9 月 1 日", "1949 年 10 月 1 日", "1950 年 10 月 1 日"],
        "answer": "1949 年 10 月 1 日"
    },
    # 这里可以继续添加更多题目,共 100 道
    # ...
]

class PatriotismGame:
    def __init__(self, root):
        self.root = root
        self.root.title("爱国主义教育小游戏")
        self.score = 0
        self.question_index = 0
        self.questions = random.sample(questions, len(questions))  # 随机打乱题目顺序

        # 创建界面元素
        self.question_label = tk.Label(root, text="", wraplength=300)
        self.question_label.pack(pady=20)

        self.option_buttons = []
        for i in range(4):
            button = tk.Button(root, text="", command=lambda idx=i: self.check_answer(idx))
            button.pack(pady=5)
            self.option_buttons.append(button)

        self.next_question()

    def next_question(self):
        if self.question_index < len(self.questions):
            current_question = self.questions[self.question_index]
            self.question_label.config(text=current_question["question"])
            options = current_question["options"]
            for i in range(4):
                self.option_buttons.config(text=options)
        else:
            self.show_result()

    def check_answer(self, selected_index):
        current_question = self.questions[self.question_index]
        selected_option = current_question["options"][selected_index]
        if selected_option == current_question["answer"]:
            self.score += 1
            messagebox.showinfo("正确", "回答正确!")
        else:
            messagebox.showerror("错误", f"回答错误,正确答案是:{current_question['answer']}")
        self.question_index += 1
        self.next_question()

    def show_result(self):
        messagebox.showinfo("游戏结束", f"游戏结束,你的得分是:{self.score}/{len(self.questions)}")
        self.root.destroy()

if __name__ == "__main__":
    root = tk.Tk()
    game = PatriotismGame(root)
    root.mainloop()
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 中文实名注册

本版积分规则

小黑屋|东台市机器人学会 ( 苏ICP备2021035350号-1;苏ICP备2021035350号-2;苏ICP备2021035350号-3 )

GMT+8, 2025-3-14 20:44 , Processed in 0.042593 second(s), 28 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表