不根据字典更改房间



我目前正在制作一款基于文本的游戏,你可以在房间之间移动并在房间中拾取道具,直到你到达一个"boss"房间。到目前为止,我已经正确地打印了所有内容,并让它显示房间里是否有物品,它是什么物品,以及你在哪个房间。

我目前的问题是它不是根据用户输入来改变房间。例如,你从坟墓开始。用户输入"北方"应该会把你的位置带到兵营。然而,它不能把我从坟墓中移开。

我的代码如下:
def intro():
print("You've finally awoken, my fallen Lord. Your kingdom, once great, is now simply ruins.")
print("To restore yourself to your once grand ability, you must collect the Souls of the previous Lords.")
print("You can navigate your decrepit domain by entering: North, South, East and West.")
print("To collect the Souls, you can enter: collect (soul name).")
print("-" * 30, "n")
def main():
while True:
location = 'Sepulture'
inventory = []
my_dict = {
'Sepulture': {'North': 'Barracks', 'West': 'Grand Entrance', 'East': 'Chapel', 'South': 'Ruined Town'},
'Barracks': {'East': 'War Room', 'South': 'Sepulture', 'soul': 'Soul of a Warrior Lord'},
'War Room': {'West': 'Barracks', 'soul': 'Soul of a Dragon Lord'},
'Chapel': {'East': 'Sepulture', 'soul': 'Soul of Light'},
'Grand Entrance': {'North': 'Great Tower', 'West': 'Sepulture', 'soul': 'Soul of a Lord of Men'},
'Great Tower': {'South': 'Grand Entrance', 'soul': 'Soul of a Lord of Lords'},
'Ruined Town': {'North': 'Sepulture', 'East': 'Chamber of the Lord of Cinder', 'soul': 'Soul of the Pygmy'},
'Chamber of the Lord of Cinder': {'West': 'Ruined Town', 'soul': 'Lord of Cinder'}
}
print("You currently reside in the", location + '.')
print("You currently possess the following: ", *inventory)
if location in my_dict and location != 'Sepulture':
print("My Lord, you've found the {}.".format(my_dict[location]['soul']))
print("What would you like to do, my Lord?")
move = input()
if move in my_dict[location]:
location == my_dict[location][move]
intro()
main()

感谢您的帮助。

只需稍微重组一下,就可以了:

def intro():
print("You've finally awoken, my fallen Lord. Your kingdom, once great, is now simply ruins.")
print("To restore yourself to your once grand ability, you must collect the Souls of the previous Lords.")
print("You can navigate your decrepit domain by entering: North, South, East and West.")
print("To collect the Souls, you can enter: collect (soul name).")
print("-" * 30, "n")
my_dict = {
'Sepulture': {'North': 'Barracks', 'West': 'Grand Entrance', 'East': 'Chapel', 'South': 'Ruined Town'},
'Barracks': {'East': 'War Room', 'South': 'Sepulture', 'soul': 'Soul of a Warrior Lord'},
'War Room': {'West': 'Barracks', 'soul': 'Soul of a Dragon Lord'},
'Chapel': {'East': 'Sepulture', 'soul': 'Soul of Light'},
'Grand Entrance': {'North': 'Great Tower', 'West': 'Sepulture', 'soul': 'Soul of a Lord of Men'},
'Great Tower': {'South': 'Grand Entrance', 'soul': 'Soul of a Lord of Lords'},
'Ruined Town': {'North': 'Sepulture', 'East': 'Chamber of the Lord of Cinder', 'soul': 'Soul of the Pygmy'},
'Chamber of the Lord of Cinder': {'West': 'Ruined Town', 'soul': 'Lord of Cinder'}
}
def main():
location = 'Sepulture'
inventory = []
while True:
print("You currently reside in the", location + '.')
print("You currently possess the following: ", *inventory)
if location in my_dict and location != 'Sepulture':
print("My Lord, you've found the {}.".format(my_dict[location]['soul']))
print("What would you like to do, my Lord?")
move = input()
if move in my_dict[location]:
location = my_dict[location][move]
intro()
main()

然而,如果你要得出合乎逻辑的结论,你需要认真考虑将所有的故事情节移动到一个文本文件中,在启动时读取。这样,你就可以使用你的"冒险引擎"了。在不重写程序的情况下。这就是早期冒险游戏的运作方式。

location = my_dict[location][move] 

因为你在这里写了两个等于这样的==

相关内容

  • 没有找到相关文章

最新更新