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)