欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 新车 > python实现tkinter解密剧情文本游戏

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

2025/3/31 21:38:22 来源:https://blog.csdn.net/licy__/article/details/143506983  浏览:    关键词:python实现tkinter解密剧情文本游戏

目录

需求

效果

代码实现

代码说明


需求

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

效果

代码实现

import tkinter as tkclass StoryGame:def __init__(self, master):self.master = mastermaster.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_nodeself.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 = mastermaster.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_nodeself.update_story()
    • choose_option(self, next_node): 处理玩家选择的选项。
      • self.current_node = next_node: 更新当前节点为选择的下一个节点。
      • self.update_story(): 更新故事内容和按钮。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词