I'm using Python 2.7 on Win7x64 with PyGame
What is the best way to iterate over all the tiles in the game or screen, efficienty? Ideally I have about 800 tiles, but that is a dynamic number I've been playing around with.
The way I've got it is the logic is looking through all the tiles per frame (at 60fps, and I haven't yet got GFX and Logic decoupled):
for tile in TILES: #tiles is a simple list if player_pos is in tile.coords: #coords is a list filled with (x, y) tuples tile.color = RED #a simple attribute change, performance hit whether this line is 'pass' or not
but the list of
TILES is huge enough that looking through 800 tiles, 60x a second obviously isn't efficient.
What is the best way to proceed from here? How do I find the occupied tiles, without iterating over them every frame? I could split the map up into quadrants or something similar, but maybe there's a fancy concept I'm not familiar with that would work better.
edit: Solved, for now: I added an attribute to the Tile class that has the lowest and highest two coordinates and then every frame, the logic looks to see of the player's pos is between the two ranges like so:
for tile in TILEs: #pos is player pos if tile.range_x <= pos <= tile.range_x: #range_x is the lowest and highest # x coordinate within the tile in pixels if tile.range_y <= pos <= tile.range_y: #low and high y here pos_tiles.append(tile)
edit2; I should mention that a tile holds several coordinates, meaning a unit (or player) can move within a tile.