1 回答
TA贡献1846条经验 获得超7个赞
您可以通过 为字典中的键设置值dictionary[key] = value。通过rowand定义一个键col并将其关联到字典:
keys = [WALL, KEY, DOOR, MOVE_INCREASE]
game_dict = {}
with open("game1.txt", "r") as file:
for row, line in enumerate(file):
for col, char in enumerate(line):
if char in keys:
game_dict[(row, col)] = char
如果您有对象的类(Wall,Key,...),那么您可以执行以下操作:
class_dict = { WALL : Wall, KEY : Key, DOOR : Door, MOVE_INCREASE: MoveIncrease}
game_dict = {}
with open("game1.txt", "r") as file:
for row, line in enumerate(file):
for col, char in enumerate(line):
if char in class_dict:
game_dict[(row, col)] = class_dict[char](char)
print(game_dict)
添加回答
举报