Home > front end >  Python text based game: Invalid moves
Python text based game: Invalid moves

Time:10-16

I am new to coding and Python. I am struggling with why I am getting the keyError: 'item' when I am putting in a invalid direction. If I put any directions in that lead to another room I have no issues with the game and can complete it fully. I can grab every item and beat the boss. It is only when I put a Invalid move in that I error out from the start or after I have retrieved an item and try to put in a Invalid move. Any help will be very much appreciated. Here is the code I have so far:

def show_instructions():
    print()
print("Ghoul Text Adventure Game")
print("Collect 6 items to win the game, or be crushed by Blightcrusher, the Ghoul.")
print("Move commands: go South, go North, go East, go West")
print("Add to Inventory: get 'item name'")

def main():  # Define the game
    pass

# Make dict for list of rooms you can travel, along with items you can pick up.
rooms = {
    'Cave Entrance': {'go South': 'Main Feast Area'},
    'Main Feast Area': {'item': 'Fleeting Soul Boots', 'go North': 'Cave Entrance', 'go West': 'The Deep', 'go South': 'The Spike'},
    'The Deep': {'item': 'Undead Leggings', 'go South': 'The Pottery', 'go East': 'Main Feast Arena'},
    'The Pottery': {'item': 'Shadow Helm','go North': 'The Deep', 'go East': 'The Spike'},
    'The Spike': {'item': 'Phantom chest', 'go North': 'Main Feast Area', 'go West': 'The Pottery', 'go East': 'Foggy Area'},
    'Foggy Area': {'item': 'Legendary Bow', 'go North': 'Graveyard', 'go West': 'The Spike'},
    'Graveyard': {'item': 'Phoenix Down', 'go North': 'SHADOW ROOM', 'go South': 'Foggy Area'},
    'SHADOW ROOM': {'item': 'Blightcrusher', 'go South': 'Graveyard'}
    }
# setting up format to show players current room and items in Inventory
def layout():
print('----------------------------')
print('You are in the {}'.format(current_room))
print('Inventory: '   str(inventory))
if 'item' in rooms[current_room]:
    print('You see '   rooms[current_room]['item'])
print('----------------------------')

# start player at Cave Entrance
current_room = 'Cave Entrance'
inventory = []
directions = ''


while True:
layout()
directions = input('\nWhat is your next move? ')  # prompting for directions
if directions in rooms[current_room]:  # Set so players move go with directions in rooms list
    directions = directions
    current_room = rooms[current_room][directions]
elif directions == str('get '   rooms[current_room]['item']):
    if ['item'] not in inventory:
        print('You retrieved ', rooms[current_room]['item'])
        inventory.append(rooms[current_room]['item'])
        del rooms[current_room]['item']
else:
    print('Invalid')   # Make the rest of the moves invalid
if current_room == 'SHADOW ROOM':
    print('The boss, Blightcrusher Appears!')
    if len(inventory) == 6:
        print('You Crit Headshot Blightcrusher!')
        print('Blightcrusher shatters into pieces, You WIN!')
        print('Thanks for playing!')
    else:
        print('Oh no, Blightcrusher crushes you….GAME OVER!')
        print('Thanks for playing! Hope you enjoyed it!')
    break
main()

CodePudding user response:

The problem is that the 'Cave Entrance' has no item. So, if you go e.g. North in that room, it passes the first if statement. In the next one:

elif directions == str('get ' rooms[current_room]['item']):

an item is required, because you are getting 'item' from the current room.

  • Related