亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何訪問具有特定 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 關注
  • 129 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號