Can someone give me some example code that creates a surface with a transparent background in pygame?
Colorkey transparency makes a single color value transparent. Any pixels matching the colorkey will not be drawn. The surface alpha value is a single value that changes the transparency for the entire image. A surface alpha of 255 is opaque, and a value of 0 is completely transparent.
Creating a surface Creating surfaces in pygame is quite easy. We just have to pass the height and the width with a tuple to pygame. Surface() method.
The method get_rect() returns a Rect object from an image. At this point only the size is set and position is placed at (0, 0). We set the center of the Rect to the center of the screen: rect = img. get_rect() rect.
This should do it:
image = pygame.Surface([640,480], pygame.SRCALPHA, 32) image = image.convert_alpha()
Make sure that the color depth (32) stays explicitly set else this will not work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With