基于python文本的游戏未解决的引用错误消息



我正在为一个基于文本的游戏编写代码。我有8个房间,这些房间里有6个物品,除了在辅导员小屋和水晶湖的开始房间,这是最后一个房间,有恶棍。下面是我的代码。除1个大1外,大部分是正确的,没有多少错误。我正在使用pycharm并获得以下错误:未解决的参考"房间"这基本上出现在我代码中的每个房间引用上。我认为这与:

def main():
     rooms = {

在我的代码的顶部。有人能帮我一下吗?

谢谢! !

def instructions():
    print('*******************************************************************************************')
    print('Welcome to: Friday the 13th Text Based Game')
    print('Move Between Rooms, Collect all 6 items')
    print('Once you have all 6 items navigate to Crystal Lake where you will encounter Jason Voorhees')
    print('Use all 6 items to protect the campers and destroy Jason!')
    print('*******************************************************************************************')
    print('To move between rooms, type: go North, go South, go East, or go West')
    print('To add an item to inventory, type: add "item name"')
    print('You may exit at any time by typing: Exit')
    print('*******************************************************************************************')

# move between rooms and print current room
def move_rooms(current_room, directions, rooms):
    current_room = rooms[current_room][directions]
#    new_room = current_room[directions]
    return current_room

# add item from room and remove from list
def add_item(current_room, directions, rooms, inventory):
    inventory.append(rooms[current_room]['item'])
    del rooms[current_room]['item']

# dictionary of rooms and items
def main():
    rooms = {
        'Counselor Cabin': {'East': 'Campers Cabin', 'South': 'Cafeteria'},
        'Campers Cabin': {'West': 'Counselor Cabin', 'item': 'Flashlight'},
        'Cafeteria': {'North': 'Counselor Cabin', 'East': 'First Aid', 'item': 'Water'},
        'First Aid': {'West': 'Cafeteria', 'South': 'Shower Building', 'item': 'Bandages'},
        'Shower Building': {'North': 'First Aid', 'item': 'Matches'},
        'Supply Storage': {'North': 'Cafeteria', 'South': 'Maintenance Garage', 'item': 'Can of Gas'},
        'Maintenance Garage': {'North': 'Supply Storage', 'East': 'Crystal Lake', 'item': 'Axe'},
        'Crystal Lake': {'West': 'Maintenance Garage'},
    }

# create blank inventory list
inventory = []
# and start
current_room = 'Counselor Cabin'
instructions()
# Last room win or end game
while True:
    if current_room == 'Crystal Lake':
        if len(inventory) == 6:
            print('*******************************************************************************************')
            print('You hit Jason with the Axe and he falls down')
            print('*******************************************************************************************')
            print('You then take the gas and pour it all over Jason')
            print('*******************************************************************************************')
            print('You stirke a match and light Jason on fire......he has been destroyed!')
            print('*******************************************************************************************')
            print('Congratulations!!!' 'You have saved all of the campers!')
            print('Thank you for playing, come back soon!')
            print('*******************************************************************************************')
            break
        else:
            print('*******************************************************************************************')
            print('Jason has found you!')
            print('*******************************************************************************************')
            print('You did not collect all of the items to destroy Jason')
            print('*******************************************************************************************')
            print('Jason slays you with his machete and now he is after the campers!')
            print('*******************************************************************************************')
            print('GAME OVER')
            print('*******************************************************************************************')
            break
# print current room & inventory
    print('*******************************************************************************************')
    print('You are in the', current_room)
    print(inventory)
    print('*******************************************************************************************')
    if current_room != 'Crystal Lake' and 'item' in rooms[current_room].keys():
        print('You have found the {}'.format(rooms[current_room]['item']))
    print('*******************************************************************************************')
    directions = input('What direction do you want to go?').title().split()
# move into new room
    if len(directions) >= 2 and move[1] in rooms[current_room].keys():
        current_room = move_rooms(current_room, directions[1], rooms)
        continue
# add to inventory
    elif len(directions[0]) == 3 and directions[0] == 'Add' and ' '.join(directions[1:]) in rooms[current_room]['item']:
        print('You add teh {} to your inventory'.format(rooms[current_room]['item']))
        print('*******************************************************************************************')
        add_inventory(current_room, directions, rooms, inventory)
        break
# invalid and exit commands
    if directions == 'Exit':
        print('You have chosen to exit the game. Jason has found you and has killed you!')
        print('Thank you for playing, Come back soon!')
        break
    else:
        print('Invalid command, try again!')
        continue

main()

我同意@BeRT2me的观点,这是一个缩进问题。现在的问题是,您的while循环运行在main函数之外,rooms是一个变量,仅在该函数内部定义(并在函数退出后清除)。我相信你想要的是:

def instructions():
    print('***************************************************************')
    print('Welcome to: Friday the 13th Text Based Game')
    print('Move Between Rooms, Collect all 6 items')
    print('Once you have all 6 items navigate to Crystal Lake where you will encounter Jason Voorhees')
    print('Use all 6 items to protect the campers and destroy Jason!')
    print('*******************************************************************************************')
    print('To move between rooms, type: go North, go South, go East, or go West')
    print('To add an item to inventory, type: add "item name"')
    print('You may exit at any time by typing: Exit')
    print('*******************************************************************************************')

# move between rooms and print current room
def move_rooms(current_room, directions, rooms):
    current_room = rooms[current_room][directions]
    # new_room = current_room[directions]
    return current_room

# add item from room and remove from list
def add_item(current_room, directions, rooms, inventory):
    inventory.append(rooms[current_room]['item'])
    del rooms[current_room]['item']

# dictionary of rooms and items
def main():
    rooms = {
        'Counselor Cabin': {'East': 'Campers Cabin', 'South': 'Cafeteria'},
        'Campers Cabin': {'West': 'Counselor Cabin', 'item': 'Flashlight'},
        'Cafeteria': {'North': 'Counselor Cabin', 'East': 'First Aid', 'item': 'Water'},
        'First Aid': {'West': 'Cafeteria', 'South': 'Shower Building', 'item': 'Bandages'},
        'Shower Building': {'North': 'First Aid', 'item': 'Matches'},
        'Supply Storage': {'North': 'Cafeteria', 'South': 'Maintenance Garage', 'item': 'Can of Gas'},
        'Maintenance Garage': {'North': 'Supply Storage', 'East': 'Crystal Lake', 'item': 'Axe'},
        'Crystal Lake': {'West': 'Maintenance Garage'},
    }

    # create blank inventory list
    inventory = []
    # and start
    current_room = 'Counselor Cabin'
    instructions()
    # Last room win or end game
    while True:
        if current_room == 'Crystal Lake':
            if len(inventory) == 6:
                print('*******************************************************************************************')
                print('You hit Jason with the Axe and he falls down')
                print('*******************************************************************************************')
                print('You then take the gas and pour it all over Jason')
                print('*******************************************************************************************')
                print('You stirke a match and light Jason on fire......he has been destroyed!')
                print('*******************************************************************************************')
                print('Congratulations!!!' 'You have saved all of the campers!')
                print('Thank you for playing, come back soon!')
                print('*******************************************************************************************')
                break
            else:
                print('*******************************************************************************************')
                print('Jason has found you!')
                print('*******************************************************************************************')
                print('You did not collect all of the items to destroy Jason')
                print('*******************************************************************************************')
                print('Jason slays you with his machete and now he is after the campers!')
                print('*******************************************************************************************')
                print('GAME OVER')
                print('*******************************************************************************************')
                break
        # print current room & inventory
        print('*******************************************************************************************')
        print('You are in the', current_room)
        print(inventory)
        print('*******************************************************************************************')
        if current_room != 'Crystal Lake' and 'item' in rooms[current_room].keys():
            print('You have found the {}'.format(rooms[current_room]['item']))
        print('*******************************************************************************************')
        directions = input('What direction do you want to go? ').title().split()
        # move into new room
        if len(directions) >= 2 and directions[1] in rooms[current_room].keys():
            current_room = move_rooms(current_room, directions[1], rooms)
            continue
        # add to inventory
        elif len(directions[0]) == 3 and directions[0] == 'Add' and ' '.join(directions[1:]) in rooms[current_room]['item']:
            print('You added the {} to your inventory'.format(rooms[current_room]['item']))
            print('*******************************************************************************************')
            add_item(current_room, directions, rooms, inventory)
        # invalid and exit commands
        if directions[0] == 'Exit':
            print('You have chosen to exit the game. Jason has found you and has killed you!')
            print('Thank you for playing, Come back soon!')
            break
        else:
            print('Invalid command, try again!')
            continue

main()

我已经修复了上面的缩进和其他一些小错误。

这个游戏看起来很酷。(我建议在每个房间旁边显示下一个可能的方向。)

def instructions():
    print('*******************************************************************************************')
    print('Welcome to: Friday the 13th Text Based Game')
    print('Move Between Rooms, Collect all 6 items')
    print('Once you have all 6 items navigate to Crystal Lake where you will encounter Jason Voorhees')
    print('Use all 6 items to protect the campers and destroy Jason!')
    print('*******************************************************************************************')
    print('To move between rooms, type: go North, go South, go East, or go West')
    print('To add an item to inventory, type: add "item name"')
    print('You may exit at any time by typing: Exit')
    print('*******************************************************************************************')

# move between rooms and print current room
def move_rooms(current_room, directions, rooms):
    current_room = rooms[current_room][directions]
    return current_room

# add item from room and remove from list
def add_inventory(current_room, rooms, inventory):
    inventory.append(rooms[current_room]['item'])
    del rooms[current_room]['item']

# dictionary of rooms and items
def main():
    rooms = {
        'Counselor Cabin': {'East': 'Campers Cabin', 'South': 'Cafeteria'},
        'Campers Cabin': {'West': 'Counselor Cabin', 'item': 'Flashlight'},
        'Cafeteria': {'North': 'Counselor Cabin', 'East': 'First Aid', 'South': 'Supply Storage', 'item': 'Water'},
        'First Aid': {'West': 'Cafeteria', 'South': 'Shower Building', 'item': 'Bandages'},
        'Shower Building': {'North': 'First Aid', 'item': 'Matches'},
        'Supply Storage': {'North': 'Cafeteria', 'South': 'Maintenance Garage', 'item': 'Gas'},
        'Maintenance Garage': {'North': 'Supply Storage', 'East': 'Crystal Lake', 'item': 'Axe'},
        'Crystal Lake': {'West': 'Maintenance Garage'}
    }

# create blank inventory list
    inventory = []
# and start
    current_room = 'Counselor Cabin'
    instructions()
# Last room win or end game
    while True:
        if current_room == 'Crystal Lake':
            if len(inventory) == 6:
                print(current_room)
                print('You have found Jason at Crystal Lake')
                print('*******************************************************************************************')
                print('You hit Jason with the Axe and he falls down')
                print('*******************************************************************************************')
                print('You then take the gas and pour it all over Jason')
                print('*******************************************************************************************')
                print('You strike a match and light Jason on fire......he has been destroyed!')
                print('*******************************************************************************************')
                print('Congratulations!!!' 'You have saved all of the campers!')
                print('Thank you for playing, come back soon!')
                print('*******************************************************************************************')
                break
            else:
                print('*******************************************************************************************')
                print('Jason has found you!')
                print('*******************************************************************************************')
                print('You did not collect all of the items to destroy Jason')
                print('*******************************************************************************************')
                print('Jason slays you with his machete and now he is after the campers!')
                print('*******************************************************************************************')
                print('GAME OVER')
                print('*******************************************************************************************')
                break
# print current room & inventory
        print('*******************************************************************************************')
        print('You are in the', current_room)
        print(inventory)
        print('*******************************************************************************************')
        if current_room != 'Crystal Lake' and 'item' in rooms[current_room].keys():
            print('You have found the {}'.format(rooms[current_room]['item']))
        print('*******************************************************************************************')
        directions = input('Do you want to add item or go direction?').title().split()
# move into new room
        if len(directions) >= 2 and directions[1] in rooms[current_room].keys():
            current_room = move_rooms(current_room, directions[1], rooms)
            continue
# add to inventory
        elif len(directions[0]) == 3 and directions[0] == 'Add' and ' ' 
                ''.join(directions[1:]) in rooms[current_room]['item']:
            print('You add the {} to your inventory'.format(rooms[current_room]['item']))
            print('*******************************************************************************************')
            add_inventory(current_room, rooms, inventory)
            continue
# exit commands
        if directions[0] == 'Exit':
            print('You have chosen to exit the game. Jason has found you and has killed you!')
            print('Thank you for playing, Come back soon!')
            break
# invalid commands
        else:
            print('Invalid command, try again!')
            continue

main()

最新更新