此处的完整代码:https://drive.google.com/open?id=1iCfj5fLjuD42LLkfhuZoT_7gmXZgHUdk
我正在制作游戏,当我被困在需要从房间中取出物品时将其从房间中取出的时候。
您将键入->,然后选择您要在该房间中拿起的物品。
因此,例如->选择然后->摇滚。
我想要做的是将'rock'添加到库存中,并且仅从播放器当前所在的房间(maplocation[myPlayer.location][EXAMINATION]
)的EXAMINATION键中删除该字符串。
inventory = ['sword', 'map', 'bread', 'enchanted rod', 'water' ]
maplocation = {
'Mountains': {
ZONENAME: 'Mountains',
EXAMINATION: "rock, twig, snow",
SIDE_UP: '',
SIDE_DOWN: 'Vallee',
SIDE_LEFT: '',
SIDE_RIGHT: 'Start',
},
}
def inventory_pick(myAction):
print('What do you want to pick up?')
pickup = input('-> ')
current_room = maplocation[myPlayer.location]
if current_room.get(EXAMINATION, None) == pickup:
inventory.append(pickup)
del current_room[EXAMINATION, pickup]
print(pickup + " has been added to your inventory")
else:
print("There is no " + pickup + " here!" )
答案 0 :(得分:0)
您的代码虽然不太合理。我猜想,如果玩家的pickup
与当前房间的“检查”相同,您想从当前房间中删除“检查”吗?
所以不是
if pickup in maplocation[myPlayer.location][EXAMINATION]:
inventory.append(pickup)
print(pickup + " has been added to your inventory")
for EXAMINATION in maplocation[myPlayer.location][EXAMINATION].value():
del EXAMINATION[pickup]
您应该
current_room = map_locations[my_player.location] # break it up to give
# semantic meaning to your code
if current_room.get(EXAMINATION, None) == pickup:
inventory.append(pickup)
del current_room[EXAMINATION]
# or current_room.pop(EXAMINATION, None)
答案 1 :(得分:0)
我不认为您正在尝试调用EXAMINATION
,因为它只是一个字符串。这就是我根据您提供的代码可以想到的:
def inventory_pick(myAction):
print('What do you want to pick up?')
pickup = input('-> ')
if pickup in maplocation[myPlayer.location][EXAMINATION]:
inventory.append(pickup)
print(pickup + " has been added to your inventory")
del maplocation[myPlayer.location][EXAMINATION][pickup]
else:
print("There is no " + pickup + " here!" )