How to make translucent sprites in pygame

I just started working with pygame, and I'm trying to make a translucent sprite, and the original sprite file is an opaque bitmap file downloaded from disk. I do not want to edit the original image if I can help. I am sure there is a way to do this with pygame code, but Google is not helping me.

+4
source share
4 answers

Perhaps in my initial question I was not clear, but I think I understood it myself. What I was looking for turns out to be the Surface set_alpha () method, so all I had to do was make sure the translucent images were on their own surface.

Here is an example with my codeless:

import pygame, os.path from pygame.locals import * class TranslucentSprite(pygame.sprite.Sprite): def __init__(self): pygame.sprite.Sprite.__init__(self, TranslucentSprite.container) self.image = pygame.image.load(os.path.join('data', 'image.bmp')) self.image = self.image.convert() self.image.set_colorkey(-1, RLEACCEL) self.rect = self.image.get_rect() self.rect.center = (320,240) def main(): pygame.init() screen = pygame.display.set_mode((640,480)) background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((250,250,250)) clock = pygame.time.Clock() transgroups = pygame.sprite.Group() TranslucentSprite.container = transgroups """Here the Translucency Code""" transsurface = pygame.display.set_mode(screen.get_size()) transsurface = transsurface.convert(screen) transsurface.fill((255,0,255)) transsurface.set_colorkey((255,0,255)) transsurface.set_alpha(50) TranslucentSprite() while 1: clock.tick(60) for event in pygame.event.get(): if event.type == QUIT: return elif event.type == KEYDOWN and event.key == K_ESCAPE: return transgroups.draw(transsurface) screen.blit(background,(0,0)) screen.blit(transsurface,(0,0)) pygame.display.flip() if __name__ == '__main__' : main() 

Is this the best method? It seems the simplest and most understandable.

+2
source

After loading the image, you need to enable the alpha channel on the Surface . it will look something like this:

 background = pygame.Display.set_mode() myimage = pygame.image.load("path/to/image.bmp").convert_alpha(background) 

This will download the image and immediately convert it to a pixel format suitable for alpha blending on the display surface. You can use some other surface if you need to break some other buffer outside the screen in a different format.

You can set the alpha pixel quite simply, let's say you have a function that takes a 3-tuple for the rgb color value and returns some desired 4-dimensional rgba + alpha color, you can change the surface to a pixel:

 def set_alphas(color): if color == (255,255,0): # magenta means clear return (0,0,0,0) if color == (0,255,255): # cyan means shadow return (0,0,0,128) r,g,b = color return (r,g,b,255) # otherwise use the solid color from the image. for row in range(myimage.get_height()): for col in range(myimage,get_width()): myimage.set_at((row, col), set_alphas(myimage.get_at((row, col))[:3])) 

There are other, more useful ways to do this, but it gives you an idea, I hope.

+3
source

If your image has a solid background color that you want it to become transparent, you can set it as the color_key value, and pygame will be transparent when the image is blitting.

eg:

 color = image.get_at((0,0)) #we get the color of the upper-left corner pixel image.set_colorkey(color) 
+2
source

you might consider switching to using png images, where you can make whatever transparency you want right on the image.

+1
source

All Articles