【问题标题】:Pygame: How do I slowly fade and kill a sprite?Pygame:我如何慢慢淡化并杀死一个精灵?
【发布时间】:2018-12-05 00:11:17
【问题描述】:

当我运行这段代码sprite.spritecollide(a,group,True) 时,精灵会从屏幕上移除。但它会立即消失。如何对其应用淡化效果?我希望它在完全移除之前慢慢淡出。我已经阅读了文档,但仍然不知道该怎么做。

【问题讨论】:

    标签: python pygame sprite fadeout kill


    【解决方案1】:

    您可以通过用白色(使用所需的 alpha 值)填充每个像素的 alpha 表面来使其透明,并传递pygame.BLEND_RGBA_MULT 特殊标志。将self.fade 属性添加到您的精灵子类并将其设置为True 以启动效果,然后每帧降低alpha 值,复制原始图像并使其透明。当 alpha

    import pygame as pg
    from pygame.math import Vector2
    
    
    pg.init()
    PLAYER_IMAGE = pg.Surface((42, 68), pg.SRCALPHA)
    PLAYER_IMAGE.fill(pg.Color('dodgerblue'))
    
    
    class Entity(pg.sprite.Sprite):
    
        def __init__(self, pos, *groups):
            super().__init__(*groups)
            self.image = PLAYER_IMAGE
            self.rect = self.image.get_rect(center=pos)
            self.vel = Vector2(0, 0)
            self.pos = Vector2(pos)
            self.alpha = 255
            self.fade = False
    
        def update(self):
            self.pos += self.vel
            self.rect.center = self.pos
    
            if self.fade:  # If the fade effect is activated.
                # Reduce the alpha each frame, create a new copy of the original
                # image and fill it with white (with the self.alpha value)
                # and pass the BLEND_RGBA_MULT special_flag to reduce the alpha.
                self.alpha = max(0, self.alpha-5)  # alpha should never be < 0.
                self.image = PLAYER_IMAGE.copy()
                self.image.fill((255, 255, 255, self.alpha), special_flags=pg.BLEND_RGBA_MULT)
                if self.alpha <= 0:  # Kill the sprite when the alpha is <= 0.
                    self.kill()
    
    
    def main():
        screen = pg.display.set_mode((640, 480))
        clock = pg.time.Clock()
        all_sprites = pg.sprite.Group()
        entity = Entity((250, 170), all_sprites)
        entity2 = Entity((350, 270), all_sprites)
        group = pg.sprite.Group(entity2)
        done = False
    
        while not done:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    done = True
                elif event.type == pg.KEYDOWN:
                    if event.key == pg.K_d:
                        entity.vel.x = 5
                    elif event.key == pg.K_a:
                        entity.vel.x = -5
                    elif event.key == pg.K_w:
                        entity.vel.y = -5
                    elif event.key == pg.K_s:
                        entity.vel.y = 5
                    elif event.key == pg.K_SPACE:
                        entity.fade = True  # Start the fade effect.
                elif event.type == pg.KEYUP:
                    if event.key == pg.K_d and entity.vel.x > 0:
                        entity.vel.x = 0
                    elif event.key == pg.K_a and entity.vel.x < 0:
                        entity.vel.x = 0
                    elif event.key == pg.K_w:
                        entity.vel.y = 0
                    elif event.key == pg.K_s:
                        entity.vel.y = 0
    
            all_sprites.update()
    
            collided = pg.sprite.spritecollide(entity, group, False)
            for sprite in collided:
                sprite.fade = True  # Start the fade effect.
    
            screen.fill((30, 30, 30))
            all_sprites.draw(screen)
    
            pg.display.flip()
            clock.tick(60)
    
        pg.quit()
    
    
    if __name__ == '__main__':
        main()
    

    【讨论】:

      猜你喜欢
      • 2020-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      • 2016-04-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多