Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a surface with a transparent background in pygame

Can someone give me some example code that creates a surface with a transparent background in pygame?

like image 280
Paul D. Eden Avatar asked Nov 29 '08 21:11

Paul D. Eden


People also ask

How do you make a pygame surface transparent?

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.

How do I create a surface in pygame?

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.

What is Get_rect () in pygame?

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.


1 Answers

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.

like image 53
UnkwnTech Avatar answered Sep 21 '22 12:09

UnkwnTech