Cornholio
Cornholio

Reputation: 985

How to get Mask with transparency in pygame?

I'm writing a game in Python with Pygame, and I'm trying to keep my player sprite from touching trees dotted around the map. My player's sprite has transparency (of course), and when I load a Pygame Mask with pygame.mask.from_surface(self.getNextFrame()), it only loads the parts with actual color.

My question is, does anyone know how to get pygame to load the mask WITH the transparency from the original image?

Upvotes: 1

Views: 1247

Answers (1)

orlp
orlp

Reputation: 117661

Yes, use the second argument:

mask = pygame.mask.from_surface(self.getNextFrame(), 0)

By setting it to 0 any pixel is seen as opaque. See the documentation.

Please note that the result of this is simply a rectangle of the size of the surface. So it might actually be more performant to use this:

mask = pygame.mask.Mask(self.getNextFrame().get_size())
mask.fill()

Upvotes: 2

Related Questions