从字典数组合并项抓取

2024-05-19 16:34:58 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我为一个文本游戏编写的代码,我在游戏中设置了dict在房间之间导航。用户负责收集的每个房间中都有一个项目,我正在寻找如何从这些阵列中收集/存储项目的指导

def nextroom(currentloc, direction):
    rooms = {
        'Bedroom': {'North': 'Parents Office', 'South': 'Living-room', 'East': 'Kitchen', 'West': 'Bathroom', 'Item': 'Invalid'},
        'Parents Office': {'North': 'Invalid','South': 'Bedroom', 'East': 'Dads Man Cave', 'West': 'Invalid', 'Item': 'Golf Club'},
        'Bathroom': {'North': 'invalid', 'South': 'Invalid', 'East': 'Bedroom', 'West': 'Invalid', 'Item': 'Hand Soap'},
        'Living-room': {'North': 'Bedroom', 'South': 'Invalid', 'East': 'Dining-room', 'West': 'Invalid', 'Item': 'Nerf Gun'},
        'Dining-room': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid',  'West': 'Living-room', 'Item': 'Bucket of Marbles'},
        'Kitchen': {'North': 'Garage', 'South': 'Invalid', 'East': 'Invalid',   'West': 'Bedroom', 'Item': 'Handcuffs' },
        'Garage': {'North': 'invalid', 'South': 'Kitchen', 'East': 'Invalid', 'West': 'Invalid', 'Item': 'Rc Car'},
        'Dads Man Cave': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Parents Office', 'Item': 'Infiltrator'}
}
    next = rooms[currentloc][direction]
    if next.lower() == 'invalid':
        print("You can't go that direction!")
        return currentloc
    else:
        return next


def player_stat(location):
    rooms = {
        'Bedroom': {'North': 'Parents Office', 'South': 'Living-room', 'East': 'Kitchen', 'West': 'Bathroom','Item': 'Invalid'},
        'Parents Office': {'North': 'Invalid', 'South': 'Bedroom', 'East': 'Dads Man Cave', 'West': 'Invalid','Item': 'Golf Club'},
        'Bathroom': {'North': 'invalid', 'South': 'Invalid', 'East': 'Bedroom', 'West': 'Invalid', 'Item': 'Hand Soap'},
        'Living-room': {'North': 'Bedroom', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Invalid', 'Item': 'Nerf Gun'},
        'Dining-room': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Living-room', 'Item': 'Bucket of Marbles'},
        'Kitchen': {'North': 'Garage', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Bedroom', 'Item': 'Handcuffs'},
        'Garage': {'North': 'invalid', 'South': 'Kitchen', 'East': 'Invalid', 'West': 'Invalid', 'Item': 'Rc Car'},
        'Dads Man Cave': {'North': 'invalid', 'South': 'Invalid', 'East': 'Invalid', 'West': 'Parents Office', 'Item': 'Infiltrator'}
}

    print('----------------------------')
    print('You are in {} with {}'.format(location, rooms[location]['Item']))
    print('----------------------------')



def play():
     currentRoom = 'Bedroom'
     while currentRoom != 'Exit' or 'exit':
         player_move = input('Enter your move:\n')
         if player_move == 'Exit' or player_move == 'exit':
            currentRoom = 'Exit'
            print('Play again soon!')
            break
         elif player_move == 'South' or player_move == 'south':
            currentRoom = nextroom(currentRoom, 'South')
            player_stat(currentRoom)
         elif player_move == 'North' or player_move == 'north':
            currentRoom = nextroom(currentRoom, 'North')
            player_stat(currentRoom)
         elif player_move == 'West' or player_move == 'west':
            currentRoom = nextroom(currentRoom, 'West')
            player_stat(currentRoom)
         elif player_move == 'East' or player_move == 'east':
            currentRoom = nextroom(currentRoom, 'East')
            player_stat(currentRoom)




player_move = ''
intro()
instructions()
new_game = input('Do you want to play a game? (Yes or No): ')

if new_game == 'no' or new_game == 'No':
    print('Have a good day!')
elif new_game == 'yes' or new_game == 'Yes':
    play()

我知道我必须在玩家统计中加入以下代码,以显示收集的物品的运行总数。。但我们正在寻找关于如何

ItemsCollected.append(rooms[location]['Item'])

多谢各位


Tags: ormoveitemroomplayerofficewestsouth