【问题标题】:Creating a dictionary for specific positions of characters in a file为文件中字符的特定位置创建字典
【发布时间】: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


【解决方案1】:

您可以通过dictionary[key] = value 为字典中的键设置值。通过rowcol 定义一个键并将其关联到字典:

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

如果您有对象的类(WallKey、...),那么您可以执行以下操作:

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)

【讨论】:

  • 谢谢!无论如何,该函数是否可以跳过一个实例,其中有一个空白/什么都没有,并且不向字典中添加任何内容,因为我遇到的错误是“AssertionError:None 不是 的实例”
  • @kit01 我很困惑。你从哪里得到这个错误。一般来说,您只需测试if key != None:。但我不知道你在哪里得到这个错误,你的关键是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-11
  • 1970-01-01
  • 2014-03-19
  • 2021-06-22
  • 2021-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多