网站安全建设总结报告,网络推广公司代理,做软件项目需不需要有网站,做网站注册验证码引言
在Python入门到精通#xff08;二#xff09;中#xff0c;我们了解基本的控制流以及重要的数据结构#xff1a;列表、字典、 集合等。
现在编写一个小游戏对数据结构、控制流进行简单应用#xff0c;巩固基础#xff0c;加深理解。 文章目录引言文字冒险游戏说明…引言在Python入门到精通二中我们了解基本的控制流以及重要的数据结构列表、字典、 集合等。现在编写一个小游戏对数据结构、控制流进行简单应用巩固基础加深理解。文章目录引言文字冒险游戏说明一、游戏实现思路二、涉及数据结构三、代码实现及注释其他文字冒险游戏说明一、游戏实现思路1初始化游戏创建游戏实例初始化数据结构玩家状态字典游戏地图位置字典敌人列表游戏结束标志2游戏主循环游戏开始 ↓ 显示欢迎信息 ↓ 获取玩家姓名 ↓[游戏循环开始]↓ 显示状态生命值/位置/背包 ↓ 获取玩家指令移动/查看/拾取/使用/退出 ↓ 根据指令执行对应操作 ↓ 处理特殊事件危险/谜题 ↓ 更新游戏状态 ↓[检查游戏结束条件]生命值0或玩家退出 ↓ 循环继续/游戏结束3核心功能流程移动系统输入移动方向 → 检查出口是否存在 → 更新位置 → 显示新位置描述 → 触发危险检查 → 触发谜题检查战斗系统流程遇到敌人 → 随机生成攻击值 → 计算伤害 → 更新生命值 → 检查是否死亡 → 更新游戏状态物品系统流程拾取检查物品存在 → 移到背包 → 从地图移除 使用检查背包 → 执行效果 → 移除物品消耗品二、涉及数据结构字典· self.player存储玩家信息包括姓名、生命值、物品清单和当前位置self.player{name:,# 字符串玩家姓名health:100,# 整数生命值inventory:[手电筒],# 列表背包物品location:森林入口# 字符串当前位置}· self.locations存储每个位置的信息包括描述、出口、物品、危险概率和谜题标志self.locations{森林入口:{description:...,# 位置描述exits:{北:黑暗洞穴,...},# 出口映射items:[木棍],# 可拾取物品danger:30,# 危险概率可选puzzle:True# 谜题标志可选}}列表· self.enemies存储敌人类型self.enemies[哥布林,狼,...]# 敌人类型集合· self.player[“inventory”]存储玩家拥有的物品self.player[inventory][手电筒]# 动态存储物品· self.locations[location][“items”]存储每个位置可拾取的物品self.locations[location][items][]# 位置物品列表字符串String用于描述、名称等文本信息三、代码实现及注释importtimeimportrandomclassTextAdventureGame:# 初始化def__init__(self):# 初始化玩家属性使用字典存储self.player{name:,# 玩家姓名health:100,# 生命值inventory:[手电筒],# 背包初始有一个手电筒location:森林入口# 初始位置}# 游戏地图使用字典存储每个位置的信息每个位置也是一个字典self.locations{森林入口:{description:你站在一片神秘的森林入口。阳光透过树叶洒下斑驳的光影。,exits:{北:黑暗洞穴,东:古老神庙,西:河边},# 出口用字典表示方向与位置的关系items:[木棍]# 该位置的物品列表},# 其他位置类似...}# 敌人列表self.enemies[哥布林,狼,巨蜘蛛,骷髅战士]# 游戏结束标志self.game_overFalse# 缓慢打印文字增加游戏氛围defprint_slowly(self,text,delay0.03):forcharintext:print(char,end,flushTrue)time.sleep(delay)print()# 显示玩家状态defshow_status(self):print(f\n{*50})print(f{self.player[name]}| ❤️ 生命值:{self.player[health]})print(f 位置:{self.player[location]})# 如果背包不为空则用逗号分隔物品否则显示空print(f 背包:{, .join(self.player[inventory])ifself.player[inventory]else空})print(f{*50})# 处理危险事件defhandle_danger(self):# 获取当前位置的危险概率如果没有则默认为0danger_chanceself.locations[self.player[location]].get(danger,0)# 根据危险概率随机决定是否遇到危险ifrandom.randint(1,100)danger_chance:enemyrandom.choice(self.enemies)self.print_slowly(f\n⚔️ 突然一只{enemy}跳了出来)# 简单的战斗系统随机生成玩家和敌人的攻击力player_attackrandom.randint(10,30)enemy_attackrandom.randint(5,25)# 玩家受到伤害self.player[health]-enemy_attackprint(f你造成了{player_attack}点伤害)print(f{enemy}造成了{enemy_attack}点伤害)print(f你的生命值:{self.player[health]})# 检查玩家是否死亡ifself.player[health]0:self.print_slowly( 你被击败了...游戏结束)self.game_overTruereturnFalseelse:print(f你击败了{enemy})returnTruereturnFalse# 处理谜题defhandle_puzzle(self):# 检查当前位置是否有谜题ifself.locations[self.player[location]].get(puzzle):self.print_slowly(\n 你发现了一个谜题)self.print_slowly(古老的石板上刻着我始于结束终于开始。我是什么)answerinput(你的答案是什么 ).strip().lower()# 允许一些答案变体ifanswerin[时间,时钟,沙漏,time,clock]:self.print_slowly(✅ 石门缓缓打开你获得了神秘护符)self.player[inventory].append(神秘护符)# 恢复生命值但不超过150self.player[health]min(150,self.player[health]50)print(❤️ 生命值恢复了50点)else:self.print_slowly(❌ 回答错误什么也没有发生...)# 查看当前位置deflook_around(self):location_infoself.locations[self.player[location]]self.print_slowly(f\n{location_info[description]})# 如果有物品则显示iflocation_info.get(items):self.print_slowly(f你发现了:{, .join(location_info[items])})# 显示出口self.print_slowly(f出口:{, .join(location_info[exits].keys())})# 移动到新位置defmove(self,direction):currentself.player[location]exitsself.locations[current][exits]ifdirectioninexits:new_locationexits[direction]self.player[location]new_location self.print_slowly(f\n 你向{direction}走来到了{new_location})self.look_around()# 检查危险self.handle_danger()# 如果游戏没有结束检查谜题ifnotself.game_over:self.handle_puzzle()returnTrueelse:self.print_slowly(f\n❌ 那里没有路)returnFalse# 拾取物品deftake_item(self,item):locationself.player[location]itemsself.locations[location].get(items,[])ifiteminitems:# 将物品从地图移到背包self.player[inventory].append(item)items.remove(item)self.print_slowly(f\n✅ 你拾取了{item})returnTrueelse:self.print_slowly(f\n❌ 这里没有{item})returnFalse# 使用物品defuse_item(self,item):ifiteminself.player[inventory]:ifitem神秘护符:self.print_slowly(\n✨ 神秘护符发出光芒你感觉充满了力量)# 使用护符恢复生命值但不超过200self.player[health]min(200,self.player[health]100)print(f❤️ 生命值恢复了100点当前:{self.player[health]})self.player[inventory].remove(item)# 使用后移除else:self.print_slowly(f\n 你使用了{item}但似乎没什么效果...)returnTrueelse:self.print_slowly(f\n❌ 你的背包里没有{item})returnFalse# 主游戏循环defplay(self):self.print_slowly( 欢迎来到文字冒险游戏)self.player[name]input(请输入你的名字: )self.print_slowly(f\n你好{self.player[name]}开始你的冒险吧)self.look_around()# 循环直至结束whilenotself.game_over:self.show_status()actioninput(\n你要做什么(移动/查看/拾取/使用/退出): ).strip().lower()ifaction移动:directioninput(往哪个方向(北/南/东/西): ).strip()self.move(direction)elifaction查看:self.look_around()elifaction拾取:iteminput(拾取什么物品: ).strip()self.take_item(item)elifaction使用:iteminput(使用什么物品: ).strip()self.use_item(item)elifaction退出:self.print_slowly( 再见欢迎下次再来冒险)breakelse:self.print_slowly(❌ 我不明白这个指令...)# 如果是因为生命值耗尽而结束显示游戏结束ifself.player[health]0:self.print_slowly(\n 游戏结束)# 运行游戏if__name____main__:gameTextAdventureGame()game.play()其他print函数基本语法print(*objects, sep’ ‘, end’\n’, filesys.stdout, flushFalse)参数说明objects表示可以一次输出多个对象用逗号分隔。sep用来间隔多个对象默认是一个空格。end用来设定以什么结尾默认是换行符\n。file要写入的文件对象默认是标准输出sys.stdout。flush输出是否被强制刷新默认False。