0

我正在用 python 构建一个文本冒险,我想让我的物品影响游戏中的东西,例如,通道太暗而不能走下去,除非玩家在他们的库存中拿着一盏灯。

已经根据我的代码编写此代码的最佳方法是什么?

---这是我的房间和通往连通房间的路线---

rooms = ["hallEnt", "hallMid", "snowRoom", "giantNature", "strangeWall", "riverBank"]
roomDirections = {
    "hallEnt":{"e":"hallMid"},
    "hallMid":{"s":"snowRoom", "e":"giantNature", "w":"hallEnt"},
    "snowRoom":{"n":"hallMid"},
    "giantNature":{"s":"strangeWall", "e":"riverBank", "w":"hallMid"},
    "strangeWall":{"s":"hallOuter", "e":"riverBank", "n":"giantNature"},
    "riverBank":{"e":"lilyOne", "w":"giantNature"},
    "lilyOne":{"e":"lilyTwo", "w":"riverBank", "n":"riverBank", "s":"riverBank"},
    "lilyTwo":{"e":"riverBank", "w":"lilyThree", "n":"riverBank", "s":"riverBank"},
    "lilyThree":{"e":"riverBank", "w":"lilyFour", "n":"riverBank", "s":"riverBank"},
    "lilyFour":{"e":"riverBank", "w":"treasureRoom", "n":"riverBank", "s":"riverBank"},
    "treasureRoom":{"w":"hallEnt"},

---这是我的物品和它们的房间位置。---

roomItems = {
    "hallEnt":["snowboots"],
    "snowRoom":["lamp"],
    "treasureRoom":["treasure"],
    }

我的查询的另一个示例,我不希望玩家能够通过 (e)ast 从“hallMid”到“giantNature”,除非他们在他们的 invItems 中持有“灯”。

4

2 回答 2

1

以下示例在您被允许进入房间时返回一个空列表。或创建缺失项目列表。

roomRequirements = { "giantNature" : ["lamp"], "snowRoom" : ["snowboots"] }

inventory = [ "lamp" ]

def changeroom (room):
    missing = []
    if room in roomRequirements.keys():
        for item in roomRequirements[room]:
            if item not in inventory:
                missing.append(item)

    print (missing)

changeroom("hallEnt")
changeroom("giantNature")
changeroom("snowRoom")
于 2014-11-18T07:58:23.053 回答
0

您可能想考虑使用每次进入房间时运行的嵌套 if语句,尤其是在效果是被动的情况下。假设您使用 1 或 0 存储项目的值,您可以这样做:

lamp = 0
rooms = ["hallEnt", "hallMid", "snowRoom", "giantNature", "strangeWall", "riverBank"]
room = rooms[0]

if lamp == 1:
        connected_room = ["hallMid", "snowRoom"]
        print "You are currently inside the " + room + "."
        print "You can see " + connected_room[0] + " and " + connected_room[1] + " in the distance."
else:
        connected_room = ["snowRoom"]
        print "You are currently inside the " + room + "."
        print "You can see the " + connected_room[0] +  " in the distance."
于 2014-11-18T08:10:16.940 回答