我到目前为止得到的代码
import pyglet
from pyglet.window import key
animation = pyglet.image.load_animation('/home/arctic/Downloads/work/gif/ErrorToSurprised.gif')
animSprite = pyglet.sprite.Sprite(animation)
w = animSprite.width
h = animSprite.height
window = pyglet.window.Window(width=w, height=h, resizable=True)
@window.event
def on_key_press(symbol, modifiers):
if symbol == key.A:
animation = pyglet.image.load_animation('image1.gif')
elif symbol == key.B:
animation = pyglet.image.load_animation('image2.gif')
elif symbol == key.ENTER:
print("Enter Key Was Pressed")
@window.event
def on_draw():
window.clear()
pyglet.app.run()
这会产生一个错误,我认为我没有在 elif 符号==键下正确加载 gif。该函数显示窗口默认的 gif。然后听按键按下,根据按键显示某个gif
这里有两个问题:
on_draw
中向屏幕上绘制任何内容,因此不会出现任何内容。您需要添加animSprite.draw()
。animation
变量中,并且不对其执行任何操作。您必须将 animSprite.image
属性更改为新动画。这是包含这两项更改的代码版本。
import pyglet
from pyglet.window import key
window = pyglet.window.Window(resizable=True)
initial_animation = pyglet.image.load_animation(
"/home/arctic/Downloads/work/gif/ErrorToSurprised.gif"
)
animation_1 = pyglet.image.load_animation("image1.gif")
animation_2 = pyglet.image.load_animation("image2.gif")
animSprite = pyglet.sprite.Sprite(initial_animation)
window.width = animSprite.width
window.height = animSprite.height
@window.event
def on_key_press(symbol, modifiers):
if symbol == key.A:
animSprite.image = animation_1
elif symbol == key.B:
animSprite.image = animation_2
elif symbol == key.ENTER:
print("Enter Key Was Pressed")
@window.event
def on_draw():
window.clear()
animSprite.draw()
pyglet.app.run()