I have this idea for a game where you dodge projectiles while controlling a helicopter. I am wondering if you can make a sprite appear as a GIF image, or something along the lines of two images switching every fraction of a second. I know how to make a sprite appear as one image:
self.surf = pygame.image.load("example.png").convert()
But I was wondering if this had an effect:
self.surf = pygame.image.load("example.gif").convert()
Unfortunately, it only displayed the first image in the gif.
Here is the GIF image:
Ok, so I looked at the answers and tried to implement them in my code, but then it was all too confusing and I had tried to do something a bit more simple. This is what I came up with:
if play == 1:
self.surf = pygame.image.load("Image2.png").convert()
pygame.display.update()
play = 2
time.sleep(.2)
if play == 2:
self.surf = pygame.image.load("Image1.png").convert()
pygame.display.update()
play = 1
time.sleep(.2)
But, all that did was display the player sprite as image 1. Is there anything I can add to make this work?
Pygame respectively the
pygame.image
module can only handle non-animated GIFs.But on the Pygame homepage is introduced the GIFImage library:
Another option is to use the Pillow library (pip install Pillow).
Write a function, that can convert a PIL image to a
pygame.Surface
: (see also PIL and pygame.image)Use the PIL library to load a GIF frame by frame: (see also Extracting The Frames Of An Animated GIF Using Pillow
See also Load animated GIF and a simple animated gif viewer example:
You can use the list of
pygame.Surface
objects to generate a Spritesheet.