如何在文本冒险中编码项目交互

时间:2014-11-18 05:41:12

标签: python text-based

我正在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"],
    }

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

2 个答案:

答案 0 :(得分: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")

答案 1 :(得分: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."