在pyglet中定期更改图像(精灵?)

时间:2012-06-27 14:39:54

标签: python sprite pyglet

如何使此代码正常工作:只需安装pyglet并将"assassin1.png""assassin2.png"更改为存储在您将此代码保存到文件的目录中的图像的名称。

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()

描述:此代码创建一个黑色背景屏幕,其中显示fps和 图像“assassin1.png”显示在位置(50,30)。只要按住右方向按钮,图像就会向右移动。

目标:我想实现这一点,只要按住右方向按钮并且图像正在移动,assassin1.png图像会定期更改(每0.25秒左右)第二张图像{{ 1}}。这是为了造成图像行走的模糊错觉。

我如何实现这一目标?

我已经在Assassin类中创建了一个空的assassin2.png方法,它似乎是放置代码以实现我的目标的合适位置。但是如果你想将代码放在另一个地方也可以。

1 个答案:

答案 0 :(得分:4)

pyglet.sprite.Sprite课程允许您随时将其图片编辑为animation。因此,在spritees构造函数中,我们定义了一个walk动画:

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)
    #...

接下来,我们添加一个函数,以便更改动画:

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

最后我们在适当的时候调用该函数:

#...
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
#...

瞧!当用户按下右键时,精灵会移动并动画!