On Sep 11, 2007, at 9:00 PM, Ian Mallett wrote:

Opps.  That's:

if frame_number%10 == 0: #frame_number is a multiple of ten
  #draw frame 1
elif frame_number%9 == 0: #frame_number ends in 9
  #draw frame 2
[snip lots of elifs]

Or assuming you store the images in a list, e.g.:

images = [image1, image2, image3, image4, image5]

then

this_image = images[frame_number % len(images)]

which works with any arbitrary number of images without code changes and is faster and much more concise that a big if/elif block (which IMO should be avoided as much as possible).

I also have this handy class for loading lots of images from files that I can match with a pattern:

class Animation:

        def __init__(self, file_pattern, colorkey=(0,0,0), alpha=255, fade=0):
                self.images = []
                for path in glob.glob('data/%s' % file_pattern):
                        image = pygame.image.load(path).convert()
                        image.set_colorkey(colorkey, RLEACCEL)
                        image.set_alpha(max(alpha, 0), RLEACCEL)
                        alpha -= fade
                        self.images.append(image)

Which I use for explosions where I want the alpha to fade to transparent as it plays through.

You use it like so:

xplode = Animation('xplode/*.jpg', alpha=180, fade=8)

or to just load it with everything opaque:

anim = Animation('somedir/*.png')

You said you were using sprite sheets though so it may not be that helpful to you.

-Casey

Reply via email to