Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
up vote 3 down vote accepted

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)

import pyglet
window = pyglet.window.Window()

def load_anim():
    for i in range(2):
    return arrImages

def update_frames(dt):
    global counter

def on_draw():
    print counter,0,0,0)

frames = load_anim()
share|improve this answer
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)

# Load sprites
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)

def on_draw():

if __name__ == '__main__':

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)

# Create a static sprite
sprite = pyglet.resource.image('cat.jpg')

# Cut our cat up into a 5x5 grid of images to move through (sprite sheet)
raw = pyglet.image.load('cat.jpg')
raw_seq = pyglet.image.ImageGrid(raw, 5, 5)
anim = pyglet.image.Animation.from_image_sequence(raw_seq, 0.1, True)
sprite2 = pyglet.sprite.Sprite(anim)

def on_draw():
  sprite.blit(25, 25, width=150, height=150)

if __name__ == '__main__':
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.