0

I'm developing a simple tile game that displays a grid image and paints it with successive layers of images. So I have-

list_of_image_tiles = { GRASS: pygame.image.load('/grass.png').convert_alpha(), TREES: pygame.image.load('/trees.png').convert_alpha(), etc}

Then later on I blit these-

DISPLAYSURF.blit(list_of_images[lists_of_stuff][TREES], (col*TILESIZE,row*TILESIZE))

DISPLAYSURF.blit(list_of_images[lists_of_stuff][GRASS], (col*TILESIZE,row*TILESIZE))

Note that for brevity I've not included a lot of code but it does basically work- except performance is painfully slow. If I comment out the DISPLAYSURF stuff performance leaps forward, so I think I need a better way to do the DISPLAYSURF stuff, or possibly the pygame.image.load bits (is convert_alpha() the best way, bearing in mind I need the layered-image approach?)

I read something called psycho might help, but not sure how to fit that in. Any ideas how to improve the performance most welcome.

SamChancer
  • 75
  • 5

2 Answers2

2

There are a couple of things you can do.

  1. Perform the "multi-layer" blit just once to a surface then just blit that surface every frame to the DISPLAYSURF.
  2. Identify parts of the screen that need to be updated and use screen.update(rectangle_list) instead of screen.flip().

Edit to add example of 1. Note: you didn't give much of your code, so I just fit this with how I do it.

# build up the level surface once when you enter a level.
level = Surface((LEVEL_WIDTH * TILESIZE, LEVEL_HIGHT * TILESIZE))
for row in range(LEVEL_HIGHT):
    for col in range(LEVEL_WIDTH):
        level.blit(list_of_images[lists_of_stuff][TREES], (col * TILESIZE, row * TILESIZE))
        level.blit(list_of_images[lists_of_stuff][GRASS], (col * TILESIZE, row * TILESIZE))

then in main loop during draw part

# blit only the part of the level that should be on the screen
# view is a Rect describing what tiles should be viewable
disp = DISPLAYSURF..get_rect()
level_area = Rect((view.left * TILESIZE, view.top * TILESIZE), disp.size)
DISPLAYSURF.blit(level, disp, area = level_area) 
cmd
  • 5,754
  • 16
  • 30
  • Thanks for the suggestion, can you show me roughly what these two approaches might look like syntax wise in terms of my code? – SamChancer Oct 24 '16 at 18:28
  • added example of 1. For number 2, it totally depends on how you know what to draw and often requires you to build your game returning update `Rect` so that you can build your list to pass to `screen.update()`. – cmd Oct 24 '16 at 20:52
  • Thanks a lot, I can use the example with what I've built so far, a great help. – SamChancer Oct 25 '16 at 10:33
0

You should use colorkey whenever you dont need per pixel alpha. I just changed all convert_alphas in my code to simple convert and set color key for fully opaque parts of image. Performance increase TEN FOLD!

Amila Senadheera
  • 12,229
  • 15
  • 27
  • 43
R3qUi3M
  • 65
  • 4