# 2D animation example in pyglet (python) looping through 2 images/sprites every x seconds

Suppose you have two images: step1.png and step2.png .

Can anyone show me a very simple example in pyglet how to loop through those 2 images say every 0.5 seconds?

The character doesn't have to move, just a simple black screen with a fixed region wherein the two images continually change every 0.5 secs.

I know how to make a character move, shoot projectiles etc. but I just can't figure out how to control the looping speed of the images.

I use my own sprite class, which has an timer variable which accumulates the dtime in the update_frames() message. By this you have an exact timestamp and can easily change the image based on certain timings.I do not have a source available but I will add this later, if needed

Update: Here is a small piece of code: It loads two frames and displays flip flop it after a second. This will be done in update_frames(dt) (counter is a float between [0-2[, you can also multiply dt to have different timings )

Note that the print in on_draw will print on each frame, but count changes only 10 times a second, configured in pyglet.clock.schedule_interval(update_frames,1/10.0) (play with this value, you will see the duration of the animation will not change)

#!usr/bin/python
import pyglet
window = pyglet.window.Window()
counter=.0

arrImages=[]
for i in range(2):
tmpImg=pyglet.resource.image("step"+str(i)+".png")
arrImages.append(tmpImg)
return arrImages

def update_frames(dt):
global counter
counter=(counter+dt)%2

@window.event
def on_draw():
print counter
pyglet.gl.glClearColor(0,0,0,0)
window.clear()
frames[int(counter)].blit(320,200,0,
frames[int(counter)].width,
frames[int(counter)].height)

pyglet.clock.schedule_interval(update_frames,1/10.0)
pyglet.app.run()

• Could you add the source example? That would be very helpful, thank you! – Bentley4 Jun 28 '12 at 10:33
• How do I change the duration how long every image is displayed? When I run this code I can see the image flickering, is this supposed to happen? (I've had some problems like that before and I read it could be related to missing a particular driver.) Also, this code seems to be very demanding on your processor. If I change the value of 1/10 say 1/10000000 its already somewhat better for the eyes but still very visibly flickering. – Bentley4 Jun 30 '12 at 15:50
• in update_frames(dt). I sum up the time between calls to this function and modulo it by 2 so it counts up to 1.999999 and then starts over on 0. If you want to have a duration of half a second add 2*dt. There is no flickering in my version .. could be an pyglet/driver issue – Peter Parker Jun 30 '12 at 20:31

Like this:

import pyglet

# Create and open a window
window = pyglet.window.Window(200, 200)

s0 = pyglet.resource.image('01.jpg')
s1 = pyglet.resource.image('02.jpg')
sprites = [s0, s1]

# Animation
anim = pyglet.image.Animation.from_image_sequence(sprites, 0.5, True)
sprite = pyglet.sprite.Sprite(anim)

@window.event
def on_draw():
window.clear()
sprite.draw()

if __name__ == '__main__':
pyglet.app.run()


But you probably want to use a texture atlas instead of that, like this:

import pyglet

# Create and open a window
window = pyglet.window.Window(200, 200)

# Cut our cat up into a 5x5 grid of images to move through (sprite sheet)