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)
添加回答
举报