我正在尝试在文件中搜索 #, M,K, O, D 的实例,并且对于每个实例,在字典中返回 {position(tuple), instance type(#, M,K, O, D)} 。我编写了一个函数来查找文件中实例的坐标,但是我不确定如何使用此信息添加和更新整个字典。这是我到目前为止所拥有的:
def init_game_information(dungeon_name="game1.txt"):
self._dungeon=load_game(dungeon_name)
game_dict={}
with open("game1.txt", "r") as file:
for line in file:
for row, line in enumerate(self._dungeon):
for col, char in enumerate(line):
if WALL in line:
x=WALL
y=get_positions(self, WALL)
dict_WALL={y,x}
game_dict.update(dict_WALL)
if KEY in line:
x=KEY
y=get_positions(self, KEY)
KEY={y,x}
game_dict.update(dict_KEY)
if DOOR in line:
x=DOOR
y=get_positions(self, DOOR)
dict_DOOR={y,x}
game_dict.update(dict_DOOR)
if MOVE_INCREASE in line:
x=MOVE_INCREASE
y=get_positions(self, MOVE_INCREASE)
dict_MOVE={y,x}
game_dict.update(dict_MOVE)
def get_positions(self, entity):
""" Returns a list of tuples containing all positions of a given Entity
type.
Parameters:
entity (str): the id of an entity.
Returns:
)list<tuple<int, int>>): Returns a list of tuples representing the
positions of a given entity id.
"""
positions = []
for row, line in enumerate(self._dungeon):
for col, char in enumerate(line):
if char == entity:
positions.append((row,col))
return positions
这是游戏文件/应返回内容的示例:
喵喵时光机
相关分类