简体   繁体   中英

Text-based inventory system, dictionary access

I've run into a brick wall trying to figure out this inventory system in a text based adventure game I'm making.

The issue I'm having is developing a method to move an item from the room inventory to the player's inventory, and I suppose on the way I've created some key errors. I've tried to rectify the code by creating the inventory independent of the dictionary, but it seems to be more practical to use than not.

Here's my code so far:

def ShowInstructions():
    #print the main menu and commands
    print("College Text Adventure Game")
    print("Collect all 6 of your missing school items across campus in order to take notes in class and get an A, or fail the class.")
    print("Move commands: go South, go North, go East, go West")
    print("Add to Inventory: get 'item name'")

def Main():
    rooms = { # Creating a dictionary for the rooms, linking them to one another according to the layout
        'Dorm Room' : { 'name' : 'Dorm Room', 'item' : '', 'east' : 'Common Room'},
        'Common Room' : { 'name' : 'Common Room', 'item' : 'backpack', 'east' : 'Campus Store', 'west' : 'Dorm Room', 'north' : 'Library', 'south' : 'Book Store' },
        'Library' : { 'name' : 'Library', 'item' : 'Notepad', 'south' : 'Common Room', 'east' : 'Study Room'},
        'Study Room' : { 'name' : 'Study Room', 'item' : 'pen', 'west' : 'Library'},
        'Campus Store' : { 'name' : 'Campus Store', 'item' : 'laptop', 'west' : 'Common Room', 'north' : 'Coffee Shop'},
        'Coffee Shop' : { 'name' : 'Coffee Shop', 'item' : 'coffee',  'south' : 'Campus Store'},
        'Book Store' : { 'name' : 'Book Store', 'item' : 'textbook', 'north' : 'Common Room', 'east' : 'Lecture Hall'},
        'Lecture Hall' : { 'name' : 'Lecture Hall', 'item' : '', 'east' : 'Book Store'}
    }

    directions = ['north', 'south', 'east', 'west']
    current_room = rooms['Dorm Room']
    Inventory = []

    while True: # Gameplay Loop
        if current_room == rooms['Lecture Hall'] and len(Inventory) > 5:
            print('You have passed the class by taking good notes and gotten an A. Congratulations!')
        elif current_room == rooms['Lecture Hall'] and len(Inventory) < 6:
            print('You got to class, but forgot one or more of your required items. You fail to take good grades and fail the class.')
            print('You lose!')
            break
        # display current location
        print('You are in the {}.'.format(current_room['name']))
        print('Inventory:',Inventory)
        print('Room Inventory: {}'.format(current_room['item']))

        # get user input
        command = input( "What do you do? \n" )
        command.split()
        # movement
        if command[0] in directions:
            if command[0] in current_room:
                current_room = rooms[current_room[command[0]]]
            else:
                # bad movement
                print('You cannot go that way.')
        # quit game
        elif command[0] == ['exit', 'quit']:
            current_room = 'exit'
            print('Thanks for playing!')
            break
        #get item
        elif command[0] == 'get':
            Inventory.append(rooms['item'])
        # bad command
        else:
            print('Invalid input')

ShowInstructions()
Main()

I got some help from a friend that's more adept in Python than I, and we fixed a lot of the methods that I was using incorrectly, as well as some syntax or dictionary errors.

def ShowInstructions():
    # print the main menu and commands
    print("College Text Adventure Game")
    print(
        "Collect all 6 of your missing school items across campus in order to take notes in class and get an A, or fail the class.")
    print("Move commands: go South, go North, go East, go West")
    print("Add to Inventory: get 'item name'")


def Main():
    rooms = {  # Creating a dictionary for the rooms, linking them to one another according to the layout
        'Dorm Room': {'name': 'Dorm Room', 'east': 'Common Room'},
        'Common Room': {'name': 'Common Room', 'item': 'backpack', 'east': 'Campus Store', 'west': 'Dorm Room',
                        'north': 'Library', 'south': 'Book Store'},
        'Library': {'name': 'Library', 'item': 'Notepad', 'south': 'Common Room', 'east': 'Study Room'},
        'Study Room': {'name': 'Study Room', 'item': 'pen', 'west': 'Library'},
        'Campus Store': {'name': 'Campus Store', 'item': 'laptop', 'west': 'Common Room', 'north': 'Coffee Shop'},
        'Coffee Shop': {'name': 'Coffee Shop', 'item': 'coffee', 'south': 'Campus Store'},
        'Book Store': {'name': 'Book Store', 'item': 'textbook', 'north': 'Common Room', 'east': 'Lecture Hall'},
        'Lecture Hall': {'name': 'Lecture Hall', 'east': 'Book Store'}
    }

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

    while True:  # Gameplay Loop
        if current_room == 'Lecture Hall':
            if len(Inventory) == 6:
                print('You have passed the class by taking good notes and gotten an A. Congratulations!')
                break
            else:
                print(
                    'You got to class, but forgot one or more of your required items. You fail to take good grades and fail the class.')
                print('You lose!')
                break
        # display 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 see a", item)

        # get user input
        command = input("What do you do? ").split()
        # 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 cannot go that way.')
            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()

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