基于文本的游戏代码与玩家输入不匹配

问题描述 投票:0回答:1

我已经失去了耐心,因为我已经在办公桌前坐了 3 个多小时,试图弄清楚为什么我的游戏不会让我离开起始房间,尽管一切看起来都正常。我以各种不同的方式运行和测试了代码,但我完全不知所措,“愤怒”甚至不足以描述我的挫败程度。

这是我的代码:

# Dictionary of rooms, directions of player input, and items
rooms = {
    'Prison Cell': {'west': 'Prison Hallway North', 'item': 'Prison Key'},
    'Prison Hallway North': {'north': 'Storage Room', 'south': 'Prison Hallway South', 'east': 'Prison Cell'},
    'Storage Room': {'south': 'Prison Hallway North', 'item': 'Map'},
    'Prison Hallway South': {'north': 'Prison Hallway North', 'south': 'Meat Locker'},
    'Meat Locker': {'north': 'Prison Hallway South', 'east': 'Kitchen', 'item': 'Sword'},
    'Kitchen': {'west': 'Meat Locker', 'east': 'Bandit Camp Foyer', 'item': 'Disguise'},
    'Bandit Camp Foyer': {'west': 'Kitchen', 'north': 'Hallway', 'east': 'Bandit Barracks', 'item': 'Shield'},
    'Bandit Barracks': {'west': 'Bandit Camp Foyer', 'item': 'Room Key'},
    'Hallway': {'south': 'Bandit Camp Foyer', 'north': 'Mess Hall'},
    'Mess Hall': {'north': 'Main Door', 'east': 'Entryway', 'item': 'Spellbook'},
    'Entryway': {'west': 'Mess Hall', 'north': 'Dichottos Quarters'},
    'Dichottos Quarters': {'south': 'Entryway', 'item': 'Artifact'},
    'Main Door': {'north': 'Exit', 'south': 'Mess Hall'},
    'Exit': {'south': 'Main Door'}
}

# List of kill zones
kill_zones = ["Bandit Barracks"]

# Required equipment
required_equipment = {'Prison Key', 'Map', 'Sword', 'Shield', 'Disguise', 'Room Key', 'Spellbook', 'Artifact'}

# Initialize player's attributes
player = {
    "location": "Prison Cell",
    "inventory": set()
}

# Define a function to get the new state of the player based on the direction chosen
def get_new_state(direction_from_user, current_room):
    if direction_from_user in rooms[current_room]:
        return rooms[current_room][direction_from_user]
    else:
        print("You can't go that way.")
        return current_room

# Define a function to check for game over conditions
def check_game_over(current_room, player):
    if current_room in kill_zones:
        print("You've been caught! Game over!")
        return True
    elif current_room == 'Bandit Barracks' and 'Disguise' not in player["inventory"]:
        print("You entered the Bandit Barracks without a disguise! You've been beaten to a pulp! Game over!")
        return True
    elif current_room == 'Dichottos Quarters' and required_equipment - player["inventory"]:
        missing_items = required_equipment - player["inventory"]
        print(f"You fought Dichotto without the required equipment ({', '.join(missing_items)})! Game over!")
        return True
    elif current_room == 'Exit' and 'Artifact' not in player["inventory"]:
        print("You left the camp without the Artifact! The bandits have long since fled the camp... Game over!")
        return True
    else:
        return False

# Define a function to show game instructions
def show_instructions():
    print('\nWelcome to Dichotomy of Man and Magic!\nYou are on an adventure seeking thrills and adventure to find an ancient artifact.')
    print('\nIn your travels, you have discovered the artifact, only you have been caught by bandits!')
    print('The artifact is now in possession of Dichotto, the leader of these mysterious bandits. They have imprisoned you in their Den')
    print('\nYou are tasked with finding all of your equipment, get the artifact back from Dichotto, and leave with your life intact!\n')

# Define a function to show player's status
def show_status(player, current_room):
    print("Current room:", current_room)

    # Check if the current room has an item to collect
    if current_room in rooms and "item" in rooms[current_room]:
        print("Item in this room:", rooms[current_room]["item"])

    # Display player's inventory
    print("Inventory:", player["inventory"])

    # Display available directions to move
    if current_room in rooms:
        print("Available directions to move:")
        for action in rooms[current_room]:
            print(action)
    else:
        print("No available directions to move.")

# Define the game system driver
def game_driver():
    current_room = player["location"]

    # Main game loop
    while True:
        # Show current status
        show_status(player, current_room)

        # Get the player's choice
        direction = input("Enter direction to move: ").lower()

        # Check if the input is a valid direction
        if direction in rooms[current_room]:
            # Get the new state of the player based on the direction chosen
            player["location"] = get_new_state(direction, current_room)
        else:
            print("Invalid direction. Please choose from available directions.")

        # Check for game over conditions
        if check_game_over(player["location"], player):
            break

# Call the show_instructions function
show_instructions()

# Call the game driver function to start the game
game_driver()

# Check for game over conditions after the loop
if player["location"] == 'Exit' and 'Artifact' in player["inventory"]:

print("Congratulations! You have retrieved the Artifact and left the Bandit's Den. You win!")

现在我在这里尝试做的是,逻辑应该检查我当前的状态,例如库存、位置、我可以采取的可能方向等。然而,当我使用“get”命令来拾取物品时,它有效,但我不断在正确的输出下方收到“无效命令”消息。然后,当我尝试使用“向西”时,因为“向西”是唯一可用的方向,它告诉我不能走那条路,而我需要前进的关键项目是起始房间...

我需要帮助。

python if-statement while-loop text-based
1个回答
0
投票

current_room = player["location"]
仅在游戏开始时调用一次。这就是为什么它始终保持不变。将其移至下方
while True
下:

while True:
    current_room = player["location"]
    show_status(current_room)
© www.soinside.com 2019 - 2024. All rights reserved.