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

Re: [pygame] Sprite Code



I've just been doing it manually, but I haven't been doing very complicated things.

For a jumping grasshopper:

def load_image(name):
    image = pygame.image.load(name)
    image = image.convert()
    return image

def grasshopper_images():
    image = load_image("sprite2.png")
    return [image.subsurface((0,0,64,128)),
            image.subsurface((64,0,64,128)),
            image.subsurface((128,0,64,128))]

class Grasshopper(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.images = grasshopper_images()
        self.image = self.images[self.frame]
    def update(self):
        #timed animation before jumping
        if self.frame == 2:
            self.animcounter = self.animcounter + 1
            if self.animcounter == 30:
                self.animcounter = 0
                self.frame = 0
                self.sound.set_volume(0.6)
                self.sound.play()
                self.jumping = True            
                return
        #update image to correct animation frame
        self.image = self.images[self.frame]


--- On Tue, 8/19/08, kschnee <kschnee@xxxxxxxxxx> wrote:

> From: kschnee <kschnee@xxxxxxxxxx>
> Subject: [pygame] Sprite Code
> To: pygame-users@xxxxxxxx
> Date: Tuesday, August 19, 2008, 9:46 AM
> I've been looking at my sprite code again and finding a
> different way than
> I'd used for loading animation frames. That is,
> Pygame.sprite doesn't seem
> to have any way to set up the notion of having multiple
> frames, and my old
> method involved loading a single "sprite sheet"
> image and finding a set of
> rect objects refering to sections of the sprite sheet.
> 
> What I'm doing lately involves looking in a graphics
> directory
> (\graphics\sprites) for text files, loading some
> info from those, and using
> them to define the rects to be used from a sprite sheet
> image. The input is
> some simple text files and the images. The output is a
> dictionary that
> looks like this (for a sheet called "guy" having
> 4 rows of a 3-frame
> walking animation):
> {"guy":
>  
> {"frame_size":(96,128),"image":<surface
> 388x512>,
>    "animations":{ "stand":[
> (45,[(0,0,96,128),(96,0,96,128)]) [...] ]}
>   }
> }
> 
> ...Well, something like that! Anyway it's listed by
> sprite, then by
> animation name (like "stand"), then by some angle
> that's used to determine
> which frames to use based on the character's facing
> direction, and finally
> as a list of rects marking the individual animation frames.
> (An alternative
> method might be to use the text files just to say things
> like "walk =
> frames 0,1,2,3" and then automatically load frames by
> number only and trust
> the program using this code to know how to use them.) With
> this code you
> should be able to auto-load all sprite data on startup and
> then, for any
> movement angle, look up which frames to cycle through.
> 
> Any interest in this code? What methods do you use to get
> your sheet
> together?