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

Re: [pygame] spritesheets and animation



Thanks, now it shows up. But it doesn't animate.  Just displays the 2nd frame oddly enough...


Sean Berry <seaneseor@xxxxxxxxx> wrote:
Please forgive me if this is totally incorrect, I haven't worked with Sprites yet.
 
Looking at your code, you have a Sprite class called Gene.
 
You then instantiated a sprite.Group instance and called it "genesprite". But this group doesn't actually have any sprites in it yet!
You only have one instantiated sprite, so throw it in your group after you created it like so:
 
genesprite = pygame.sprite.RenderUpdates()
genesprite.add(gene)
 
I hope that works, good luck!
-Sean

On Sat, Feb 23, 2008 at 3:18 PM, Devon Scott-Tunkin <djvonfunkin@xxxxxxxxx> wrote:
Hi I've just started using pygame and python in general as of a week ago and I'm probably trying to get up the mountain too soon. In the following program I've frankensteined in order to simply repeat a 4 frame walk cycle animation, I can't get the sprite to display, all I see is black.  If anyone can point me to what I'm doing wrong I'd appreciate it. 

Thanks,

Devon

import os, pygame
from pygame.locals import *

SCREENRECT = Rect(0, 0, 320, 240)

class Spritesheet:
    def __init__(self, filename):
        self.sheet = pygame.image.load(os.path.join('data', filename)).convert()
    def imgat(self, rect, colorkey = None):
        rect = Rect(rect)
        image = pygame.Surface(rect.size).convert()
        image.blit(self.sheet, (0, 0), rect)
        if colorkey is not None:
            if colorkey is -1:
                colorkey = image.get_at((0, 0))
            image.set_colorkey(colorkey, RLEACCEL)
        return image
    #def imgsat(self, rects, colorkey = None):
    #    imgs = []
    #    for rect in rects:
    #        imgs.append(self.imgat(rect, colorkey))
    #    return imgs

def geneimages():
    spritesheet = Spritesheet('sprite_gene.png')
    return [spritesheet.imgat((0, 0, 16, 32), -1),  
            spritesheet.imgat((17, 0, 16, 32), -1), 
            spritesheet.imgat((33, 0, 16, 32), -1),  
            spritesheet.imgat((49, 0, 16, 32), -1)]
   
class Gene(pygame.sprite.Sprite):
    def __init__(self, fps = 10):
        pygame.sprite.Sprite.__init__(self)
        # Track the time we started, and the time between updates.
        # Then we can figure out when we have to switch the image.
        self._frame = 0
        self.image = self._images[self._frame]
        self.rect = self.image.get_rect()
        self._start = pygame.time.get_ticks()
        self._delay = 1000 / fps
        self._last_update = 0

 # Call update to set our first image.
        self.update(pygame.time.get_ticks())
       
    def update(self, t):
        # Note that this doesn't work if it's been more that self._delay
        # time between calls to update(); we only update the image once
        # then, but it really should be updated twice.

        if t - self._last_update > self._delay:
            self._frame += 1
            if self._frame >= len(self._images): self._frame = 0
            self.image = self._images[self._frame]
            self._last_update = t

def main():
    pygame.init()
    screen = pygame.display.set_mode(SCREENRECT.size)
    Gene._images = geneimages()
    gene = Gene()                           
   
 
    genesprite = pygame.sprite.RenderUpdates()

    # Set-up background surface
    global background
    background = "">    background = "">
    # keep track of time
    clock = pygame.time.Clock()

    # Blit everything to the screen
    screen.blit(background, (0, 0))
    pygame.display.flip()
   
    # game loop
    while 1:
        # maintain frame rate
        clock.tick(30)
       
        # get input
        for event in pygame.event.get():
            if event.type == QUIT   \
               or (event.type == KEYDOWN and    \
                   event.key == K_ESCAPE):
                return

        # clear sprites
        genesprite.clear(screen, background)

        # update sprites
        genesprite.update(4)

        # redraw sprites
        genesprite.draw(screen)
        pygame.display.flip()

       

if __name__ == '__main__': main()

Never miss a thing. Make Yahoo your homepage.



Never miss a thing. Make Yahoo your homepage.