鼠标悬停在精灵对象Pyglet上?

2024-05-21 07:44:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我想知道是否有一种方法可以捕捉鼠标悬停在精灵对象上?在

my_sprite = pyglet.sprite.Sprite(image, x, y)

在Tkinter中是这样的:

^{pr2}$

Tags: 对象方法imagetkintermy精灵pygletsprite
1条回答
网友
1楼 · 发布于 2024-05-21 07:44:59

下面是一个演示代码,用于检测鼠标悬停在移动的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()

相关问题 更多 >