我正在使用 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'
上面的代码不起作用,因为我不知道如何执行地图移动。
忽然笑
守着星空守着你
www说
相关分类