简体   繁体   中英

Need help printint the 'item' when the player enters the room, updating the Inventory list and lose game

Below is my code I can move between rooms effectively. However, when I'm in a room that has no east command, it repeats and doesn't give me a try again. When I type exit, I get an error. And I for the

I also can't figure out how to update my Inventory list. So, it displays all the item the player has got so far. And as the player enters the room. I want to print the 'item' in the currentRoom. Can't seem to figure it out. I suppose I'm missing something simple.

The last thing I need help figuring out is how to make it to where if the player enters the villian room without every 'item'. Then the player lose's the game.

Below this is an example of what should print out for the player.

    Tea Shop Quest Game
    Collect 6 items to win the game or be consumed by Emma's Rage!!
    Move commands: go South, go North, go East, go West
    Add to Inventory: get 'item name'
     ---------------------------
    You are in The Counter
    You see a "insert Item here"
    Where do you want to go? 
    >

def show_instructions():
    # print a main menu and the commands
    print('Welcome to the Drink Quest!')  # Welcomes player to the game
    print("Collect 6 items to win the game, or be eaten by the dragon.")
    print("Move commands: go North, go East, go West, go South, exit")  # Informs the player of the commands of the game
    print("Add to Inventory: get 'item name'")


def currentStatus(currentRoom, currentInventory):
    # print current status
    print(' ---------------------------')
    print('You are in ' + currentRoom)
    print('You see ' + 'insert Roomitem')
    print('Your current inventory is:')
    print(currentInventory)


# A dictionary for the simplified dragon text game
# The dictionary links a room to other rooms and list the items in that room .
def main():
    Rooms = {
        'Upper Room1': {'west': 'Upstairs', 'item': 'Lift Off'},
        'Upstairs': {'south': 'The Counter', 'east': 'Upper Room1', 'item': 'Aloe/Tea'},
        'Side Room': {'east': 'The Counter', 'item': 'Cup/Lid'},
        'The Counter': {'south': 'Backroom', 'west': 'Side Room', 'east': 'Storage Room', 'north': 'Upstairs'},
        'Storage Room': {'west': 'The Counter', 'north': 'Hidden Storage', 'item': 'Flavors'},
        'Hidden Storage': {'south': 'Storage Room', 'items': 'Ice'},
        'Backroom': {'north': 'The Counter', 'east': 'The Villain', 'item': 'Straw'},
        'The Villain': {'west': 'Bedroom'}
    }
# I need to print the item to the currentStatus when the player enters the room.
    # Start the player in the first room
    Inventory = []
    currentRoom = 'The Counter'
    show_instructions()

    # loop forever
    while True:
        if currentRoom.lower() == 'exit':
            break
        # print current status
        currentStatus(currentRoom, Inventory)

        # get action from user
        print("Where do you want to go? ")
        navigate = input('>')

        if navigate.lower() == 'exit':
            currentRoom = 'exit'
            continue

        # Navigation
        navigate = navigate.split(' ', 1)
        command = navigate[0].lower()
        direction = navigate[1].lower()

        if command == 'go':
            if direction in Rooms[currentRoom]:
                currentRoom = Rooms[currentRoom][direction]
            else:
                print('You can\'t go that way!')
        elif command == 'get':
            if direction not in Inventory and 'item' in Rooms[currentRoom] and direction == Rooms[currentRoom]['item'].lower():
                Inventory.append(Rooms[currentRoom]['item'].lower())
                print('item' + "Added to Inventory!")
            else:
                print("You can't collect", direction)
        else:
            print("Invalid Choice choose a different path!")

    print("Thanks for playing Drink Quest!!")


main()

It seems your function that does that should be given access to the Rooms:

Update:

Some rooms don't have an 'item' in them, so that needs to be checked:

def currentStatus(Rooms, currentRoom, currentInventory):
    # print current status
    print(' ---------------------------')
    print('You are in ' + currentRoom)
    room = Rooms[currentRoom]
    if 'item' in room:
        print('You see ' + room['item'])
    print('Your current inventory is:')
    print(currentInventory)

and this should be called like this:

# print current status
currentStatus(Rooms, currentRoom, Inventory)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM