地牢爬行游戏roomChoice始终输出1,房间切换功能故障求助
地牢爬行游戏移动功能修复方案
核心问题梳理
当前游戏无法切换房间的原因集中在变量作用域混乱、函数参数传递错误和逻辑判断失效三个方面:
- movement函数直接覆盖传入参数,无视用户输入的房间状态
- 关键变量
direction未传递到movement函数,导致移动逻辑无法触发 - 错误的while循环条件使得房间号更新代码从未执行
- 全局变量与局部变量同名冲突,导致状态传递失效
- 房间跳转逻辑存在笔误(如ns方向指向Room1而非Room5,房间5/6错误调用room4)
修复后的完整代码
import random import sys START_ROOM = 1 FINAL_ROOM = 6 START_GOLD = 10 # 暂未实现的函数占位(避免运行报错) def story1(): print("你进入了初始房间,这里是地牢的入口...") def shop(): print("房间里有个小商店,暂时还没开放...") def combat(): print("遭遇了一只小怪物,轻松解决了它!") def room2(goldAmount, visited_room, chealth): if not visited_room: print("你来到了Room2,这里有5金币") goldAmount +=5 visited_room = True else: print("你已经来过Room2了") direction = input("选择移动方向 [n]orth, [s]outh: ") while direction not in ["n", "s"]: direction = input("无效输入,请输入[n]/[s]: ") return movement(2, goldAmount, visited_room, chealth, direction) def room3(goldAmount, visited_room, chealth): if not visited_room: print("你来到了Room3,这里有8金币") goldAmount +=8 visited_room = True else: print("你已经来过Room3了") direction = input("选择移动方向 [n]orth: ") while direction != "n": direction = input("无效输入,请输入[n]: ") return movement(3, goldAmount, visited_room, chealth, direction) def room4(goldAmount, visited_room, chealth): if not visited_room: print("你来到了Room4,这里有12金币") goldAmount +=12 visited_room = True else: print("你已经来过Room4了") direction = input("选择移动方向 [w]est: ") while direction != "w": direction = input("无效输入,请输入[w]: ") return movement(4, goldAmount, visited_room, chealth, direction) def room5(goldAmount, visited_room, chealth): if not visited_room: print("你来到了Room5,这里有15金币") goldAmount +=15 visited_room = True else: print("你已经来过Room5了") direction = input("选择移动方向 [e]ast: ") while direction != "e": direction = input("无效输入,请输入[e]: ") return movement(5, goldAmount, visited_room, chealth, direction) def room6(goldAmount, visited_room, chealth): print("你到达了最终房间,成功逃出地牢!") return (6, goldAmount, visited_room, chealth) def room1(goldAmount, visited_room, chealth): print(f"你的当前生命值: {chealth}") if not visited_room: story1() gold = 10 print(f"房间里有{gold}枚金币") goldAmount += gold print(f"拾取后你现在有{goldAmount}枚金币") visited_room = True shop() combat() else: print("你已经来过这个房间了") shop() direction = input("选择移动方向 [n]orth, [ns]north-south, [s]outh, [se]south-east, [e]ast, [w]est: ") valid_directions = ["n", "s", "e", "w", "ns", "se"] while direction not in valid_directions: print("无效输入...") direction = input("请输入[n]/[s]/[e]/[w]/[ns]/[se]: ") # 将direction传入movement函数 return movement(1, goldAmount, visited_room, chealth, direction) def movement(current_room, goldAmount, visited_room, chealth, direction): # 不再覆盖传入参数,直接基于当前房间和方向计算新房间 new_room = current_room if direction == "n": new_room = 2 print("前往Room2") elif direction == "ns": new_room = 5 print("前往Room5") elif direction == "s": new_room = 3 print("前往Room3") elif direction == "e": new_room = 4 print("前往Room4") elif direction == "w": new_room = 5 print("前往Room5") elif direction == "se": new_room = 6 print("前往Room6") else: print("无效方向,游戏结束") sys.exit() # 检查生命值是否存活 if chealth <=0: print("你生命值耗尽,游戏结束") sys.exit() return (new_room, goldAmount, visited_room, chealth) def main(): game_over = False print("欢迎来到地牢爬行游戏...") while not game_over: choice = input("主菜单: [p]开始游戏, [i]查看说明, [q]退出: ") print() if choice == "p": gold_amount = START_GOLD current_room = START_ROOM max_health = 10 current_health = max_health player_damage = 1 # 每个房间单独记录访问状态 visited = {1:False, 2:False, 3:False, 4:False, 5:False, 6:False} print(f"当前生命值: {current_health}/{max_health},玩家伤害: {player_damage}") while current_room != FINAL_ROOM: if current_room == 1: current_room, gold_amount, visited[1], current_health = room1(gold_amount, visited[1], current_health) elif current_room == 2: current_room, gold_amount, visited[2], current_health = room2(gold_amount, visited[2], current_health) elif current_room == 3: current_room, gold_amount, visited[3], current_health = room3(gold_amount, visited[3], current_health) elif current_room == 4: current_room, gold_amount, visited[4], current_health = room4(gold_amount, visited[4], current_health) elif current_room == 5: current_room, gold_amount, visited[5], current_health = room5(gold_amount, visited[5], current_health) elif current_room == 6: current_room, gold_amount, visited[6], current_health = room6(gold_amount, visited[6], current_health) else: print(f"错误:房间号{current_room}不存在") sys.exit() if current_health > 0: print(f"你成功逃出地牢,剩余生命值{current_health},持有金币{gold_amount}枚!") else: print("你在地牢中丧生,游戏结束") elif choice == "i": print("在地牢中探险,收集金币,注意躲避暗处的危险") print("最终到达第6个房间即可逃出地牢") print() elif choice == "q": print("再见!") game_over = True else: print("请输入[p], [i], 或 [q]...") print() if __name__ == "__main__": main()
关键修复点说明
- 参数传递修正:movement函数新增
direction参数,由room函数传入用户输入的方向,确保移动逻辑能获取到有效输入 - 变量作用域清理:移除冗余全局变量,改用局部变量传递游戏状态(如main中的
current_room、gold_amount),避免同名变量冲突 - 逻辑判断修复:删除movement函数中无效的
while d < 0循环,改为直接根据方向计算新房间号 - 房间跳转逻辑修正:修正ns方向指向Room5的错误,以及main函数中房间5/6调用错误的问题
- 访问状态优化:用字典统一管理每个房间的访问状态,避免单个变量冗余
内容的提问来源于stack exchange,提问作者User90000000000
相关产品推荐
相关产品推荐

