如何访问具有特定 Class 属性值的 Class 实例?

我正在使用 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

另一段代码:


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'

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


慕仙森
浏览 113回答 3
3回答

忽然笑

您可以将所有房间组织在 adict中,其中键是 2-tuple (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 ...

守着星空守着你

我认为最好的选择是其他类的商店房间的分离逻辑,例如:class Room:    def __init__(self,name,info,xpos,ypos,exits):        self.name = name        self.info = info        self.xpos = xpos        self.ypos = ypos        self.exits = exitsclass 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 Noneintro_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)

www说

你真的做不到cloc = Room.get(ypos==ypos+1)您应该创建单独的方法来获取和设置类属性,如下所示:def getY(self):    return self.yposdef setY(self, ypos):    self.ypos = ypos#do the same for xpos所以cloc = Room.get(ypos==ypos+1)变成currentY = cloc.getY()cloc.setY(currentY += 1)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python