首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >不会根据dict来更换房间

不会根据dict来更换房间
EN

Stack Overflow用户
提问于 2021-08-14 17:34:37
回答 2查看 47关注 0票数 0

我目前正在开发一款基于文本的游戏,你可以在房间之间移动,并在房间里拿起物品,直到你到达一个“老板”房间。到目前为止,我已经正确地打印了所有东西,并显示了房间中是否有物品,以及它是什么物品,以及您在哪个房间。

我现在的问题是,它不会根据用户的输入来改变房间。举个例子,你从“深渊”开始。用户输入North应该会将您的位置带到Barracks中。然而,这并不能让我从墓穴中走出来。

我的代码如下:

代码语言:javascript
复制
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()

任何帮助都是非常感谢的。

EN

回答 2

Stack Overflow用户

发布于 2021-08-14 17:41:20

只要稍微重组一下,这就行得通了:

代码语言:javascript
复制
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()

然而,如果你想要得出它的逻辑结论,你需要认真地考虑将所有的故事情节移动到一个文本文件中,以便在启动时读取。这样,你就可以在其他故事中使用你的“冒险引擎”,而无需重写程序。这就是早期的冒险游戏的工作原理。

票数 2
EN

Stack Overflow用户

发布于 2021-08-14 17:39:05

代码语言:javascript
复制
location = my_dict[location][move] 

因为您已经像这样编写了两个与之相等的==

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68785624

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档