omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Spritesheet Animations

    Pythonista
    5
    12
    7334
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • C0deH4cker
      C0deH4cker last edited by

      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

      1 Reply Last reply Reply Quote 0
      • Gcarver166
        Gcarver166 last edited by

        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.

        1 Reply Last reply Reply Quote 0
        • C0deH4cker
          C0deH4cker last edited by

          I didnt need to worry about alpha though because of the black background

          1 Reply Last reply Reply Quote 0
          • Gcarver166
            Gcarver166 last edited by

            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.
            <pre>
            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)
            </pre>

            1 Reply Last reply Reply Quote 0
            • Gcarver166
              Gcarver166 last edited by

              Here is another sample of an animated walking soldier.

              https://gist.github.com/4180945

              1 Reply Last reply Reply Quote 0
              • Dalorbi39
                Dalorbi39 last edited by

                test @Gcarver where would the alpha code go?

                1 Reply Last reply Reply Quote 0
                • Gcarver166
                  Gcarver166 last edited by

                  Right after the Image.open() call.

                  1 Reply Last reply Reply Quote 0
                  • Dalorbi39
                    Dalorbi39 last edited by

                    thanks, (dont mind me i'm an idiot)

                    1 Reply Last reply Reply Quote 0
                    • eliskan175
                      eliskan175 last edited by

                      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.

                      1 Reply Last reply Reply Quote 0
                      • C0deH4cker
                        C0deH4cker last edited by

                        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

                        1 Reply Last reply Reply Quote 0
                        • eliskan175
                          eliskan175 last edited by

                          Hey C0deh4cker, check out the code I posted in the share section using this example.

                          1 Reply Last reply Reply Quote 0
                          • omz
                            omz last edited by

                            In version 1.3 (out today), spritesheet animations should be a lot easier to do. The <a href="http://omz-software.com/pythonista/docs/ios/scene.html#scene.image">image()</a> function has some new, optional parameters that allow you to draw just a part of an image, without having to slice it using PIL.

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post
                            Powered by NodeBB Forums | Contributors