1

How can I add a moving Gif in pygame for a simple loop? I tried normal image importing but the .gif was invisible. Any ideas on how to do this? I want to do it for a new game I'm making with the main characters legs moving and fire to move. I haven't tried sprites in pygame because image importing is easy enough and it has worked thus far for static images with maybe a change to face different directions

I've tried simply this:

img = pygame.image.load("img.gif")

I blit'd it onto the screen. Invisible. I tried it with a .PNG and it was visible.

7
  • If your question was not a duplicated (which it is) it would be better received if you posted the code that you tried, FWIW. Nov 12, 2014 at 3:54
  • You can't do that. pygame does not allow.
    – user2961646
    Nov 12, 2014 at 5:06
  • @AHuman you can work around it.
    – A.J. Uppal
    Nov 12, 2014 at 5:46
  • @GreenAsJade, he's posted all the code he's tried, you can't ask for much more code than that, it wouldn't help.
    – A.J. Uppal
    Nov 12, 2014 at 5:47
  • @ToClickorNottoClick True, but it takes a LOT of work.
    – user2961646
    Nov 12, 2014 at 5:47

1 Answer 1

2

Short answer: You cannot do that using pygame.image.load

Long answer: Yes, there is a work around.

  1. Extract each of the images from the gif (if on a Mac use this).
  2. Add all the image names to a list
  3. Using a for loop, load them all and put it in a dictionary                                                                   you can use another list for this instead, your choice.

img_names = [*my image names*]
all_imgs = {}
for img in img_names:
    all_imgs[img] = pygame.image.load(img)

  1. Then looping through img_names, you can blit each image to the screen

for img in img_names:
    screen.blit(all_imgs[img], (desiredx, desiredy))
1
  • I did this and it only loaded one image... Same code exactly but didnt work
    – HKVariant
    Dec 7, 2014 at 2:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.