


Remember that the screen is a Surface!īlue_image = pygame.Surface(size, pygame.SRCALPHA) # Contains a flag telling pygame that the Surface is per-pixel alpha Screen.fill(WHITE) # Make the background white. import pygameīLUE = (0, 0, 255, 50) # This color contains an extra integer. Press 2, 3 or 4 multiple times to make them more opaque. Press the keys 1, 2, 3 or 4 to make the images appear. purple_t_colorkey(BLACK)Ĭopy this in an empty file and run it. This can be useful if you don't want the slower performance of a per-pixel Surface. That's why the black rectangle in the middle disappear.Ĭolorkeys and Surface alphas can be combined, but per-pixel alpha cannot. Unlike the other Surfaces, this Surface default color won't be black but transparent. (my_image, BLUE, my_image.get_rect(), 10) The Surface will now draw transparency if the color contains the fourth alpha value. My_image = pygame.Surface(size, pygame.SRCALPHA) # Creates an empty per-pixel alpha Surface. This method also requires the Surface to be created as a per-pixel alpha Surface, and the color arguments needs to contain a fourth alpha integer. This gives you the most freedom and flexibility but is also the slowest method. Makes every pixel in the Surface transparent by a individual alpha value. my_t_alpha(100) # 0 is fully transparent and 255 fully opaque. With this method you can have different alpha values but it will affect the whole Surface. Makes the whole Surface transparent by an alpha value. Colorkeys cannot have different alpha values, it can only make a color not visible. Setting another colorkey will overwrite the previous. My_t_colorkey(BLACK) # Black colors will not be blit.Ī Surface can only have one colorkey. If you have an image with a black rect inside you could set a colorkey to prevent the black color from being blit.

Makes a color fully transparent, or more accurately, making a color simply not be blit. There are kinds 3 of transparency supported in pygame: colorkeys, Surface alphas, and per-pixel alphas.
