简体   繁体   中英

Pygame Sprite transparency collision

I would like to create physics body around the physical paths of the texture (cutting out alpha channel). How to do it in pygame? Is it going to be effective?

player_idle = pygame.image.load('data\img\player\idle\idle-1.png')

You can use the mask module, especially the pygame.mask.from_surface() function:

Creates a Mask object from the given surface by setting all the opaque pixels and not setting the transparent pixels.

Then you can store the resulting Mask object as mask -attribute in your Sprites and eg use pygame.sprite.collide_mask() for pixel-perfect collision detection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM