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

Re: [pygame] about animation gif file



> > some gif file has many frames to paly animation,Can pygame load these
> > frames? and do the animation?
> 
> Yes. Load the GIF as a Surface. Make a bunch of Rect objects 
> representing where in the image each frame is. Use those as clipping 
> regions.

If you're talking about storing multiple frames in a single
(non-animated) image, Pete demonstrates this nicely in SolarWolf. The
code is quite readable. This code is found in gfx.py:

def animstrip(img, width=0):
    if not width:
        width = img.get_height()
    size = width, img.get_height()
    images = []
    origalpha = img.get_alpha()
    origckey = img.get_colorkey()
    img.set_colorkey(None)
    img.set_alpha(None)
    for x in range(0, img.get_width(), width):
        i = pygame.Surface(size)
        i.blit(img, (0, 0), ((x, 0), size))
        if origalpha:
            i.set_colorkey((0,0,0))
        elif origckey:
            i.set_colorkey(origckey)
        images.append(optimize(i))
    img.set_alpha(origalpha)
    img.set_colorkey(origckey)
    return images

-- 
 The first time the [artificially intelligent] creature was put down
 in the game world, he just stared at his feet. I was puzzled, but
 after debugging found that the creature was trying to eat himself.
 He was hungry, and had spotted himself as a nearby convenient object!
     - Richard Evans, creator of the AI game "Black & White"