为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕仙森 2022-07-26 16:53:41
我正在使用 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'上面的代码不起作用,因为我不知道如何执行地图移动。
查看完整描述

3 回答

?
忽然笑

TA贡献1806条经验 获得超5个赞

您可以将所有房间组织在 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 ...


查看完整回答
反对 回复 2022-07-26
?
守着星空守着你

TA贡献1799条经验 获得超8个赞

我认为最好的选择是其他类的商店房间的分离逻辑,例如:


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)


查看完整回答
反对 回复 2022-07-26
?
www说

TA贡献1775条经验 获得超8个赞

你真的做不到


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)


查看完整回答
反对 回复 2022-07-26
  • 3 回答
  • 0 关注
  • 112 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号