在课堂上创建列表的更好方法是什么?

时间:2016-06-27 22:36:39

标签: python list variables global

我一直致力于让我的基于文本的游戏更加逼真,我想要实现的一个设计就是确保房间保持静止状态。到某一点(即玩家在房间内使用药水,如果他们回到那个房间,那药水就不应该在那里了。)

这就是我的代码基本上是如何设置的(我一直在使用"学习Python的艰难之路"由Zed Shaw编写,所以我的代码以相同的方式设置):

class Scene(object):

    def enter(self):
        print "This scene is not yet configured. Subclass it and implement enter()."

class Room(Scene):
    potions = []
    for i in range(3):
        potions.append(Potion())

    def enter(self):
        ...

当我运行时,我得到一个NameError: global name 'potions' is not defined。我知道我可以解决以下两种方法之一:1。使魔药成为一个全局变量,但是我必须为每个包含药水的房间制作一个新的清单(共有36个房间,设置为6x6网格)要么 2.将这行代码放入enter函数,但这会导致每次用户进入房间时列表重置为3个药水。

potions = []
for i in range(3):
    potions.append(Potion())

如果没有其他办法,我想为所有包含药水的房间宣布一个新变量(仅有5个)。但我的问题是,如果有另一种方法可以使这项工作成为一个全球性的。 谢谢你的帮助!

1 个答案:

答案 0 :(得分:4)

首先,让我们看看你的例子(我将简化它):

class Room(Scene):
   potions = [Potion() for x in range(3)]

您所做的是创建一个类属性potions,它们在 Room的所有实例之间共享。例如,你会在我的每个房间看到我的魔药都是相同的魔药实例(十六进制数是相同的!)。如果我在一个实例中修改potions列表,它会修改所有Room个实例中的相同列表:

>>> room1.potions
[<__main__.Potion instance at 0x7f63552cfb00>, <__main__.Potion instance at 0x7f63552cfb48>, <__main__.Potion instance at 0x7f63552cfb90>]
>>> room2.potions
[<__main__.Potion instance at 0x7f63552cfb00>, <__main__.Potion instance at 0x7f63552cfb48>, <__main__.Potion instance at 0x7f63552cfb90>]
>>> 

听起来您希望potions成为Room的每个实例的唯一属性。

您将在某个地方实例化一个房间,例如room = Room()。您需要为Room编写构造函数,以便自定义您的实例:

class Room(Scene):
    def __init__(self): # your constructor, self refers to the Room instance.
        self.potions = [Potion() for x in range(3)]

现在,当您创建房间实例时,它将包含3个药水。

您现在需要考虑如何让角色在入口之间保持房间实例。这将是整个游戏中持续存在的某种变量。

这种对象组合的想法将延伸到你的游戏中。也许你有一个Dungeon班,有36个房间:

class Dungeon(object):
    def __init__(self):
        self.rooms = [[Room() for x in range(6)] for x in range(6)]

或许你的房间最多有四扇门,你可以将它们连接成可能不那么方形的东西:

class Room(Scene):
    def __init__(self, north_room, east_room, south_room, west_room):
        self.north_door = north_room
        self.east_door = east_room
        [... and so on ...]
        # Note: You could pass `None` for doors that don't exist.

甚至更具创造性,

class Room(Scene):
    def __init__(self, connecting_rooms): # connecting_rooms is a dict
        self.connecting_rooms = connecting_rooms

除了两个例子都会为连接房间带来鸡蛋和鸡蛋问题,所以最好添加一个方法来添加每个房间连接:

class Room(Scene):
    def __init__(self):
        self.rooms = {}
        # ... initialize your potions ...
    def connect_room(self, description, room):
        self.rooms[description] = room

然后你可以这样做:

room = Room()
room.connect_room("rusty metal door", room1)
room.connect_room("wooden red door", room2)
room.connect_room("large hole in the wall", room3)

那么也许你的地牢看起来像这样:

class Dungeon(Scene):
    def __init__(self, initial_room):
        self.entrance = initial_room

现在最后,你只需要在游戏期间抓住dungeon Dungeon rslt = (df.assign(Consecutive=df.Value .groupby((df.Value != df.Value.shift()) .cumsum()) .transform('size')) .query('Consecutive > 1') .groupby('Consecutive') .agg({'No':{'No':'first'}, 'Date': {'BeginDate':'first', 'EndDate':'last'}}) .reset_index() ) rslt.columns = [t[1] if t[1] else t[0] for t in rslt.columns] 个实例。

顺便说一下,&#34;房间&#34;由&#34;路径&#34;连接被称为Graph