1

I'm making animated sprites in pygame, and would like help finding a way to flip from one to the other? The current code looks something like this:

class normal(pygame.sprite.Sprite):
  def __init__(self):
    #etc, list of images to create the animation

class tall(pygame.sprite.Sprite):
  def __init__(self):
    #rinse and repeat with a different set of images

I already have an idea for how to trigger the change via keystroke. But I'm not sure which variable to change, and to what. When I try to change with the following code, nothing happens

fps = 25
pygame.init()
my_sprite = normal()
my_group = pygame.sprite.Group(my_sprite)

#etc until we get to the part where it changes

    if event.type == pygame.KEYDOWN:
      if event.key == pygame.K_RETURN:
        if my_sprite == normal():
          my_sprite = tall()
          fps = 30
        else:
          my_sprite = normal()
          fps = 25

I'm not sure exactly what isn't working in my code as it doesn't come back with an error. Can someone point me in the right direction?

2 Answers2

2

It's not working because when the code calls normal() it's creating a new instance of an object. So the call:

if my_sprite == normal():

Is saying "is my existing sprite object == this new sprite object", which is never true. You can use the python function to type() of the object to do the same thing, or add your own type-function as I have presented in the code below.

I would track the state of the sprite inside the class, and use some functions grow() and shrink() to change the size automatically.

class GrowingSprite( pygame.sprite.Sprite, x, y ):
    def __init__( self ):
        #etc, list of images to create the animation
        self.image_short  = ... # load "short" image
        self.image_tall   = ... # load "tall" image
        # Set the initial state
        self.image        = self.image_short       # start short
        self.rect         = self.image.get_rect()
        self.rect.centerx = x
        self.rect.centery = y
        self.state        = 'short' 

    def getState( self ):
        return self.state

    def grow( self ):
        self.state = 'tall'
        self.image = self.image_tall
        current_x  = self.rect.centerx     # preserve existing location
        current_y  = self.rect.centery
        self.rect  = self.image.get_rect()
        self.rect.center = ( current_x, current_y )

    def shrink( self ):
        self.state = 'short'
        self.image = self.image_short
        current_x = self.rect.centerx      # preserve existing location
        current_y = self.rect.centery
        self.rect = self.image.get_rect()
        self.rect.center = ( current_x, current_y )
Kingsley
  • 14,398
  • 5
  • 31
  • 53
0

I found a workaround that is a bit redundant, but it makes it possible to expand it in case there are more groups

It requires making two groups that can be pulled back and fourth, and changing the foo.draw(screen) to the new group. This is how it looks

nml_sprite = normal()
tal_sprite = tall()
tg = 1 #this is nothing more than a switch to trigger the change
tal_group = pygame.sprite.Group(tal_sprite)
nml_group = pygame.sprite.Group(nml_sprite)
cursprite = nml_group #this variable determines which sprite set is loaded
  ...
      ...
        if event.key == pygame.K_RETURN:
          if tg == 1:
            curspt = tal_group
            tg = 2
          else:
            curspt = nml_group
            tg = 2
         ...
       ...
     nml_group.update()
     tal_group.update()
     curspt.draw(screen)
     ...