我的自定义鼠标图像没有显示在pyglet中。它正常加载(当您在类外部创建窗口时),但是当我在类中创建窗口并尝试添加自定义鼠标光标时,什么也没有发生
class x:
def __init__(self):
self.game_window = pyglet.window.Window()
self.on_mouse_press = self.game_window.event(self.on_mouse_press)
self.game_cursor = pyglet.image.load('image.png')
self.cursor = pyglet.window.ImageMouseCursor(self.game_cursor, 50, 70)
self.game_window.set_mouse_cursor(self.cursor)
我试过打印每一行代码(用于鼠标图像加载)
打印时-self.game\u cursor=pyglet.image.load('image.png')-结果如下: ImageData 85x82
打印时-self.cursor=pyglet.window.ImageMouseCursor(self.game\u cursor,50,70)-这是结果: 位于0x7f4dad76b390的pyglet.window.ImageMouseCursor对象
打印时-self.game\u窗口。设置鼠标\u光标(self.cursor)-结果如下: 无
我如何修复鼠标显示
我强烈建议您将窗口类继承到类中,而不是尽可能将其保留为内部值。因此,您可以使用覆盖钩子替换
on_
函数。也许这是一种过时的方法,但就目前而言,我已经学会了处理这些事情——这是再次被推荐的下面是一个工作示例:
根据ImageMouseCursor类的pyglet文档link方法draw(x,y)是抽象的。因此,我尝试过对ImageMouseCursor进行子分类,并实现如下绘制方法:
但是,如果未启用总账混合,这将不起作用。需要gl混合来显示alpha通道。我通过对Window类进行子分类并使用glEnable/glBlendFunc函数来启用它。这是代码中执行以下操作的部分:
希望这有帮助
相关问题 更多 >
编程相关推荐