我目前正在开发一款基于文本的游戏,你可以在房间之间移动,并在房间里拿起物品,直到你到达一个“老板”房间。到目前为止,我已经正确地打印了所有东西,并显示了房间中是否有物品,以及它是什么物品,以及您在哪个房间。
我现在的问题是,它不会根据用户的输入来改变房间。举个例子,你从“深渊”开始。用户输入North应该会将您的位置带到Barracks中。然而,这并不能让我从墓穴中走出来。
我的代码如下:
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()任何帮助都是非常感谢的。
发布于 2021-08-14 17:41:20
只要稍微重组一下,这就行得通了:
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()然而,如果你想要得出它的逻辑结论,你需要认真地考虑将所有的故事情节移动到一个文本文件中,以便在启动时读取。这样,你就可以在其他故事中使用你的“冒险引擎”,而无需重写程序。这就是早期的冒险游戏的工作原理。
发布于 2021-08-14 17:39:05
location = my_dict[location][move] 因为您已经像这样编写了两个与之相等的==
https://stackoverflow.com/questions/68785624
复制相似问题