【发布时间】:2020-09-25 10:36:13
【问题描述】:
我正在尝试在文件中搜索#、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
这是游戏文件的示例/应返回的内容:
【问题讨论】:
-
你有
Wal,Door, ...的课程吗?
标签: python python-3.x class dictionary pygame