EDIT
I wrote the following, which is around 10x faster than the original one.
Where width
is the width of the map, and height
is the height of the map.
Tiles are appended to the tiles
list first from right to left and then from bottom to up.
for t in tiles:
index = tiles.index(t)
if index > width - 1:
t.surroundings.append(tiles[index - width])
if not index % width == width - 1:
t.surroundings.append(tiles[index - width + 1])
if not index % width == 0:
t.surroundings.append(tiles[index - width - 1])
if not index % width == width - 1:
t.surroundings.append(tiles[index + 1])
if not index % width == 0:
t.surroundings.append(tiles[index - 1])
if index < width * height - width:
t.surroundings.append(tiles[index + width])
if not index % width == width - 1:
t.surroundings.append(tiles[index + width + 1])
if not index % width == 0:
t.surroundings.append(tiles[index + width - 1])
Thanks for help everyone! I would check all the answers accepted if I could.