鼠标悬停在 Sprite 对象 Pyglet 上?
Mouse hover on Sprite object Pyglet?
我想知道是否有办法使用 Pyglet 捕捉鼠标悬停在 sprite 对象上?
my_sprite = pyglet.sprite.Sprite(image, x, y)
Tkinter 中的类似内容:
sprite.bind(circle, "<Enter>", on_enter)
下面是一个检测鼠标悬停在移动的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()
我想知道是否有办法使用 Pyglet 捕捉鼠标悬停在 sprite 对象上?
my_sprite = pyglet.sprite.Sprite(image, x, y)
Tkinter 中的类似内容:
sprite.bind(circle, "<Enter>", on_enter)
下面是一个检测鼠标悬停在移动的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()