0
\$\begingroup\$

Drawing a large background is relatively slow in PyGame. In order to avoid drawing BG every frame, you could draw it once, then do nothing.

However, if something is overdrawn onto the surface and keeps moving, you will need to redraw the background in order to "erase" the color pixels left by moving object; otherwise, you will have "traces" of the moving object.

I have a moving object in my PyGame. However, I do not want to "clear the color buffer" by redrawing the background image. Redrawing the background image every frame is slow.

My solution :

I will "clear" only required portions (where the "traces" of moving object are left) of the "buffer" by redrawing portions of background.

Is there any other better way to have a consistent background?

\$\endgroup\$
  • \$\begingroup\$ Re-drawing the BG each frame shouldnt be too slow, are you calling convert_alpha() when you load the image? \$\endgroup\$ – Serial May 12 '14 at 22:50
  • \$\begingroup\$ I am. conver_alpha() had significantly improved performance of my game. However I still want to squeeze out more frames because I want to add more stuff moving around in the game. However my solution to squeeze out more frame by "partical rerendering background" is not good enough. I am wondering if there was other techniques that I can pull more frames that's not "partial rerendering background" \$\endgroup\$ – Tofu_Craving_Redish_BlueDragon May 13 '14 at 7:34
1
\$\begingroup\$

If I understand you correctly, you don't want to have to redraw the entire screen when a single object changes position. You've basically hit upon the solution; only redraw the portion of the screen that you need to. One method to do this is dirty rectangles. Roughly, the algorithm is:

  1. Get the objects that will be moving this frame.
  2. Get their bounding boxes, before they move. Call this list of boxes B.
  3. For each box b in B, check if b is intersecting bounding boxes of objects that aren't moving. If it is, union those the boxes (including b) and replace b with this new rectangle.
  4. Redraw all the portions of the background defined by the rectangles in B, as well as all the entities to which those rectangles belong.

However, clearing the screen and redrawing everything should be fast enough nowadays. If you have already tried redrawing everything every frame and it is slow, you may want to ensure there's not something else suspicious going on.

\$\endgroup\$
  • \$\begingroup\$ I am working on xo olpc. It is a powerful but performance is limited \$\endgroup\$ – Tofu_Craving_Redish_BlueDragon May 8 '14 at 19:33
  • \$\begingroup\$ ah, yeah, if you're hardware limited not much you can do there but try to optimize. \$\endgroup\$ – Chaosed0 May 8 '14 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.