Linkzx28
Linkzx28

Reputation: 29

I can't move anywhere in my text based game

I'm make this game for my final project and it won't let me move for some reason.

This is what I have so far:

def ShowInstructions():
    # print the main menu and commands
    print("Fire Demon Hunt")
    print(
        "Collect all 6 Dragon Items items across the Castle in order to take on the Fire Demon, or Die by his hand.")
    print("Move commands: go South, go North, go East, go West")
    print("Add to Inventory: get 'item name'")


def Main():
        rooms = {#A dictionary for the simplified text game that links a room to other rooms.
            'Courtyard': {'Name': 'Courtyard', 'North': 'Armory', 'South': 'Kitchen', 'East': 'Hall', 'West': 'Foyer'},
            'Kitchen': {'Name': 'Kitchen', 'item': 'Dragon Wings', 'North': 'Courtyard', 'East': 'Storage'},
            'Storage': {'Name': 'Storage', 'item': 'Dragon Blade', 'West': 'Kitchen'},
            'Hall': {'Name': 'Hall', 'item': 'Dragon Shield', 'West': 'Courtyard', 'North': 'Bedroom'},
            'Bedroom': {'Name': 'Bedroom', 'item': 'Angels Kiss', 'South': 'Hall'},
            'Foyer': {'Name': 'Foyer', 'item': 'Book of Light', 'East': 'Courtyard'},
            'Armory': {'Name': 'Armory', 'item': 'Dragon Helm', 'South': 'Courtyard', 'West': 'Secret Room'},
            'Secret Room': {'Name': 'Secret Room', 'East': 'Armory'}
        }

    directions = ['north', 'south', 'east', 'west']
    current_room = 'Courtyard'
    Inventory = []
    ShowInstructions()

    while True:
        if current_room == 'Secret Room':
            if len(Inventory) == 6:
                print('Congratulations! You have reached the Secret Room and defeated the Fire Demon!')
                break
            else:
                print('You have reached the Secret Room but did not find all of the Items the Fire Demon laughs as he tears you to shreds.')
                print('You Lose!')
                break
        # displays the players current location
        print()
        print('You are in the {}.'.format(current_room))
        print('Inventory', Inventory)
        room_dict = rooms[current_room]
        if "item" in room_dict:
            item = room_dict["item"]
            if item not in Inventory:
               print("You look around and see a", item)


        # gets the users input
        command = input('What do you wish to do? ').split() # this controls the movement
        #movement
        if command[0] == 'go':
            if command[1] in directions:
                room_dict = rooms[current_room]
                if command[1] in room_dict:
                    current_room =room_dict[command[1]]
                else:
                    # bad movement
                    print('You run into the wall.')
            else:
                print("Invalid entry")

        # quit game
        elif command[0] in ['exit', 'quit']:
            print('Thanks for playing!')
            break
        # get item
        elif command[0] == 'get':
            if command[1] == item:
                Inventory.append(item)
                print(item, "collected")
            else:
                print('Invalid command')
        # bad command
        else:
            print('Invalid input')


Main()

Upvotes: 1

Views: 117

Answers (1)

Alexander
Alexander

Reputation: 17335

The problem was with your strings. You were comparing "north" to "North" and "east" to "East" which was coming back false and causing you to keep hitting the wall. I fixed it so that it works now.

The code does still have one bug though. When the player comes upon the "Book of Light" if you tried to grab it then it will also fail because I title case all the input and title case "Book of Light" is "Book Of Light". There are multiple ways of solving this, the most obvious being change the value in the dictionary so that it is title case, but there are other workarounds you might want to consider. I will leave that up to you.

def ShowInstructions():
# print the main menu and commands
    print("Fire Demon Hunt")
    print(
    "Collect all 6 Dragon Items items across the Castle in order to take on the Fire Demon, or Die by his hand.")
    print("Move commands: go South, go North, go East, go West")
    print("Add to Inventory: get 'item name'")


def Main():
    rooms = {#A dictionary for the simplified text game that links a room to other rooms.
        'Courtyard': {'Name': 'Courtyard', 'North': 'Armory', 'South': 'Kitchen', 'East': 'Hall', 'West': 'Foyer'},
        'Kitchen': {'Name': 'Kitchen', 'item': 'Dragon Wings', 'North': 'Courtyard', 'East': 'Storage'},
        'Storage': {'Name': 'Storage', 'item': 'Dragon Blade', 'West': 'Kitchen'},
        'Hall': {'Name': 'Hall', 'item': 'Dragon Shield', 'West': 'Courtyard', 'North': 'Bedroom'},
        'Bedroom': {'Name': 'Bedroom', 'item': 'Angels Kiss', 'South': 'Hall'},
        'Foyer': {'Name': 'Foyer', 'item': 'Book of Light', 'East': 'Courtyard'},
        'Armory': {'Name': 'Armory', 'item': 'Dragon Helm', 'South': 'Courtyard', 'West': 'Secret Room'},
        'Secret Room': {'Name': 'Secret Room', 'East': 'Armory'}
    }

    directions = ['north', 'south', 'east', 'west']
    current_room = 'Courtyard'
    Inventory = []
    ShowInstructions()

    while True:
        if current_room == 'Secret Room':
            if len(Inventory) == 6:
                print('Congratulations! You have reached the Secret Room and defeated the Fire Demon!')
                break
            else:
                print('You have reached the Secret Room but did not find all of the Items the Fire Demon laughs as he tears you to shreds.')
                print('You Lose!')
                break
        # displays the players current location
        print()
        print('You are in the {}.'.format(current_room))
        print('Inventory', Inventory)
        room_dict = rooms[current_room]
        if "item" in room_dict:
            item = room_dict["item"]
            if item not in Inventory:
                print("You look around and see a", item)


        # gets the users input
        command = input('What do you wish to do? ').lower().split()
        #movement

        if command[0] == 'go':
            if command[1] in directions:
                direction = command[1].title()
                room_dict = rooms[current_room]
                if direction in room_dict:
                    current_room =room_dict[direction]
                else:
                    # bad movement
                    print('You run into the wall.')
            else:
                print("Invalid entry")

        # quit game
        elif command[0] in ['exit', 'quit']:
            print('Thanks for playing!')
            break
        # get item
        elif command[0] == 'get':
            name = ' '.join(command[1:]).title()
            print(name)
            if name == item:
                Inventory.append(item)
                print(item, "collected")
            else:
                print('Invalid command')
        # bad command
        else:
            print('Invalid input')

Main()

Output:

Fire Demon Hunt
Collect all 6 Dragon Items items across the Castle in order to take on the Fire Demon, or Die by his hand.
Move commands: go South, go North, go East, go West
Add to Inventory: get 'item name'

You are in the Courtyard.
Inventory []
What do you wish to do? go south

You are in the Kitchen.
Inventory []
You look around and see a Dragon Wings
What do you wish to do? get Dragon Wings
Dragon Wings
Dragon Wings collected

You are in the Kitchen.
Inventory ['Dragon Wings']
What do you wish to do? go east

You are in the Storage.
Inventory ['Dragon Wings']
You look around and see a Dragon Blade
What do you wish to do? get Dragon Blade
Dragon Blade
Dragon Blade collected

You are in the Storage.
Inventory ['Dragon Wings', 'Dragon Blade']
What do you wish to do? go north
You run into the wall.

You are in the Storage.
Inventory ['Dragon Wings', 'Dragon Blade']
What do you wish to do? go west

You are in the Kitchen.
Inventory ['Dragon Wings', 'Dragon Blade']
What do you wish to do? go north

You are in the Courtyard.
Inventory ['Dragon Wings', 'Dragon Blade']
What do you wish to do? go north

You are in the Armory.
Inventory ['Dragon Wings', 'Dragon Blade']
You look around and see a Dragon Helm
What do you wish to do? get dragon helm
Dragon Helm
Dragon Helm collected

You are in the Armory.
Inventory ['Dragon Wings', 'Dragon Blade', 'Dragon Helm']
What do you wish to do?

Upvotes: 2

Related Questions