【问题标题】:is there any way to delete pygame sprite image using a function?有什么方法可以使用函数删除 pygame sprite 图像?
【发布时间】:2021-03-05 15:10:17
【问题描述】:

正如您可能通过“self.image”所说的那样,它旨在代表玩家的健康状况。我想知道是否有任何方法可以在满足特定条件时使用“healthDeleted(self)”来删除(或更改)self.image。

class Player_sprite_Health(pygame.sprite.Sprite):
    def __init__(self):
        self.image = pygame.image.load("images/HUD/hud_heartFull.png")
        self.rect = self.rect = self.image.get_rect(topleft = (35,10))

    def healthDeleted(self):
        ''' Upon collision with an enemy object
            this function will be called
            It's purpose is to remove health
         '''

【问题讨论】:

    标签: python pygame


    【解决方案1】:

    如果要删除pygame.sprite.Sprite 对象,必须将其从所有pygame.sprite.Groups 中删除。 Sprite 可以使用kill从所有中删除:

    class Player_sprite_Health(pygame.sprite.Sprite):
        # [...]
    
        def healthDeleted(self):
            self.kill()
    

    【讨论】:

    • 不是我想要的,但它完成了工作。
    • @FUTURE 你想要什么?不要试图绕过系统。使用killremove
    猜你喜欢
    • 2021-11-13
    • 1970-01-01
    • 2014-07-01
    • 2020-04-29
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    相关资源
    最近更新 更多