3
\$\begingroup\$

I've made a script for generating a game world based off of image pixel data.

Trying to make a python map editor

My progress so far has resulted in a program which loads an image and draws sprites in positions correlating with the map, like this:

enter image description here

enter image description here

Now the problem is that even for small levels, I'm noticing a drop in frame rate. The provided example level will make pygame drop from 100 fps to 80 fps. If the map is 40 x 40 the frame rate will be around 17.

This is concerning, for two reasons. The program is not doing anything else than drawing images right now, how bad will the frame rate be when game logic is also being calculated every frame? Second, the aim is to ultimately make a two-player strategy game, in which the levels would be significantly larger than the example, and in that case 20 frames per second is way below acceptable parameters.

Every sprite in the game is a 48x48 pixel png image with some portion of transparency. Everything except rendering the sprites is done once when the game starts. This is the only code being run on every frame:

    def draw(self):
    self.screen.fill((0,0,0))

    for e in self.map_layout.fixed_list:
        self.screen.blit(e.picture, e.rect)

I'm guessing pygame's method for rendering images does not scale very well. Windows task manager shows my CPU utilisation at 2% with a map of size 40x40.

My question is: What can I do to improve frame rate? I don't mind switching from (or complementing) pygame to something else, so long as the transition is not very difficult for a novice programmer like myself.

\$\endgroup\$
3
  • \$\begingroup\$ Are you using a texture atlas or are you using single images? \$\endgroup\$ Jan 11, 2013 at 21:14
  • \$\begingroup\$ @VaughanHilts Single images, but I'm going to implement a spritesheet today. Hopefully it will improve performance. \$\endgroup\$ Jan 12, 2013 at 3:50
  • 1
    \$\begingroup\$ You should defintely do that - texture swapping can be a real killer. You should also look into implementing spritebatching if pygame does not natively support it, as that will also botch your performance! \$\endgroup\$ Jan 12, 2013 at 5:23

1 Answer 1

2
\$\begingroup\$

I was able to significantly improve frame rate by changing the code so that every tile displays a sprite generated from a texture atlas, instead of blitting a separate png to the display surface for every tile.

I based the code on aquasheep's improved spritesheet function, found in this stackoverflow entry.

When previously a 40x40 map would bring the frame rate down from 100 to 17, a map of the same size now has ~90 fps. This is still far from optimal though, as a map of size 127x127 (desired standard size) will have a frame rate of just under 40.

I am still looking for a way to replace pygame for the purpose of rendering, or to make the algorithm at least twice as fast.

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Try this: draw everything once on a huge surface, and then draw only that surface, instead of looping every sprite. \$\endgroup\$
    – szotp
    Jan 12, 2013 at 12:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .