2024-05-21 07:44:59 发布
网友
我想知道是否有一种方法可以捕捉鼠标悬停在精灵对象上?在
my_sprite = pyglet.sprite.Sprite(image, x, y)
在Tkinter中是这样的:
下面是一个演示代码,用于检测鼠标悬停在移动的gif精灵上,你可以尝试一下,把它改成你喜欢的。在
import pyglet from pyglet.window import mouse animation = pyglet.image.load_animation('ur_image_gif_path_like_xxx.gif') bin = pyglet.image.atlas.TextureBin() animation.add_to_texture_bin(bin) sprite = pyglet.sprite.Sprite(img=animation) window = pyglet.window.Window() @window.event def on_draw(): window.clear() sprite.draw() def update(dt): sprite.x += dt*10 @window.event def on_mouse_motion(x, y, dx, dy): # print(x, y, dx, dy) image_width = sprite.image.get_max_width() image_height = sprite.image.get_max_height() if sprite.x+image_width>x>sprite.x and sprite.y+image_height>y>sprite.y: print("mouse hover sprite") else: print("mouse leave sprite") pyglet.clock.schedule_interval(update, 1/60.) pyglet.app.run()
下面是一个演示代码,用于检测鼠标悬停在移动的gif精灵上,你可以尝试一下,把它改成你喜欢的。在
相关问题 更多 >
编程相关推荐