How to make an Sprite turn red and return to the original color?

Asked

Viewed 14 times

0

I am recreating a game in pygame as a hobby, in this game I have a ship that can take a single shot or keep shooting if you hold the button, but the ship will heat up and gain a red coloring. But I’m having a really hard time finding a way to do that, and I’ve even managed to get the ship to turn red, but what it takes is getting her back to normal.

For this I thought to just take away the opacity only of the red Surface, but no satisfactory results.

Below is part of the class code:

class Player(pygame.sprite.Sprite):
    def __init__(self, screen):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load(util.decode_b64_img(ship)).convert_alpha()
        self.image = pygame.transform.scale(self.image, (50, 40))

        self.current_heat = 0
        
        self.red_surf = pygame.Surface(self.rect.size, SRCALPHA)
        self.alpha = 255
        self.red_surf.fill((255, 0, 0, self.alpha))

    def heat_cannon(self, heat_value):
        self.current_heat = min(self.current_heat + heat_value, self.resistance)

        self.red_surf.fill((0, 5, 5, 0))
        self.image.blit(self.red_surf, (0, 0), special_flags=BLEND_RGBA_SUB)

    def cool_down(self):
        self.current_heat = max(self.current_heat - self.cool_down_rate, 0)

        self.alpha = max(self.alpha-5, 0)
        self.red_surf.fill((0, 0, 0, self.alpha))
        self.image.blit(self.red_surf, (0, 0))

The problem is in the method cool_down, that the red_surf should be more transparent and not. In my view, it is because of the blend mode that is in the method heat_cannon.

This is only part of the code, but I’ll leave the link to the repository here: https://github.com/HugoPFe/Project-Asteroids

There would be some way around this blend mode and make Surface transparent?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.