鼠标悬停在 Sprite 对象 Pyglet 上?

问题描述 投票:0回答:2

我想知道是否有办法用 Pyglet 捕获鼠标悬停在精灵对象上?

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

Tkinter 中有这样的东西:

sprite.bind(circle, "<Enter>", on_enter)
python events sprite mouse pyglet
2个回答
0
投票

下面是一个检测鼠标悬停在移动的 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()

0
投票

您正在寻找边界检查处理程序。请参阅 pyglet-users 中的讨论。最重要的是 pyglet 没有用于精灵边界检查的内置处理程序。尽管代码不是很清楚或没有详细记录,但上述受访者展示了一个基本的对象边界测试循环。不仅“粗鲁但有效”,这也是处理程序通常会为您做的事情。

© www.soinside.com 2019 - 2024. All rights reserved.