【问题标题】:How can I access a Class instance with a certain Class attribute value?如何访问具有特定 Class 属性值的 Class 实例?
【发布时间】:2020-02-11 18:08:03
【问题描述】:

我正在使用 Python 制作基于文本的 RPG,并尝试设置地图和玩家移动。但是,我还没有弄清楚如何“链接”房间,以便玩家可以使用诸如北、南等命令无缝地从一个房间移动到另一个房间。

我正在使用一个名为“Room”的类,它具有 x 坐标和 y 坐标属性。我已经制作了地图中每个“瓦片”的实例,具有某些 x-pos 和 y-pos 值。我要做的是根据当前的 x-pos 或 y-pos 转到“Room”类的当前实例。但是,我不知道如何根据属性值找到类实例。

下面是我的代码。任何帮助将不胜感激。

class Room:
    def __init__(self,name,info,xpos,ypos,exits):
        self.instances.append(self)
        self.name = name
        self.info = info
        self.xpos = xpos
        self.ypos = ypos
        self.exits = exits

作为参考,room.exits 是您可以从房间的哪些部分退出。这可能只有 's' 和 'n',表示东面和西面都有墙。

为了进一步参考,在下面的代码中,cloc 代表当前位置。我目前将其设置为:

intro_room = Room("Living Room of House", "You are in a dusty living room, in a stranger's house. You don't know how you got here. It's hard to see and your hands are tied", 100, 100, ['s','n'])
cloc = intro_room

另一个sn-p代码:

def inp():
    basic = input(">")
    if basic ==  'i':
        print(" ")
        print("This is what is in your current inventory", inventory)
    elif basic == 'h':
        print(" ")
        ### use this when programming actual game
        print("Available commands:\n-move\n-take\n-look\n-talk\n-use\n-enter")
    elif basic == 'm':
        print(" ")
        print(current_location)
    elif basic == 'description':
        print(cloc.name, cloc.info)
    elif basic == 'n':
        cloc = Room.get(ypos==ypos+1) ###try to access instance with certain instance attribute value???
        #etc. for 's', 'e', 'w'

上面的代码不起作用,因为我不知道如何执行地图移动。

【问题讨论】:

  • @usr2564301 它是您可以离开的房间的边缘,例如南边或北边。我希望这样如果他们的选择不在 room.exits 中,你就不能从那里退出。感谢您要求澄清。

标签: python python-3.x class inheritance


【解决方案1】:

您可以将所有房间组织在 dict 中,其中键是 2 元组 (xpos, ypos)。然后,根据您的exits,您可以退出到不同的房间。

all_rooms = {
   (0, 0): Room(..., xpos=0, ypos=0, ...),
   (0, 1): Room(..., xpos=0, ypos=1, ...),
   ...
}

def inp():
    ...
    elif basic == "move":
        direction = input(f"Which direction? Your options are {cloc.exits}. \n>")
        if direction in cloc.exits:
            # determine new coordinate set based on direction
            new_loc = {
                'n': (cloc.xpos, cloc.ypos + 1),
                's': (cloc.xpos, cloc.ypos - 1),
                'e': (cloc.xpos + 1, cloc.ypos),
                'w': (cloc.xpos - 1, cloc.ypos),
            }[direction]
            # change current location to the new room
            cloc = all_rooms[new_loc]
        else:
            print("You can't move in that direction.")
    elif ...

【讨论】:

    【解决方案2】:

    我认为最好的选择是其他类的 store Rooms 的分离逻辑,例如:

    class Room:
        def __init__(self,name,info,xpos,ypos,exits):
            self.name = name
            self.info = info
            self.xpos = xpos
            self.ypos = ypos
            self.exits = exits
    
    class RoomCollection:
        def __init__(self):
            self.rooms = []
    
        def add(self, room):
            self.rooms.append(room)
    
        def find_by_xpos(self, xpos):
            for room in self.rooms:
                if(xpos == room.xpos):
                    return room
            return None
    
    intro_room = Room("Living Room of House", "You are in a dusty living room, in a stranger's house. You don't know how you got here. It's hard to see and your hands are tied", 100, 100, ['s','n'])
    
    all_rooms = RoomCollection()
    all_rooms.add(intro_room)
    
    room = all_rooms.find_by_xpos(100)
    
    print(room.name)
    

    【讨论】:

      【解决方案3】:

      你真的做不到

      cloc = Room.get(ypos==ypos+1)
      

      您应该创建单独的方法来获取和设置类属性,如下所示:

      def getY(self):
          return self.ypos
      
      def setY(self, ypos):
          self.ypos = ypos
      
      #do the same for xpos
      

      所以

      cloc = Room.get(ypos==ypos+1)
      

      变成

      currentY = cloc.getY()
      cloc.setY(currentY += 1)
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-06-19
        • 1970-01-01
        • 2021-05-24
        • 2021-12-05
        • 2015-02-16
        • 1970-01-01
        • 1970-01-01
        • 2020-11-21
        相关资源
        最近更新 更多