0
votes

My game map is a 2d-matrix that consists of different tiles (ex. map[y][x] = tile). Each tile has an image, and a rectangle. Currently the map is nearly 1000 tiles in size, and it takes quite some time to blit every one of them to the screen.

My current goal is to find a way to reduce the amount of time it takes to access each item of the matrix, and blit only the necessary tile-objects to the screen. Here is my main obstacle in trying to find a solution:

- Because it is a side-scrolling game, none of the tiles are static (the rectangles are always being adjusted with the player's movement, thus making it mandatory to re-blit the entire screen).

Here is generally how the map functions in the game:

  1. For tile in tile matrix: blit tile to screen
  2. Blit player and NPCs
  3. Update player position
  4. If player moves: adjust all tiles (camera system)

I'm looking for more efficient ideas of doing the same thing. As I said above, blitting every darn tile takes a lot of time, and to add to that, I'm not sure how to selectively blit different tiles when they are constantly changing location. All ideas are welcome. Thank you.

1

1 Answers

1
votes

When you're iterating over your tiles you can do a test to check if the current tile is contained within the camera's view port, if it is you can draw, otherwise you can skip blitting the tile.

for tile in tiles:
  if camera.viewport.contains(tile.rect):
    tile.draw()

The contains method is determining if a rectangle is inside another. You'll also need to use 2 different frames of reference, screen space and world space.