Need Help with Python, Pyglet animations!

Started by
23 comments, last by Kylotan 14 years, 1 month ago
***EDITED FOR A CLEARER DESCRIPTION OF WHAT IM LOOKING FOR*** hey so I've searched around a little bit and i cant find an answer exactly to my n00b question. just started learning how to code in python but i know this is possible. so here goes: i don't need an animation to PLAY. i just wish to use a picture (png or gif preferably) to have X amount of frames in it (some cases hundreds of frames) and to be able to display Y frame when Z key is pressed (the cause or event doesnt matter as it will change down the road) in python/pyglet how can i reference a GIF file frame (either frames from an already existing animation OR a grid/sprite sheet i already made) i'll have multiple images that will be running at once so this process needs to work for all. i know about batches, i know about image grids, i know about textureregions, bins and atlases. none of the documentation i've found has any specific implementation on how to an actual example is really what i need. i found something close on these forums but its not complete. thanks for any help/input guys! still learning. [Edited by - jpetrie on March 15, 2010 10:20:02 PM]
Advertisement
really? 50 views and not one reply?

no one knows how to access one frame at a time with pyglet? this is a game dev forum lol...
Despite the information you've given it's not quite specific enough for me to be sure what you want. Do you want animations/frames to play sequentially when a key is pressed, or do you want different keys to display different single frames (no actual animation), or something else entirely? It seems like you want the 2nd but I'm having a hard time picturing it. It would be best, if you can, to also describe the finished game here. Pretend it works and I'm playing it...what am I doing? What am I seeing on screen?

Quote:
really? 50 views and not one reply?

no one knows how to access one frame at a time with pyglet? this is a game dev forum lol...


That's very normal, and not considered a lot of views for this forum. People frequently view posts that they probably can't answer. There aren't a lot of pyglet users here. I'll do my best. Maybe swiftcoder will swing by - he's probably the most experienced pyglet user here.
Quote:Original post by Hollower
Maybe swiftcoder will swing by - he's probably the most experienced pyglet user here.
Thanks ;)

Thus far, full support for controlling animation hasn't made it into pyglet. However, I had the same need a while ago, and ended up implementing the necessary features, which code you can find here, on my blog.

That class should be a drop-in replacement for pyglet.sprite.Sprite, of which it is a subclass.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

hey guys, thanks for the reply.

specifically , i don't need an animation to PLAY. i just wish to use a picture (png or gif preferably) to have X amount of frames in it (some cases hundreds of frames) and to be able to display Y frame when Z key is pressed (the cause or event doesnt matter as it will change down the road)

currently the best method that is completely undocumented (even in the source code) is "AnimatedFrame" in pyglet. only mentioned ONCE throughout the entirety of documentation and never explained or example.

if its not doable i was willing to go the way of a sprite sheet. but the issue remains .. i need an example of how to display a SINGLE frame efficiently (using bins or batches is fine by me) because there will be about a dozen GIFS that will be doing this.

i will check out swiftcoder's blog tonight (going out for my b day :) )
and will check back tomorrow.
thanks again guys
oops double posted.
Use pygame instead ;P
pygame doesn't do what i need (read above, last reply)

if you can show me an exact code example of how it can id GLADLY be a pygame convert. but ive researched that enough in the past to know its lacking for my specific needs
hey swiftcoder, i shot you and email asking for an example of your subclass being used to display a single frame. im sure its pretty straight forward but i just cant see it from staring at the code.
thanks for your help!

Honestly "a dozen" is not a lot and plain old blitting will be plenty fast. And if you mean to display only one of them at a time then you really don't need to batch since it is for combining simultaneously displayed images into one draw call.

animation = pyglet.image.load_animation('animation.gif')
frames = [frame.image for frame in animation.frames]

You're right the docs don't list AnimationFrame's instance variables, but they are the same as the constructor arguments. Obviously that class is only intended for internal use by Animation.

I know you said you didn't want to keep lists but this can be encapsulated in a class that takes care of everything. I decided to take a stab at this, but I don't really understand your application so my example looks kind of strange. I don't know how you plan to bind keys to potentially thousands of individual frames, so I just bound a few in the example (A,B,C).

import pygletfrom pyglet.window import keyclass GifWindow(pyglet.window.Window):    def __init__(self):        super(GifWindow, self).__init__()        self.frames = {}        self.show = None        def load_gifs(self):        self.set_mouse_cursor( self.get_system_mouse_cursor(self.CURSOR_WAIT) )                import glob        for filename in glob.glob("*.gif"):            anim = pyglet.image.load_animation(filename)            self.frames[filename] = [frame.image for frame in anim.frames]                self.set_mouse_cursor( self.get_system_mouse_cursor(self.CURSOR_DEFAULT) )        def on_draw(self):        self.clear()        if self.show:            self.show.blit(0,0)        def on_key_press(self, symbol, modifiers):        frames = self.frames                if symbol == key.A:            self.show = frames["cat.gif"][0]                elif symbol == key.B:            self.show = frames["dog.gif"][25]                elif symbol == key.C:            self.show = frames["dog.gif"][42]window = GifWindow()window.load_gifs()pyglet.app.run()

This topic is closed to new replies.

Advertisement