【问题标题】:Is there a way to change my_sprite or my_group to change the animation displayed?有没有办法改变 my_sprite 或 my_group 来改变显示的动画?
【发布时间】:2019-10-15 00:37:48
【问题描述】:

我正在 pygame 中制作动画精灵,并且需要帮助找到一种从一个翻转到另一个的方法?当前代码如下所示:

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

我已经知道如何通过按键触发更改。但我不确定要更改哪个变量以及更改什么。当我尝试使用以下代码进行更改时,没有任何反应

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

我不确定我的代码到底有什么问题,因为它不会返回错误。有人能指出我正确的方向吗?

【问题讨论】:

    标签: python pygame sprite


    【解决方案1】:

    它不起作用,因为当代码调用normal() 时,它正在创建一个对象的新实例。所以调用:

    if my_sprite == normal():
    

    在说“是我现有的精灵对象 == 这个新的精灵对象”,这绝不是真的。您可以使用 python 函数对对象的type() 执行相同的操作,或者添加您自己的类型函数,如下面的代码所示。

    我会在类内部跟踪精灵的状态,并使用一些函数grow()shrink()自动改变大小。

    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 )
    

    【讨论】:

    • 感谢您的帮助!真的很感激!
    【解决方案2】:

    我发现了一个有点多余的解决方法,但它可以在有更多组的情况下扩展它

    这需要做两个可以拉回的组和第四组,并将foo.draw(screen)更改为新组。看起来是这样的

    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)
         ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多