[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Paralax snow flaks: Some tipps for enhancing the performance?



Ludwig, the problem you are seeing is not that the snow is any slower, but the amount of time needed to draw the background and update the screen takes longer.

The code you have written seems good.

In Pygame and SDL everything defaults to software by defualt. You can force your display to be hardware accelerated, but you have to ask for it, and it only works in FULLSCREEN display modes.

To get the hardware acceleration, add the FULLSCREEN flag to your HWSURFACE|DOUBLEBUF.

Most pygame projects use the software display modes and keep track of the modified areas of the screen (the "dirty rectangles"). By tracking these you know exactly what to update and your project will basically run the same speed. Here is what you'd want to change from your source to speed it up with software mode.

First, change the Flocke.draw to return the value from fill.

def draw(self):
return screen.fill(self.color, (self.xpos, self.ypos, self.size, self.size))

Before your loop create an empty list named "dirty". Then change the loop to look like this.


while 1:
for r in self.dirty:
screen.blit(background, dirty, dirty)
old_dirty = dirty
dirty = []

for jede_flock in flockerl:
jede_flocke.update()
dirty.append(jede_flocke.draw())

pygame.display.update(old_dirty + dirty)


Hopefully that is understandable. It doesn't take a lot of code at this point to track the changed areas and only work with them.