I use an animation to make a character walk.
On keypress, i change the sprite animation and x position
The problem is that the animation freezes while the key is pressed
when i do not change the sprite.image attribute, the animation continues
when updating the position.
Any idea why the animation freezes when changing the image attribute?
thanks
here is the code:
import pyglet
from pyglet.window import key
game_window = pyglet.window.Window(800, 600)
pyglet.resource.path = ['../resources']
pyglet.resource.reindex()
level_label = pyglet.text.Label(text="My Amazing Game",
x=400, y=575, anchor_x='center')
spriteGrid =
pyglet.image.ImageGrid(pyglet.resource.image("female_walkcycle.png"), 4,
9)
anim_right = pyglet.image.Animation.from_image_sequence(spriteGrid[0:6],
0.2, True)
anim_left = pyglet.image.Animation.from_image_sequence(spriteGrid[19:27],
0.2, True)
anim_up = pyglet.image.Animation.from_image_sequence(spriteGrid[28:37],
0.2, True)
anim_down = pyglet.image.Animation.from_image_sequence(spriteGrid[9:18],
0.2, True)
class Player(pyglet.sprite.Sprite):
def __init__(self, img, x, y):
self.walkspeed = 1
self.key_handler = key.KeyStateHandler()
pyglet.sprite.Sprite.__init__(self, img=img, x=x, y=y)
def update(self, dt):
""" this method should be called every frame """
if self.key_handler[key.LEFT]:
self.image = anim_left
self.x -= self.walkspeed
if self.key_handler[key.RIGHT]:
self.image = anim_right
self.x += self.walkspeed
if self.key_handler[key.UP]:
self.image = anim_up
self.y += self.walkspeed
if self.key_handler[key.DOWN]:
self.image = anim_down
self.y -= self.walkspeed
player = Player(img=anim_left, x=10, y=10)
# Tell the main window that the player object responds to events
game_window.push_handlers(player.key_handler)
objects = [ player ]
@game_window.event
def on_draw():
game_window.clear()
level_label.draw()
player.draw()
def update(dt):
for obj in objects:
obj.update(dt)
if __name__ == '__main__':
# Update the game 120 times per second
pyglet.clock.schedule_interval(update, 1/120.0)
pyglet.app.run()
--
You received this message because you are subscribed to the Google Groups
"pyglet-users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/pyglet-users/-/ocUQCdlRveoJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/pyglet-users?hl=en.