pygame animated gif

Started by
1 comment, last by daviangel 16 years, 9 months ago
Well I'm working on my second game and I have decided that it would be like a Zelda. I have successfully gotten the map to scroll, zelda image to move, and turn. But the image has an animation, though I can't see it. My guess is that I'm reloading the image to fast to. Is there a way I can make the animation show? The animation is simply the user walking. here is the code

#Import
import pygame
from pygame.locals import *

#defing globals
global zelda_image
global image
image = "zelda_walk_right.gif"
zelda_image = pygame.image.load(image)
zelda_image = zelda_image.convert()

#initiate screen
pygame.init()

screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Zelda Suck Ass ed.")

background = pygame.image.load("map.PNG")
background = background.convert()


#define

class map_update:
    
    def __init__(self, bg, zelda_pos):
        self.bg = bg
        self.zelda = [zelda_pos[0], zelda_pos[1]]
        
    def scroll(self):
        
        self.bg = [self.bg[0], self.bg[1]]
        
        self.keystate = pygame.key.get_pressed()
        
        
        if self.zelda[1] <= 70 and self.bg[1] not in range(-10, 100):
            if self.keystate[pygame.locals.K_UP]:
                self.bg[1] += 5

        if self.zelda[1] >= 505:            
            if self.keystate[pygame.locals.K_DOWN]:
                self.bg[1] -= 5

        if self.zelda[0] >= 705:           
            if self.keystate[pygame.locals.K_RIGHT]:
                self.bg[0] -= 5

        if self.zelda[0] <= 70 and self.bg[0] not in range(-10, 100):            
            if self.keystate[pygame.locals.K_LEFT]:
                self.bg[0] += 5

        return self.bg



class zelda_update:

    def __init__(self, zelda, bg_p):
        self.zelda = [zelda[0], zelda[1]]
        self.bgp = [bg_p[0], bg_p[1]]

    def move(self):
        
        global zelda_image, image
        self.keystate = pygame.key.get_pressed()
                          
        if self.keystate[pygame.locals.K_DOWN]:
            
            if image != "zelda_walk_down.gif":
                image = "zelda_walk_down.gif"
                zelda_image = pygame.image.load(image)
                zelda_image = zelda_image.convert()
            
            if self.zelda[1] <= 505: 
                self.zelda[1] += 3
                           
        elif self.keystate[pygame.locals.K_RIGHT]:
            
            if image != "zelda_walk_right.gif":
            
                image = "zelda_walk_right.gif"
                zelda_image = pygame.image.load(image)
                zelda_image = zelda_image.convert()
            
            if self.zelda[0] <= 705:  
                self.zelda[0] += 3
                
        elif self.keystate[pygame.locals.K_UP]:

            if image != "zelda_walk_up.gif":            
                image = "zelda_walk_up.gif"
                zelda_image = pygame.image.load(image)
                zelda_image = zelda_image.convert()
            
            if self.zelda[1] >= 70:
                self.zelda[1] -= 3
                          
        elif self.keystate[pygame.locals.K_LEFT]:

            if image != "zelda_walk_left.gif":          
                image = "zelda_walk_left.gif"
                zelda_image = pygame.image.load(image)
                zelda_image = zelda_image.convert()
            
            if self.zelda[0] >= 70:
                self.zelda[0] -= 3
                

        self.zelda =(self.zelda[0], self.zelda[1])

        return self.zelda


#set vars out of loop
bg_p = (-10, -10)
zelda_pos = (100, 100)

keepgoing = True

clock = pygame.time.Clock()

# Set up main loop

while keepgoing:

    #time
    clock.tick(30)
    
    #initiate classes
    zelda = zelda_update(zelda_pos, bg_p)
    
    map = map_update(bg_p, zelda_pos)
    
    #Start event loop
    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepgoing = False


    #movement
    bg_p = map.scroll()
    zelda_pos = zelda.move()                    

    #blit images to screen
    screen.blit(background, bg_p)
    screen.blit(zelda_image, zelda_pos)
    pygame.display.flip()
    #end of loop


here is the picture Thanks, Joe [Edited by - avgprogramingjoe on August 9, 2007 7:57:07 PM]
Visit my general programming site to get help, tutorials, as well as get experience by joining projects or making your own - My Site
Advertisement
I'm not sure about Pygame, but for all graphics APIs I've used, none of them let you have animated gifs(they'll just show one frame). Try breaking the frames up with a program like the Gimp.
I'm a loser, thanks for letting me know.N00b game programmer.
Quote:Original post by herocks
I'm not sure about Pygame, but for all graphics APIs I've used, none of them let you have animated gifs(they'll just show one frame). Try breaking the frames up with a program like the Gimp.

Yup animated gif's are not supported by pygame
GIF (non animated)

so to get animation you have to do break up your images and code something like the following:
""" cowMooDelay.py     illustrates a multi-frame animation    adding a delay    added 2 attributes(delay,pause) to cow class    Jaime Moreno    4/11/07"""import pygamepygame.init()class Cow(pygame.sprite.Sprite):    def __init__(self):        pygame.sprite.Sprite.__init__(self)                self.loadImages()        self.image = self.imageStand        self.rect = self.image.get_rect()        self.rect.center = (320, 240)        self.frame = 0        # increase delay value to slow down animation even more        self.delay = 6        self.pause = 0    def update(self):        self.pause += 1        if self.pause >= self.delay:            #reset pause and advance animation            self.pause = 0            self.frame += 1            if self.frame >= len(self.mooImages):                self.frame = 0            self.image = self.mooImages[self.frame]            def loadImages(self):        self.imageStand = pygame.image.load("cowImages/stopped0002.bmp")        self.imageStand = self.imageStand.convert()        transColor = self.imageStand.get_at((1, 1))        self.imageStand.set_colorkey(transColor)        self.mooImages = []        for i in range(10):            imgName = "cowImages/muuuh e000%d.bmp" % i            tmpImage = pygame.image.load(imgName)            tmpImage = tmpImage.convert()            transColor = tmpImage.get_at((1, 1))            tmpImage.set_colorkey(transColor)            self.mooImages.append(tmpImage)screen = pygame.display.set_mode((640, 480))pygame.display.set_caption("Adding a Delay")background = pygame.Surface(screen.get_size())background.fill((0, 0x99, 0))screen.blit(background, (0, 0))cow = Cow()allSprites = pygame.sprite.Group(cow)clock = pygame.time.Clock()keepGoing = Truewhile keepGoing:    clock.tick(30)    for event in pygame.event.get():        if event.type == pygame.QUIT:            keepGoing = False        allSprites.clear(screen, background)    allSprites.update()    allSprites.draw(screen)        pygame.display.flip()

And if you aren't a gimp or photoshop expert here are some excellent stuff you can use here.
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe

This topic is closed to new replies.

Advertisement