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 ...

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)

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)
添加回答
舉報