Reputation: 31
So I'm trying to write a function that generates a surface that stacks two pngs I drew and is able to put holes in one so that the other shows through.
I have a drawing of a spaceship exterior. (self.origImage)
I also have a drawing of that same spaceship's interior. (self.baseImage)
I also have a drawing of damage. (pygame.image.load('Damage1.png'))
The damage drawing is a png with per pixel alpha transparency on the edges with increasingly opaque yellows and oranges toward the center, and in the middle a purplish color. ('#770777')
The idea is to blit the damage onto the spaceship exterior. It would look like someone shot a hole into a spaceship full of purple. That part works. Then I want to set a color key to that purple color and blit the whole thing onto the interior drawing so that instead of purple, you would see what's inside.
This is my attempt. It runs whenever new damage is applied and it mostly works, but the purple doesn't convert.
def generateImage(self):
self.buildImage = self.origImage.copy()
for x in range(len(self.objectSquares)):
for y in range(len(self.objectSquares[x])):
if self.objectSquares[x][y].health < 10:
self.buildImage.blit(pygame.image.load('Damage1.png'), ((x*10), (y*10)))
self.buildImage = self.buildImage.convert()
self.buildImage.set_colorkey(pygame.Color('#770777'))
self.compImage = pygame.Surface(self.buildImage.get_size()).convert()
self.compImage.blit(self.baseImage, (0, 0))
self.compImage.blit(self.buildImage, (0, 0))
I also tried grabbing the colorkey from the damage drawing to make sure it wasn't my paint program compressing the image or something.
self.buildImage.set_colorkey(pygame.image.load('Damage1.png').convert().get_at((5, 5)))
and
self.buildImage.set_colorkey(pygame.image.load('Damage1.png').get_at((5, 5)))
And various other combinations.
Does anyone see what I'm doing wrong? I'd appreciate the help!
Upvotes: 3
Views: 818
Reputation: 7511
From the docs: set_colorkey()
The colorkey will be ignored if the Surface is formatted to use per pixel alpha values. The colorkey can be mixed with the full Surface alpha value.
Try using convert_alpha()
and manually setting per-pixel values to alpha when they match your color key.
pygame.transform.threshold() might do part two for you.
Upvotes: 1