我正在参加编码课程并制作一款非常著名的基于文本的游戏,我尝试了各种方法来尝试让这个游戏运行,但没有任何变化可以运行 当输入换房间命令时;它说无效命令。 因此,在我能够做到这一点之前,我无法进一步测试任何东西。我让它工作了,然后我添加了一个更复杂的字典,所有的东西都消失了。 我不知道我是否需要回到更简单的字典和更简单的标识符,或者我是否可以简单地找到一种方法来完成这项工作。
这是完整的代码:
areas_dic = {
'Home': {'name': 'Home', 'go East': 'Plains', 'item': 'none'},
'Plains': {'name': 'Plains', 'go North': 'Rocky Terrain', 'go East': 'Fields', 'go South': 'Stream', 'item': 'Grains'},
'Stream': {'name': 'Stream', 'go East': 'Forest', 'item': 'Water'},
'Forest': {'name': 'Forest', 'go West': 'Stream', 'item': 'Firewood'},
'Fields': {'name': 'Fields', 'go West': 'Plains', 'go North': 'Farmlands', 'item': 'Vegetables'},
'Rocky Terrain': {'name': 'Rocky Terrain', 'go South': 'Plains', 'go East': 'up the Mountain', 'item': 'Medicinal Flowers'},
'up the Mountain': {'name': 'up the Mountain', 'go West': 'Rocky Terrain', 'go East': 'Mountain Top', 'item': 'Warmer Clothes'},
'Farmlands': {'name': 'Farmlands', 'go South': 'Fields', 'go West': 'DYSENTERY', 'item': 'Jerky'},
'Mountain Top': {'name': 'Mountain Top', 'go West': 'up the Mountain', 'go South': 'DYSENTERY', 'item': 'Milk'}
}
print('Welcome to Old Americas')
print('Object of the game is to collect all 7 items before falling ill')
print('You can type go North, go South, go East, go West to move')
current_location = 'Home'
inventory = \[\]
required_items = {'Grains', 'Water', 'Firewood', 'Vegetables', 'Medicinal Flowers', 'Warmer Clothes', 'Milk'}
while True:
print(f"You are in the {areas_dic\[current_location\]\['name'\]}.")
# Display available directions
directions = [direction for direction in areas_dic[current_location] if direction.startswith('go ')]
print("Available directions:", ", ".join(directions))
# Get player input
command = input("Enter your command): ").strip().lower()
# Process player input
> if command in directions:
> direction = command.split()[1]
> if direction in areas_dic[current_location]:
> current_location = areas_dic[current_location][direction]
item = areas_dic[current_location].get('item', None)
if item:
print(f"You found {item}!")
inventory.append(item)
if item in required_items:
required_items.remove(item)
print(f"Items left to collect: {', '.join(required_items)}")
else:
print("You can't go that way!")
else:
print("Invalid command. Try again.")
# Check if player has collected all items
if not required_items:
print("Congratulations! You've collected all items. Now face the villain in DYSENTERY!")
break
# Check if player has reached the final room
if current_location == 'DYSENTERY':
if 'illness' in inventory:
print("You have fallen ill. Game over!")
else:
print("You defeated the illness and survived the journey! Well done!")
break
块引用是我遇到问题的地方。或者我想我是,我不知道我是否没有正确格式化它,或者它是否需要一些不同的东西。
正如约翰·戈登(John Gordon)在评论中所说,
.lower()
似乎是搞乱代码的原因。幸运的是,这应该是一个非常简单的修复。
这是我为解决您的问题所做的:
#...
command = input("Enter your command): ").strip().lower()
# Checks each given direction
for direction in directions:
enter code here
# using .lower here means that no matter how we type go x it will always work.
if command in direction.lower():
direction = command.split()[1]
# ...
if item:
print(f"You found {item}!")
inventory.append(item)
# ...
# We add a 'break' every time we want it to stop at the other directions.
# This isn't strictly neccessary, but it helps save some processing power, even if it
# pretty much non-existant.
break
# ...
# This is added to the end so that if it goes through all of the directions
# and can't find one, it'll return the message "Invalid Command"
else:
print("Invalid command. Try again.")
我没有仔细检查所有代码,比如每个“中断”的位置,但希望您能明白。这是文字解释,而不是评论:
for循环将检查每个方向。如果找不到,它将转到最后的 else 语句,并返回 Invalid Command 消息。通过在方向和命令中添加 .lower,我们可以做到这样,即使您输入“gO EaSt”或类似的内容,它也会起作用。每当我们希望程序返回到“command = ...”行时,我们都可以添加break语句。