debugging 问题与文字为基础的游戏我创造的做法,第二次进入房间时的错误

bn31dyow  于 2023-08-06  发布在  其他
关注(0)|答案(1)|浏览(109)

问题就归结为这个。在我的文字游戏中,在你进入训练场之前,你必须从你的库存中给予饥饿的守卫面包。目前为止,这是可行的。库存更新以显示面包不再在库存中。沿着面包从库存中删除,它也从称为房间的字典中删除。
一切正常,除了当你再次进入训练场后,它会再次要求你的面包,这已经不存在了。
我的问题是如何写出while、if、elif等语句:

if "Bread" not in rooms["Kitchen"]["Item"]:
    continue

字符串
或者类似的东西,这样你就可以重新进入房间,而不会在以后的时间点上要求面包。我只写了几个星期的代码,所以我知道我的代码可能是垃圾,但我正在使用这个项目来学习。我面临的问题是,如果你没有给警卫的面包,代码需要不允许你进入,允许你给予警卫面包,然后在你给警卫面包后,像一个普通的房间一样,离开,然后再回到那个房间。
下面是代码部分。

if current_room == "Training Grounds" and "Bread" not in Inventory:
        print("A hungry guard blocks your path and shoves you back into the Great Hall.")
        current_room = "Great Hall"
        continue

    if current_room == "Training Grounds" and "Bread" in Inventory:
        player_move = input("Would you like to offer the guard bread in exchange for access to the Training Grounds? ")
        if player_move == "yes".capitalize():
            current_room = "Training Grounds"
        print("The guard lets you enter the Training Grounds.")
        Inventory.remove("Bread")
        del rooms["Kitchen"]["Item"]
        continue
    elif player_move == "no".capitalize():
        print('"Get the hell out of here!"')
        print("The guard shoves you back into the Great Hall.")
        current_room = "Great Hall"
        continue


我想这里的某个地方是我需要排队检查的地方,然后让你进入房间。或者我的方法完全错了。任何帮助都是非常感激的。
我尝试了各种各样的解决方案。用不同的方式来表达,甚至尝试使用像first_time_in_room = True这样的东西,但是,我真的不知道这些是如何工作的。

daupos2t

daupos2t1#

沿着面包从库存中删除,它也从称为房间的字典中删除。
当 * 从库存中删除面包时,它不会是合乎逻辑的,它会从字典中删除,称为房间 *,而是当面包从厨房进入库存时。
我的问题是如何写出while、if、elif等语句:if "Bread" not in rooms["Kitchen"]["Item"]: continue或类似的东西,这样你就可以重新进入房间,而不会在以后的某个时候要求面包。
面包是否在厨房里并不十分相关;关键是面包是否给了警卫。
尝试使用first_time_in_room = True之类的东西
如果你已经初始化了它,并且在给守卫面包时将它设置为False,你可以这样写:

if current_room == "Training Grounds" and "Bread" not in Inventory:
      if first_time_in_room:
        print("A hungry guard blocks your path and shoves you back into the Great Hall.")
        …

字符串

相关问题