python实现tkinter解密剧情文本游戏

目录

需求

效果

代码实现

代码说明


需求

python实现tkinter解密剧情文本游戏

效果

代码实现

import tkinter as tk

class StoryGame:
    def __init__(self, master):
        self.master = master
        master.title("剧情游戏")

        # 初始化故事节点
        self.current_node = 0

        # 故事节点数据
        self.story_nodes = {
            0: {
                "text": "你醒来在一个陌生的房间里,四周一片漆黑。",
                "options": [
                    {"text": "寻找出口", "next_node": 1},
                    {"text": "大声呼救", "next_node": 2}
                ]
            },
            1: {
                "text": "你在房间的角落找到了一扇门,门是锁着的。",
                "options": [
                    {"text": "寻找钥匙", "next_node": 3},
                    {"text": "用力撞门", "next_node": 4}
                ]
            },
            2: {
                "text": "你大声呼救,但没有人回应。",
                "options": [
                    {"text": "继续呼救", "next_node": 2},
                    {"text": "寻找出口", "next_node": 1}
                ]
            },
            3: {
                "text": "你在房间里找到了一把钥匙,成功打开了门。",
                "options": [
                    {"text": "走出房间", "next_node": 5}
                ]
            },
            4: {
                "text": "你用力撞门,门没有打开,反而受伤了。",
                "options": [
                    {"text": "寻找钥匙", "next_node": 3},
                    {"text": "休息一下", "next_node": 6}
                ]
            },
            5: {
                "text": "你走出了房间,发现外面是一片森林。",
                "options": [
                    {"text": "继续前进", "next_node": 7}
                ]
            },
            6: {
                "text": "你休息了一会儿,感觉好多了。",
                "options": [
                    {"text": "再次尝试撞门", "next_node": 4},
                    {"text": "寻找钥匙", "next_node": 3}
                ]
            },
            7: {
                "text": "你在森林里走了很久,终于看到了一座小屋。",
                "options": [
                    {"text": "敲门", "next_node": 8},
                    {"text": "绕过小屋", "next_node": 9}
                ]
            },
            8: {
                "text": "你敲了敲门,一位老人开了门。他邀请你进去。",
                "options": [
                    {"text": "接受邀请", "next_node": 10},
                    {"text": "拒绝邀请", "next_node": 11}
                ]
            },
            9: {
                "text": "你绕过了小屋,继续在森林里探险。",
                "options": [
                    {"text": "继续前进", "next_node": 12}
                ]
            },
            10: {
                "text": "老人告诉你,你是被恶龙抓来的英雄,需要拯救世界。",
                "options": [
                    {"text": "接受任务", "next_node": 13},
                    {"text": "拒绝任务", "next_node": 14}
                ]
            },
            11: {
                "text": "你拒绝了老人的邀请,继续在森林里探险。",
                "options": [
                    {"text": "继续前进", "next_node": 9}
                ]
            },
            12: {
                "text": "你在森林里迷路了,最终回到了小屋。",
                "options": [
                    {"text": "敲门", "next_node": 8}
                ]
            },
            13: {
                "text": "你接受了任务,踏上了拯救世界的旅程。",
                "options": []
            },
            14: {
                "text": "你拒绝了任务,决定独自探索这个世界。",
                "options": []
            }
        }

        # 创建故事文本显示区域
        self.text_label = tk.Label(master, text="", font=('Arial', 14), wraplength=400, justify='left')
        self.text_label.pack(pady=10)

        # 创建按钮容器
        self.button_frame = tk.Frame(master)
        self.button_frame.pack()

        # 初始化故事
        self.update_story()

    def update_story(self):
        node = self.story_nodes[self.current_node]
        self.text_label.config(text=node["text"])

        # 清除旧的按钮
        for widget in self.button_frame.winfo_children():
            widget.destroy()

        # 创建新的按钮
        for option in node["options"]:
            button = tk.Button(self.button_frame, text=option["text"], command=lambda next_node=option["next_node"]: self.choose_option(next_node))
            button.pack(side=tk.LEFT, padx=5, pady=5)

    def choose_option(self, next_node):
        self.current_node = next_node
        self.update_story()

if __name__ == "__main__":
    root = tk.Tk()
    game = StoryGame(root)
    root.mainloop()

代码说明

  1. 导入模块

    import tkinter as tk
    • tkinter: 这是 Python 的标准 GUI 库,用于创建图形用户界面。
  2. 创建 StoryGame

    class StoryGame:
        def __init__(self, master):
            self.master = master
            master.title("剧情游戏")
    
            # 初始化故事节点
            self.current_node = 0
    
            # 故事节点数据
            self.story_nodes = {
                0: {
                    "text": "你醒来在一个陌生的房间里,四周一片漆黑。",
                    "options": [
                        {"text": "寻找出口", "next_node": 1},
                        {"text": "大声呼救", "next_node": 2}
                    ]
                },
                ...
            }
    
            # 创建故事文本显示区域
            self.text_label = tk.Label(master, text="", font=('Arial', 14), wraplength=400, justify='left')
            self.text_label.pack(pady=10)
    
            # 创建按钮容器
            self.button_frame = tk.Frame(master)
            self.button_frame.pack()
    
            # 初始化故事
            self.update_story()
    • __init__(self, master): 构造函数,初始化游戏的主要组件。
      • self.master = master: 将传入的主窗口对象赋值给 self.master
      • master.title("剧情游戏"): 设置主窗口的标题。
      • self.current_node = 0: 初始化当前故事节点为 0。
      • self.story_nodes: 存储故事节点的数据,每个节点包含文本和选项。
      • self.text_label: 创建一个标签,用于显示当前的故事文本。
      • self.button_frame: 创建一个框架,用于存放按钮。
      • self.update_story(): 初始化故事,显示第一个节点的内容。
  3. 更新故事内容

    def update_story(self):
        node = self.story_nodes[self.current_node]
        self.text_label.config(text=node["text"])
    
        # 清除旧的按钮
        for widget in self.button_frame.winfo_children():
            widget.destroy()
    
        # 创建新的按钮
        for option in node["options"]:
            button = tk.Button(self.button_frame, text=option["text"], command=lambda next_node=option["next_node"]: self.choose_option(next_node))
            button.pack(side=tk.LEFT, padx=5, pady=5)
    • update_story(self): 更新当前显示的故事内容和按钮。
      • node = self.story_nodes[self.current_node]: 获取当前节点的数据。
      • self.text_label.config(text=node["text"]): 更新标签的文本内容。
      • for widget in self.button_frame.winfo_children(): widget.destroy(): 清除旧的按钮。
      • for option in node["options"]: 遍历当前节点的选项,为每个选项创建一个按钮,并绑定点击事件到 choose_option 方法。
  4. 处理选项选择

    def choose_option(self, next_node):
        self.current_node = next_node
        self.update_story()
    • choose_option(self, next_node): 处理玩家选择的选项。
      • self.current_node = next_node: 更新当前节点为选择的下一个节点。
      • self.update_story(): 更新故事内容和按钮。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

licy__

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值