[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Cycling images



mmmm, I've problem with this, the alpha channel is not working, this function allways prints "NO alpha" although the image has the alpha channel (and it doent shows the transparency bg, it shows in black)

def animstrip(img, width=0, colorkey=None):
if not width:
width = img.get_height()
size = width, img.get_height()
images = []
for x in range(0, img.get_width(), width):
i = pygame.Surface(size)
i.blit(img, (0, 0), ((x, 0), size))

if i.get_alpha() is None:
print "NO alpha"
i = i.convert()
else:
print "alpha"
i = i.convert_alpha()
if colorkey is not None:
if colorkey is -1:
colorkey = i.get_at((0,0))
i.set_colorkey(colorkey, RLEACCEL)
images.append(i)
return images

Pete Shinners wrote:


This is pretty easy to do, there are a couple techniques you may want. The main trick is that the Surface.blit() takes an optional third argument that is the source area of the source image to use. You can also do this by creating subsurfaces, but that may be more trouble than worth. Here is some code Solarwolf uses to chop a strip of images into frames.


def animstrip(img, width=0):
if not width:
width = img.get_height()
size = width, img.get_height()
images = []
for x in range(0, img.get_width(), width):
i = pygame.Surface(size)
i.blit(img, (0, 0), ((x, 0), size))
images.append(i)
return images

I've simplified this code a little. If you are dealing with colorkeys or surface alphas you will need some extra work.