2

如何使此代码工作:只需安装并更改 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,并在位置 (50, 30) 处显示图像“assassin1.png”。只要按住右方向按钮,图像就会向右移动。

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

我如何实现这个目标?

我已经在 Assassin 类中创建了一个空forward_movement()方法,这似乎是放置代码以实现我的目标的合适位置。但是,如果您想将代码放在另一个地方,那也可以。

4

1 回答 1

5

pyglet.sprite.Sprite类允许您随时将其图像编辑为动画。因此,在 sprites 构造函数中,我们定义了一个行走动画:

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

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

于 2012-09-20T10:30:42.673 回答