Sep-02-2022, 03:15 AM
You could use dict.get(key, value). This returns a special value (default is None) when the dictionary lookup fails. Here I make a list of all possible near tiles, then remove all missing tiles.
def find_local(self): x = self.rect.centerx // self.game.level.tilesize y = self.rect.centery // self.game.level.tilesize tiles = self.game.level.tile_dict return [t for t in [tiles.get((i, j)) for i in range(x-3, x+3) for j in range(y-3, y+3)] if t]