[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [pygame] 2 surfaces on a Sprite class



alex wrote:
> Hey there, I'm getting started with Sprites, I was wondering how could I
> manage 2 or more surfaces on a single sprite class. I first tried to make a
> single sprite class for each surface, but each class must share a lot of
> internal variables, so having them as a single class makes things easier..
> but then I realized that when myGroup.draw() is called, it will blit
> self.image to the given surface (usually screen) at the self.rect position,
> so...

alex, it's been a couple days since your questions, but here comes a 
reply (finally)...

one option would be to create a "Render" group that would call a 
"draw()" method for all the sprites. i'm afraid that would cost a little 
speed though. we could also just make a simpler Render group for your 
game that does what you need.

creating new specialized groups for the sprites is not hard to do, and 
certainly encouraged when you need more custom desings. here's something 
that will do what you need (warning, parsed only be my email client)


class RenderTopsUpdates(pygame.sprite.RenderClear):
     """like the normal RenderUpdates group, but will also blit
        an image named "topimage" if it exists. This code copied
        from pygame.sprite with 3 extra lines."""
     def draw(self, surface):
         spritedict = self.spritedict
         surface_blit = surface.blit
         dirty = self.lostsprites
         self.lostsprites = []
         dirty_append = dirty.append
         for s, r in spritedict.items():
             newrect = surface_blit(s.image, s.rect)
             topimage = getattr(s, 'topimage', None)
             if topimage:
                 newrect.union_ip(surface.blit(topimage, s.rect))
             if r is 0: dirty_append(newrect)
             else: dirty_append(newrect.union(r))
             spritedict[s] = newrect
         return dirty


class RenderDrawUpdates(pygame.sprite.RenderClear):
     """like the normal RenderUpdates group, but calls a "draw"
        method for each sprite. sprite.draw() gets one argument, the
        screen to draw to. it must return the area effected."""
     def draw(self, surface):
         spritedict = self.spritedict
         dirty = self.lostsprites
         self.lostsprites = []
         dirty_append = dirty.append
         for s, r in spritedict.items():
             newrect = s.draw(surface)
             if r is 0: dirty_append(newrect)
             else: dirty_append(newrect.union(r))
             spritedict[s] = newrect
         return dirty


woohoo, i got crazy and did a second rendering group.


____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org