Welcome to the Pythonista Community Forums!

Pythonista is a Python programming environment for iOS. To learn more, head over to the Pythonista Website.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Twitter
Spritesheet Animations
  • So this was a short project I did. It uses a PNG spritesheet downloaded from the internet to display an animation. Just a basic example of how to do this with Pythonista by using PIL.

    The interesting part is actually my motivation behind writing this. Someone in a chatroom asked if Python could be used to "explode a hamster." I couldn't resist :D


    Anyways, here's the Gist: https://gist.github.com/20403fcfab8cbb3bbf5a
  • Thanks for this. It has set me on the right path for animating my characters. BTW the alpha is not correct on the image. I haven't figured out the best way to set it. I used the image converted to "L" format to set the alpha channel but that is not quite right.
  • I didnt need to worry about alpha though because of the black background
  • Understood. But if anyone cares about alpha the following code inserted into hamster.py uses the 1st pixel as a keycolor to set the alpha to 0.

    d = img.load()
    keycolor = d[0,0]
    for x in range(img.size[0]):
    for y in range(img.size[1]):
    p = d[x, y]
    if p == keycolor:
    d[x, y] = (p[0], p[1], p[2], 0)
  • Here is another sample of an animated walking soldier.

    https://gist.github.com/4180945
  • test @Gcarver where would the alpha code go?
  • Right after the Image.open() call.
  • thanks, (dont mind me i'm an idiot)
  • How can this be used to explode a hamster?!

    I love this example and I'm working on a small game engine inspired by it. I will post the code once the game engine actually works and my code isn't such a mess that my bedroom envies it.
  • Ill post my code for an AnimatedSprite class when I'm done with it. It will do all of the work for you. You just need to specify the filename and # of frames. Then, call the instance's draw method from your scene's draw function. There are also methods to control the animation like FPS, jump to frame, pause/play, etc
  • Hey C0deh4cker, check out the code I posted in the share section using this example.
  • In version 1.3 (out today), spritesheet animations should be a lot easier to do. The image() function has some new, optional parameters that allow you to draw just a part of an image, without having to slice it using PIL.