Home > Back-end >  Python text based game - How to show the correct output when player wins or loses game by reaching t
Python text based game - How to show the correct output when player wins or loses game by reaching t

Time:10-13

My code works fine moving between rooms and collecting items but when player gets to the Food court where the villain is without all 6 items in the inventory it should output the player lost, or if player has all item then player wins. See my code below, I added an "if len(Inventory) == and !=" but it isn't working. Any help is appreciated. This is my first programing/scripting course I take so I need this code probably needs a lot more work than what I think but this is what I have so far.

   #enter code here#
#instructions
print('You are at the mall and there is an Alien in the Food Court that you must defeat!')
print("You can only move around the mall by typing 'North', 'South', 'East', or 'West' when asked for your move. Type 'Exit' at any time to leave the game. ")
print("You need to collect 6 items from 6 different stores to defeat the Alien.")
print("If you get to the Alien without all 6 items in your inventory, you die and lose the game.")
print('Good luck!')

rooms = { 'Marshalls': {'name': 'Marshalls', 'East': 'Miscellaneous', 'North': 'Champs'},
    'Miscellaneous': {'name': 'Miscellaneous', 'West': 'Marshalls'},
    'Champs': {'name': 'Champs', 'North': 'Pharmacy', 'South': 'Marshalls', 'East': 'Sports', 'West': 'Food Court'},
    'Sports': {'name': 'Sports', 'North': 'Verizon'},
    'Verizon': {'name': 'Verizon', 'South': 'Sports'},
    'Food Court': {'name': 'Food Court', 'East': 'Champs'},
    'Pharmacy': {'name': 'Pharmacy', 'East': 'Toys', 'South': 'Champs'},
    'Toys': {'name': 'Toys', 'West': 'Pharmacy'},
}

items = {'Miscellaneous': 'Rope',
         'Champs': 'Running shoes',
         'Sports': 'Baseball bat',
         'Verizon': 'Phone',
         'Pharmacy': 'Tranquilizer',
         'Toys': 'Water gun'
}

direction = ['North', 'South', 'East', 'West']      #how player can move
current_room = 'Marshalls'
Inventory = []

#function
def get_new_statement(current_room, direction):
    new_statement = current_room  #declaring current player location
    for i in rooms:  #loop
        if i == current_room:
            if direction in rooms[i]:
                new_statement = rooms[i][direction]  #assigning new direction to move player

    return new_statement  #return

while True:
    print('You are in the', current_room, 'store')
    print('Current inventory:', Inventory)
    print('__________________________')
    direction = input('Type an allowed direction to move to another store? or type Exit to leave game: ' )
    direction = direction
    if (direction == 'exit'):
        print('Goodbye, thanks for playing!')
        exit()
    if (direction == direction):
        new_statement = get_new_statement(current_room, direction)
        if new_statement == current_room:
            print('Move not allowed, try again.')
        else:
            current_room = new_statement
    else:
        print('Invalid entry')

    #ask to collect item in current room
    if current_room in items.keys() and items[current_room] != None and items[current_room] != 'Food Court':
        print('You an item: ', items[current_room])
        option = input('Do you wish to collect it (y/n): ')
        if option[0] == 'y' or option == 'Y':
            Inventory.append(items[current_room])
            items[current_room] = None

    #if player gets to the Food Court then player either win or looses the game
    if current_room in items.keys() and items[current_room] == 'Alien':
        if len(Inventory) == 6:
            print('Congrats you defeated the Alien!!')
        elif len(Inventory) != 6:
            print("You lost")
            print('Goodbye')
        break



  

CodePudding user response:

You need to change the line

if current_room in items.keys() and items[current_room] == 'Alien':

to

if current_room == 'Food Court':

Because current_room isn't in items, so the if statement always goes false. If you put Food Court in items, with alien as its value, then it will ask you if you want to collect it. So you need to just check if the room is Food Court, then it will evaluate to true.

Note: You might want to make it check if direction.lower() is in the room's values, so the user can enter any type of capitalized word and it will move if it can.

  • Related