0

I have a surface in pygame that has alpha values and those are important since I am using them in a mask for collision detection. However, I would like to make this surface twice as wide (so a new surface that has 2 version of the original next to each other).

My solution so far is to create a new (empty) surface that is twice as wide as my original surface and then blit the original surface on this new surface twice. This does produce a wider surface but it breaks the alpha values so the mask collisions doesn't work anymore. I have already tried to use the flag on the surface pygame.SRCALPHA and I have also set colorkeys, neither seems to work.

The code otherwise works just fine and the parts of the initially empty surfaces are transparent. Is there something else I need to add?

For reference, here is a code snippet to illustrate the issue:

import pygame,sys

class Block(pygame.sprite.Sprite):
    def __init__(self,groups):
        super().__init__(groups)
        single_surf = pygame.image.load('water.png').convert_alpha()
        self.image = pygame.Surface((single_surf.get_width() * 2,single_surf.get_height()),flags = pygame.SRCALPHA)
        self.image.set_colorkey((0,0,0))
        self.image.blit(single_surf,(0,0))
        self.image.blit(single_surf,(single_surf.get_width(),0))

        self.rect = self.image.get_rect(topleft = (200,200))
        self.mask = pygame.mask.from_surface(self.image)

class MouseBlock(pygame.sprite.Sprite):
    def __init__(self,groups):
        super().__init__(groups)
        self.image = pygame.image.load('duck.png').convert_alpha()
        self.rect = self.image.get_rect(topleft = (0,0))
        self.mask = pygame.mask.from_surface(self.image)

    def update(self):
        if pygame.mouse.get_pos():
            self.rect.center = pygame.mouse.get_pos()

pygame.init()
screen = pygame.display.set_mode((600,600))
clock = pygame.time.Clock()

visible_sprite_group = pygame.sprite.Group()
collision_sprite_group = pygame.sprite.Group()

Block([visible_sprite_group,collision_sprite_group])
mouseblock = MouseBlock(visible_sprite_group)

while True:     
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
            
        screen.fill('white')
        visible_sprite_group.update()
        visible_sprite_group.draw(screen)

        if pygame.sprite.spritecollide(mouseblock,collision_sprite_group,False,pygame.sprite.collide_mask):
            print('collision')


        pygame.display.update()
        clock.tick(60)
Another_coder
  • 728
  • 1
  • 9
  • 23
  • Could you provide a code fragment ? It would be easier to think about it. – Peterrabbit Mar 01 '22 at 14:08
  • added a snippet now – Another_coder Mar 01 '22 at 15:41
  • Well I donn't really know what could be wrong.. you definitely need to use the flag SRCALPHA so I think you should keep that. For set_colorkey I'm not sure. If you have already a rgba pixel schema I think you shouldn't use it. – Peterrabbit Mar 01 '22 at 19:20
  • pygame website seems completely off right now so I can't see the docs with functions signatures.. doesn't help.. – Peterrabbit Mar 01 '22 at 19:22
  • 1
    @Peterrabbit you can find backup of the page in archive.org: https://web.archive.org/web/20200530030918/https://www.pygame.org/docs/ – furas Mar 01 '22 at 20:33
  • `colorkey` is for something different then `alpha`. You may have to create new `Surface()`, use `convert_alpha()`, and fill with transparent background `fill((0,0,0,0))` `(R,G,B,A)` before you will blit images. See [example](https://github.com/furas/python-examples/tree/master/pygame/transparency/complex-example) – furas Mar 01 '22 at 20:38
  • 1
    @furas When you use `pygame.SRCALPHA` you get a surface with per pixel alpha format filled with (0, 0, 0, 0) - [How to make a surface with a transparent background in pygame](https://stackoverflow.com/questions/328061/how-to-make-a-surface-with-a-transparent-background-in-pygame/64512639#64512639) – Rabbid76 Mar 01 '22 at 20:40
  • 1
    you could add your images so we could test it with your images. – furas Mar 01 '22 at 20:44
  • 1
    @Rabbid76 good to know it - I never checked this. – furas Mar 01 '22 at 20:45
  • 1
    Possibly the background of the image is not transparent but black (0, 0, 0, 255). Using a PNG does not automatically mean that the image has a transparent background. – Rabbid76 Mar 01 '22 at 20:49

0 Answers0