6
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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

def load_anim():
    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)

frames = load_anim()
pyglet.clock.schedule_interval(update_frames,1/10.0)
pyglet.app.run()
\$\endgroup\$
  • \$\begingroup\$ Could you add the source example? That would be very helpful, thank you! \$\endgroup\$ – Bentley4 Jun 28 '12 at 10:33
  • \$\begingroup\$ 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. \$\endgroup\$ – Bentley4 Jun 30 '12 at 15:50
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ – Peter Parker Jun 30 '12 at 20:31
7
\$\begingroup\$

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)

@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)
raw = pyglet.image.load('cat.jpg')
raw_seq = pyglet.image.ImageGrid(raw, 5, 5)
anim = pyglet.image.Animation.from_image_sequence(raw_seq, 0.5, True)
sprite2 = pyglet.sprite.Sprite(anim)

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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