[Python] Why can't I get my text based game code to work right?

Discussão em 'Python' iniciado por Stack, Junho 18, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a text based game code. The opening works just fine, however, I can't get the game to move passed the Foyer. Beyond that, there are a few yellow errors that are popping up, but nothing in red. Please help!

    Here is my current code:

    import os

    # Display starting menu
    def prompt():
    print("Dragon Text Adventure Game")
    print("Collect 6 items to win the game, or be eaten by the Dragon.")
    print("Move Commands: go South, go North, go East, go West")
    print("Add to Inventory: get 'item name'")

    input("Press any key to continue...")

    # Clears screen
    def clear():
    os.system('cls' if os.name == 'nt' else 'clear')


    # Map
    rooms = {
    'Foyer': {'South': 'Dining Room'},
    'Dining Room': {'North': 'Foyer', 'East': 'Hallway1', 'West': 'Hallway2', 'South': 'Hallway3', 'Item': 'Key'},
    'Hallway1': {'West': 'Dining Room', 'East': 'Kitchen'},
    'Kitchen': {'West': 'Hallway', 'Item': 'Bloody Knife'},
    'Hallway2': {'South': 'Ladies Room', 'West': 'Sons Room', 'North': 'Daughters Room'},
    'Ladies Room': {'North': 'Hallway2', 'Item': 'Family Photo'},
    'Sons Room': {'East': 'Hallway2', 'Item': 'Book of Necromancy'},
    'Daughters Room': {'South': 'Hallway2', 'Item': 'Health Potion'},
    'Hallway3': {'South': 'Lords Room'},
    'Lords Quarters': {'South': 'Hallway 4', 'Item': 'Book of Lords mad Ramblings'},
    'Hallway4': {'North': 'Lords Quarters', 'South': 'Room Off Lords Quarters'},
    'Room Off Lords Quarters': {'North': 'Hallway4', 'Boss': 'Dragon'}
    }

    # List to track inventory
    inventory =[]

    # Tracks current room
    current_room = 'Foyer'
    command = ''
    # Result of last room
    msg = ''

    clear()

    prompt()

    # Gameplay loop
    while True:

    clear()

    # Display info player
    print(f'You are in the {current_room}\n Inventory: {inventory}.')

    # Display msg
    print(msg)

    #Item indicator
    if 'Item' in rooms[current_room].keys():
    nearby_item = rooms[current_room]['Item']

    if nearby_item not in inventory:

    print(f'You see a {nearby_item}.')

    # Boss encounter
    if 'Boss' in rooms[current_room].keys():

    # Lose
    if len(inventory) < 6:
    print(f'You lost the fight with {rooms[current_room]["Boss"]}.')
    break

    # Win
    else:
    print(f'You beat {rooms[current_room]["Boss"]}.')
    break

    # Accept player's move as input
    user_input = input('Enter you move:\n')

    next_move = user_input.split(' ')

    action = next_move[0].title()


    if len(next_move) > 1:
    Item = next_move[1:]
    direction = next_move[1].title()

    item = ' '.join(Item).title()


    # Moving between rooms
    if action == 'go South':

    try:
    current_room = rooms[current_room]['South']
    msg = f'You travel {direction}.'

    except:
    msg = f'You cant go that way'

    # Picking up items
    elif action == 'get':

    try:
    if Item == rooms[current_room]['Item']:

    if Item not in inventory:

    inventory.append(rooms[current_room]['Item'])
    msg = f'{Item} retrieved.'

    else:
    msg = f'{Item} already retrieved.'

    except:
    msg = f'Cant find {Item}.'

    # Exit game
    elif action == 'Exit':
    msg = f'Goodbye.'
    break

    # Any other commands invalid
    else:
    msg = f'Invalid command.'


    Because I can't get passed the first part of this game as the input for directions isn't working, I don't know if I can pick up any items, or if the rest of this code even works. So if someone could please check this whole code for me, and help me make sure that it will work, that would be great! Thanks.

    P.S. I am learning Python, so everything would need to be easy for me to input into that program.

    Continue reading...

Compartilhe esta Página