Pyton 文本基础游戏,无法让它拾取物品

问题描述 投票:0回答:1

这就是我目前所拥有的

房间={ '大厅': {'北': '卧室', '西': '客厅', '南': '餐厅', '东': '温室','项目': '无'}, '卧室': {'南': '大厅', '东': '图书馆', '物品': '项链'}, '图书馆': {'西': '卧室', '南': '大厅','物品': '法术书'}, 'Parlor': {'East': '大厅', 'South': '餐厅','Item': '木桩'}, '餐厅': {'东': '厨房', '北': '大厅', '物品': '蜡烛'}, '厨房': {'西': '餐厅','项目': '火柴'}, 'Conservatory': {'West': '大礼堂', 'North': '台球室','Item": '圣水'}, '台球室': {'南': '温室','物品': '恶魔'}, }

user_instruction = '欢迎来到游戏,收集所有物品即可获胜。移动类型:goNorth、goEast、goWest、goSouth、exit' 移动方向 = ['向北', '向南', '向东', '向西', '退出']

打印(用户指令) current_position_room = '大厅'

def get_item(current_position_room): 如果“项目”在 current_position_room 中: print("你在这里找到一个 " + current_position_room['Item']) inventory.add(current_position_room.pop('Item'))

其他: return '这个房间没有物品!'

而真实:

if current_position_room == 'Conservatory':
    print('Congratulations! You have reached the Conservatory and won the game!')
    break

print('You are in the {}.'.format(current_position_room))

user_command_of_move = input('\nEnter the moves command to move between the rooms or exit the game: ')

if user_command_of_move in direction_of_moves:
    user_command_of_move = user_command_of_move.replace("go", "")
    if user_command_of_move in rooms[current_position_room].keys():
        current_position_room = rooms[current_position_room][user_command_of_move]
        print(current_position_room)
    else:
     print('Invalid move!')

elif user_command_of_move == 'exit':
    print('Thanks for playing!')
    break
else:
    print('Invalid input')

def get_item(current_position_room): 如果“项目”在 current_position_room 中: print("你在这里找到一个 " + current_position_room['Item']) inventory.add(current_position_room.pop('Item'))

else:
    return 'This room has no item!'

如有任何帮助,我们将不胜感激 谢谢

python items
1个回答
0
投票

您没有在任何地方调用您的

get_item
函数。

并且您还希望在

inventory = set()
之前声明
while True
并将其传递给
get_item
,否则
inventory.add(current_position_room.pop('Item'))
将失败。

所以你需要:

def get_item(current_position_room, inventory):
   # Every room has 'Item' key, so the "if 'Item' in current_position_room" is always true, instead check if `Item` key has a `None` value
   if current_position_room['Item'] != 'None':
       print("You find here a " + current_position_room['Item']) 
       inventory.add(current_position_room.pop('Item'))
       return inventory
   else:
       print("This room has no item!")
       return inventory

inventory = set()
while True:
   if current_position_room == 'Conservatory':
      print('Congratulations! You have reached the Conservatory and won the game!')
      break

   print('You are in the {}.'.format(current_position_room))

   user_command_of_move = input('\nEnter the moves command to move between the rooms or exit the game: ')

   if user_command_of_move in direction_of_moves:
      user_command_of_move = user_command_of_move.replace("go", "")
      if user_command_of_move in rooms[current_position_room].keys():
         current_position_room = rooms[current_position_room][user_command_of_move]
         print(current_position_room)
         inventory = get_item(current_position_room, inventory) # Call get_item here to pick up new item and return it to your inventory - up to you?

# Other code...

PS:很难阅读你的问题在这个问题中的格式(我可能会错过一些东西)。

© www.soinside.com 2019 - 2024. All rights reserved.