Bentley4
Bentley4

Reputation: 11038

Periodically changing images(sprites?) in pyglet

How to make this code work: Just have pyglet installed and change "assassin1.png" and "assassin2.png" with the name of an images stored in the directory where you saved this code to a file.

import pyglet

class Assassin(pyglet.sprite.Sprite):
    def __init__(self, batch):
        pyglet.sprite.Sprite.__init__(self, pyglet.resource.image("assassin1.png"))
        self.x = 50
        self.y = 30
    def forward_movement(self):
        pass # How do I continously change between 'assassin1.png' and 'assassin2.png'?    

class Game(pyglet.window.Window):
    def __init__(self):
        pyglet.window.Window.__init__(self, width = 315, height = 220)
        self.batch_draw = pyglet.graphics.Batch()
        self.player = Assassin(batch = self.batch_draw)
        self.fps_display = pyglet.clock.ClockDisplay()
        self.keys_held = []      
        self.schedule = pyglet.clock.schedule_interval(func = self.update, interval = 1/60.) 

    def on_draw(self):
        self.clear()
        self.fps_display.draw()
        self.batch_draw.draw()
        self.player.draw()  

    def on_key_press(self, symbol, modifiers):
        self.keys_held.append(symbol)
        if symbol == pyglet.window.key.RIGHT:
            self.player.forward_movement()
            print "The 'RIGHT' key was pressed"

    def on_key_release(self, symbol, modifiers):
        self.keys_held.pop(self.keys_held.index(symbol))

    def update(self, interval):
        if pyglet.window.key.RIGHT in self.keys_held:
            self.player.x += 50 * interval

if __name__ == "__main__":
    window = Game()
    pyglet.app.run()

Description: This code creates a black background screen, where the fps are displayed and an image "assassin1.png" is displayed at position (50, 30). As long as the right direction button is held down the image will move to the right.

Goal: I would like to implement that whenever the right direction button is held and the image is moving, the assassin1.png image is changed periodically (every 0.25 secs or so) with a second image assassin2.png. This in order to create the vague illusion that the image is walking.

How do I achieve this goal?

I already created an empty forward_movement() method in the Assassin class which would seem an appropriate place to put the code to achieve my goal. But if you would want to place the code in another place thats ok too.

Upvotes: 2

Views: 2277

Answers (1)

sjohnson.pi
sjohnson.pi

Reputation: 398

The pyglet.sprite.Sprite class allows you to edit its image to an animation at anytime. So, in the sprites constructor, we define a walk animation:

def __init__(self, batch):
    # The image to display when not moving
    self._img_main = pyglet.image.load('assassin.png')

    self._img_right_1 = pyglet.image.load('assassin1.png')
    self._img_right_2 = pyglet.image.load('assassin2.png')
    self.anim_right = pyglet.image.Animation.from_image_sequence([
        self._img_right_1, self._img_right_2], 0.5, True)
    # 0.5 is the number in seconds between frames
    # True means to keep looping (We stop it later)

    pyglet.sprite.Sprite.__init__(self, self._img_main)
    #...

Next we add a function to make it easier to change animations:

def forward_movement(self, flag=True):
    if flag:
        self.image = self.anim_right # Now our sprite animates
    else:
        self.image = self._img_main

Finally we call the function at the appropriate time:

#...
def on_key_press(self, symbol, modifiers):
    self.keys_held.append(symbol)
    if symbol == pyglet.window.key.RIGHT:
        self.player.forward_movement(True)
        print "The 'RIGHT' key was pressed"

def on_key_release(self, symbol, modifiers):
    self.keys_held.pop(self.keys_held.index(symbol))
    if symbol == pyglet.window.key.RIGHT:
        self.player.forward_movement(False) # We have stopped moving
#...

And voilà! When the user has the right-key down, the sprite moves and animates!

Upvotes: 5

Related Questions