Python文本类游戏代码故障求助:启动即判定获胜
问题描述
我正在为脚本入门课程制作一款文本类游戏,作为编程新手已完成部分代码,但遇到逻辑故障:期望玩家收集齐6件物品时无论身处哪个房间都结束游戏,或进入Sorcerer's Lair(巫师巢穴)但未收集齐全部物品时结束游戏。但当前游戏一启动就判定已收集全部物品并显示获胜信息,请求帮忙排查问题。
故障原因分析
- 冗余代码直接触发胜利:代码最开头的
collected_items变量及对应的while循环完全多余,程序启动后会立即执行该循环——自动将collected_items累加6次,随后触发胜利判断并打印获胜信息,根本不会进入真正的游戏主逻辑(main函数)。 - 胜利条件逻辑不符合需求:原
main函数中仅当玩家进入巫师巢穴且物品全收集时才判定胜利,没有实现“收集齐物品后无论在哪都结束游戏”的要求。 - 缺少主函数调用:原代码没有执行
main()的语句,即使去掉冗余代码,游戏主逻辑也无法启动。
修复方案
- 删除开头冗余的
collected_items变量及循环代码。 - 修改
main函数中的结束判断逻辑:先检查是否收集全所有物品,满足则直接触发胜利;再检查是否进入巫师巢穴但未收集全物品,触发失败。 - 新增
main()调用语句,启动游戏主逻辑。
修复后的完整代码
# Define a dictionary to represent the layout of the rooms and their connections rooms = { 'Entrance Hall': {'North': 'Kitchen', 'South': 'Secret Passageway', 'East': 'Throne Room', 'West': 'Grand Hall'}, 'Kitchen': {'South': 'Entrance Hall', 'East': 'Dining Room'}, 'Throne Room': {'West': 'Entrance Hall', 'North': 'Armory'}, 'Armory': {'South': 'Throne Room'}, 'Grand Hall': {'East': 'Entrance Hall'}, 'Dining Room': {'West': 'Kitchen'}, 'Secret Passageway': {'North': 'Entrance Hall', 'East': "Sorcerer's Lair"}, "Sorcerer's Lair": {'West': 'Secret Passageway'} } # Define a dictionary to represent the items in each room items = { 'Grand Hall': 'Golden Chalice', 'Throne Room': 'Silver Scepter', 'Armory': 'Emerald Necklace', 'Kitchen': 'Diamond Crown', 'Dining Room': 'Enchanted Orb', 'Secret Passageway': 'Mystic Amulet' } # Define a function to display the player's status def display_status(current_room, inventory): print("Current Room:", current_room) print("Inventory:", inventory) if current_room in items: print("Item in Room:", items[current_room]) # Define the main function for gameplay def main(): print("Welcome to The Lost Kingdom Text Adventure Game") print("Collect all 6 items to defeat the Wicked Sorcerer and win the game, or endure the Wicked Sorcerer’s Spell of Death") print("Move commands: go South, go North, go East, go West") print("Add to Inventory: get 'item name'") print("************************************") # Print line of asterisks current_room = 'Entrance Hall' inventory = [] while True: display_status(current_room, inventory) print("************************************") # Print line of asterisks command = input("Enter your command: ") if command.startswith("go"): direction = command.split()[1] if direction in rooms[current_room]: current_room = rooms[current_room][direction] else: print("Invalid direction!") elif command.startswith("get"): item = ' '.join(command.split()[1:]) # Collect item name even if it has multiple words if current_room in items and items[current_room] == item: inventory.append(item) del items[current_room] # Remove item from room once picked up print("Item collected:", item) else: print("Item not available in this room!") else: print("Invalid command!") # 先判断是否收集全所有物品,满足则直接胜利 if len(items) == 0: print("Congratulations! You have collected all items and defeated the Wicked Sorcerer!") break # 再判断是否进入巫师巢穴且未收集全物品,触发失败 elif current_room == "Sorcerer's Lair": print("The Wicked Sorcerer cast the Spell of Death on you!...GAME OVER!") break print("Thanks for playing the game. Hope you enjoyed the game!") # 调用main函数启动游戏 main()
内容的提问来源于stack exchange,提问作者angiemarie1
相关产品推荐
相关产品推荐

