Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I clear a pygame alpha layer efficiently?
    text
    copied!<p>I'm trying to write a 2D game using python / pygame that blits several layers on top of one another every screen refresh. My basic setup is (from bottom to top):</p> <ul> <li>Background: surface (non-transparent), scrolls at a different rate than rest</li> <li>Midground: SRCALPHA transparent surface (static)</li> <li>Player / Sprites / Enemies: sprite group</li> <li>Forground: SRCALPHA transparent surface (static)</li> </ul> <p>Right now, I'm blitting these four layers one on top of another every screen. The background scrolls at a different rate than the other three layers, which is why I have it separate from midground. As I have the game structured now, it runs on my fairly modest laptop at 60fps. </p> <p>-BUT- I'm having trouble with the sprite group, which I'm blitting directly to the screen. Having to adjust the rect for every sprite according to my current viewport seems like an ugly way to program things, and I'd like a more elegant solution.</p> <p>I'd love to blit the sprites to another transparent surface which I could manage, but therin lies my problem: I can't find a way of clearing a transparent layer that doesn't half my performance. Some of the setups I've tried:</p> <ul> <li>I've tried filling the layer with a white surface with blend mode rgba_sub (surf.fill((255,255,255,255), area, BLEND_RGBA_SUB)) -- this is super, super slow</li> <li>I've tried surface.copy() of a blank surface - this is faster, but still halves my fps</li> <li>I've tried combining the sprites with the midground layer and using pygame.sprite.LayeredUpdates to update the sprites. This has no effect on performance, but does not work where the midground is transparent. I get trails of sprites over the background layer.</li> </ul> <p>The best solution I've found so far is my current setup of drawing sprites directly to the screen. It looks great, runs fast, but is a pain to manage, as I have to make sure each sprites' rect is adjusted according to the viewport every frame. Its also making collision detection difficult.</p> <p><strong>Is there another quick way to clear a pygame transparent surface? Quick as in, can be done 60+ times a second?</strong> Alternately, is there a setup for my layers that would still accomplish the same effect?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload