所以我有一个项目要开发一个基于文本的Python冒险游戏,有多个房间、要收集的物品以及获胜/失败的条件。我已经完成了大部分工作,但我无法触发获胜条件,也无法弄清楚如何设置失败条件。
class Item:
def __init__(self, name, description):
self.name = name
self.description = description
class Room:
def __init__(self, description, exits, items):
self.description = description
self.exits = exits
self.items = items
rooms = {
'town_road': Room("You are on the town road. There is a square to the north, a house to the east, and a side street to the west.", {'north': 'square', 'east': 'house_1', 'west': 'side_street'}, []),
'house_1': Room("You are in a house with a villager. The Fire Stone is on an alter. The Town Road is behind you to the west.", {'west': 'town_road'}, [Item('fire stone', 'A stone of pure Fire Magic')]),
'side_street': Room('You walk onto the side street. To the east runs the Town Road, north of you is a house, a shop to the south and the west street to the west.', {'east': 'town_road', 'north': 'house_2', 'south': 'shop', 'west': 'west_street'}, [Item('wind stone', 'A stone of pure Wind Magic')]),
'house_2': Room("You enter a house with a villager. The Ice Stone is on the table. The Side Street is out the door to the south.", {'south': 'side_street'}, [Item('ice stone', 'A stone of pure Ice Magic')]),
'shop': Room("You enter the shop and meet the shopkeeper. He holds the Wind Stone. The side street is behind you to the north.", {'north': 'side_street'}, [Item('wind stone', 'A stone of pure Wind Magic')]),
'west_street': Room("You walk onto the next street over. There is the side street to the east, an additional road to your north, a blacksmiths shop to your south and a house to the west.", {'east': 'side_street', 'south': 'blacksmiths', 'west': 'house_3', 'north': "north_street"}, []),
'house_3': Room('You enter a house with a villager. The Water Stone is on a counter. The West Street is behind you to the east.', {'east': 'west_street'}, [Item('water stone', 'A stone of pure Water Magic')]),
'blacksmiths': Room('You enter a blacksmiths forge, where the blacksmith is holding out the Earth Stone. The west street is behind you to the north.', {'north': 'west_street'}, [Item('earth stone', 'A Stone of pure Earth Magic')]),
'north_street': Room('You walk onto the north street, where there is a single house to the west and the square to the east.', {'west': 'house_4', 'east': 'square'}, []),
'house_4': Room('You enter another house, where a child waits with the Lightning Stone. The north street is behind you to the east.', {'east': 'north_street'}, [Item('lightning stone', 'A stone of pure Lightning Magic')]),
'square': Room("You enter the square with the demon. Have you collected all the elemental stones?", {'south': 'town_road'}, [Item('key', 'A small key')]),
}
current_room = rooms['town_road']
inventory = []
required_items = ['fire stone', 'ice stone', 'water stone', 'earth stone', 'wind_stone', 'lightning_stone']
def win_condition(inventory, required_items):
for items in required_items:
if items not in inventory:
return False
return True
if __name__ == '__main__':
while True:
print(current_room.description)
print(inventory)
print(required_items)
if win_condition(inventory, required_items):
print('Congratulations! You have collected all the stones and won the game!')
break
command = input('> ').lower().strip()
if command == 'quit':
print('Thanks for playing!')
break
elif command == 'go north':
if 'north' in current_room.exits:
current_room = rooms[current_room.exits['north']]
else:
print("You can't go that way.")
elif command == 'go south':
if 'south' in current_room.exits:
current_room = rooms[current_room.exits['south']]
else:
print("You can't go that way.")
elif command == 'go east':
if 'east' in current_room.exits:
current_room = rooms[current_room.exits['east']]
else:
print("You can't go that way")
elif command == 'go west':
if 'west' in current_room.exits:
current_room = rooms[current_room.exits['west']]
else:
print("You can't go that way")
elif command.startswith('pick up'):
item_name = command[8:].strip()
for item in current_room.items:
if item.name == item_name:
inventory.append(item)
current_room.items.remove(item)
print(f"You picked up the {item.name}.")
break
else:
print("There is nothing here by that name.")
elif command == 'inventory':
print("You are carrying:")
for item in inventory:
print(f"- {item.name}")
else:
print('Invalid command. Try going north, south, east, or west, picking up an item, or checking your inventory.')
我尝试了多种方法来使获胜条件发挥作用。但是,它没有记录我拥有所需的所有物品,而且我不知道如何将其绑定到特定房间。任何有关我做错了什么的帮助和解释将不胜感激。
如果您将
Room
和 Item
类更改为 dataclasses,您将能够看到这里出了什么问题。
<__main__.Item object at 0x00000263E77B80D0>
这是您的项目类的对象表示。这是因为你这样做
if item.name == item_name:
inventory.append(item)
所以
inventory
的类型是list[Item]
。但 required_items
的类型是 list[str]
。
因此,当您迭代
required_items
时,您将获得字符串。但随后您要检查它们是否在库存中,但它们当然不在库存中,因为只有 Item
对象在库存中。
所以你需要将获胜条件更改为类似的
def win_condition(inventory, required_items):
item_names = [i.name for i in inventory]
for items in required_items:
if items not in item_names:
return False
return True
另外,正如其他人提到的,您需要您的项目名称与您在
required_items
中的名称实际匹配。
其他一些提示:
def win_condition(inventory: list[Item], required_items: list[str]) -> bool
- 即使只是运行类型检查器也会捕获该错误。