鼠标未在pyglet中绘制

2024-09-30 02:35:59 发布

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

我的自定义鼠标图像没有显示在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)-结果如下:

我如何修复鼠标显示


Tags: 图像imageselfgamepngonload鼠标
2条回答

我强烈建议您将窗口类继承到类中,而不是尽可能将其保留为内部值。因此,您可以使用覆盖钩子替换on_函数。也许这是一种过时的方法,但就目前而言,我已经学会了处理这些事情——这是再次被推荐的

class x(pyglet.window.Window):
    def __init__(self):
        super(x, self).__init__()
        self.game_cursor = pyglet.image.load('image.png')
        self.cursor = pyglet.window.ImageMouseCursor(self.game_cursor, 50, 70)
        self.set_mouse_cursor(self.cursor)

    def on_mouse_press():
        # Your code handling on_mouse_press

下面是一个工作示例:

enter image description here

from pyglet import *
from pyglet.gl import *

key = pyglet.window.key

class main(pyglet.window.Window):
    def __init__ (self, width=800, height=600, fps=False, *args, **kwargs):
        super(main, self).__init__(width, height, *args, **kwargs)

        self.game_cursor = pyglet.image.load('image.png')
        self.cursor = pyglet.window.ImageMouseCursor(self.game_cursor, 50, 70)
        self.set_mouse_cursor(self.cursor)

        self.x, self.y = 0, 0

        self.keys = {}

        self.mouse_x = 0
        self.mouse_y = 0

        self.alive = 1

    def on_draw(self):
        self.render()

    def on_close(self):
        self.alive = 0

    def on_mouse_motion(self, x, y, dx, dy):
        self.mouse_x = x

    def on_key_release(self, symbol, modifiers):
        try:
            del self.keys[symbol]
        except:
            pass

    def on_key_press(self, symbol, modifiers):
        if symbol == key.ESCAPE: # [ESC]
            self.alive = 0

        self.keys[symbol] = True

    def render(self):
        self.clear()

        ## Add stuff you want to render here.
        ## Preferably in the form of a batch.

        self.flip()

    def run(self):
        while self.alive == 1:
            self.render()

            #      -> This is key <     
            # This is what replaces pyglet.app.run()
            # but is required for the GUI to not freeze
            #
            event = self.dispatch_events()

if __name__ == '__main__':
    x = main()
    x.run()

根据ImageMouseCursor类的pyglet文档link方法draw(x,y)是抽象的。因此,我尝试过对ImageMouseCursor进行子分类,并实现如下绘制方法:

    import pyglet
    pyglet.resource.path = ['resources']
    pyglet.resource.reindex()

    # subclass definition
    class GameMouse(pyglet.window.ImageMouseCursor):
        # class initialization
        def __init__(self):
            self.game_cursor = pyglet.resource.image('game_cursor.png')
            super().__init__(self.game_cursor, 0, 34)
        # method override
        def draw(self, x, y):
            self.game_cursor.blit(x, y)

但是,如果未启用总账混合,这将不起作用。需要gl混合来显示alpha通道。我通过对Window类进行子分类并使用glEnable/glBlendFunc函数来启用它。这是代码中执行以下操作的部分:

    # subclass definition: 
    class GameApp(pyglet.window.Window):
        # class initialization
        def __init__(self):
            super(GameApp, self).__init__()
            pyglet.gl.glEnable(pyglet.gl.GL_BLEND)
            pyglet.gl.glBlendFunc(pyglet.gl.GL_SRC_ALPHA,
                                  pyglet.gl.GL_ONE_MINUS_SRC_ALPHA)

希望这有帮助

相关问题 更多 >

    热门问题