pythonpygamepyscripter

In pygame, what is the most efficient way of resetting every sprite's position?


I'm trying to make a typical platformer using pygame but I'm not exactly sure how I should go about doing a death sequence. I want it so after time the player dies every sprite's position is reset to its original position if they moved at all.

I tried experimenting with the pygame.sprite.Group().copy() but I don't know how to use it or if it even applies to my situation.


Solution

  • I would store a copy of the original position in the sprite, and then as @furas suggests re-position and re-stat the sprite via a reset() function.

    For example:

    class ResettableSprite( pygame.sprite.Sprite ):
        MAX_HEALTH = 100
        def __init__( self, image, x, y ):
            pygame.sprite.Sprite.__init__( self )
            self.image   = image
            self.rect    = self.image.get_rect()
            self.start_x = x
            self.start_y = y
            self.reset()
    
        def reset( self ):
            self.rect.x = self.start_x
            self.rect.y = self.start_y
            self.health = ResettableSprite.MAX_HEALTH
            # ... etc.