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

Re: [pygame] sprite engine



DR0ID wrote:
Brian Fisher schrieb:

For example, what happens when the ghost moves behind the tree? In
that case, the ghost didn't dirty any part of the tree
Huh? When the Gohst moves or changes layer (!)....


I think I see what's going on here. Some people appear to be confusing a 'dirty sprite' (one that needs repainting) with a 'dirty screen rectangle)' (an area of the screen that needs to be redrawn.) Internally, I don't call the sprite flag 'dirty' - i call it 'paint.' If a graphic needs to be recreated because of size, appearance, or text changes, the 'paint' flag is turned on.

Maybe the following pseudocode will make the order of events clear.

For each dirty rectangle
   For each sprite
      If the sprite needs painting (paint flag is on)
         paint it (will add another rect to the dirty rects list)
         clear the paint flag
      If the sprite intersects the dirty rectangle
         draw it

Note that the dirty rectangle list is potentially modified as you iterate over it. Because of it, you shouldn't just 'clear the dirtyrects' at the end because you'll eat your newly-added rects! You should do a loop something like:

while len(dirtyrects) > 0:
   do proper stuff with dirtyrect[0]
   del dirtyrects[0]

This will repaint the existing dirty rectangles, and any new ones that got added during the iteration. And going through it this way is a good sanity check - if your program is looping endlessly, it's a sign you've put in an extra 'SetRect' where you don't need it, and the list never gets clear.


--Kamilche