鼠标未在pyglet中绘图

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

我的自定义鼠标图像未显示在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_cursor = pyglet.image.load('image.png')-结果如下:ImageData 85x82

当我打印时-self.cursor = pyglet.window.ImageMouseCursor(self.game_cursor,50,70)-结果如下:pyglet.window.ImageMouseCursor对象位于0x7f4dad76b390

当我打印时-self.game_window.set_mouse_cursor(self.cursor)-这是结果:

如何解决使鼠标显示?

python mouse pyglet
1个回答
0
投票

我强烈建议您将window类继承到您的类中,而不是尽可能将其保留为内部值。因此,您可以使用替代挂钩代替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

这是一个工作示例:

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()
© www.soinside.com 2019 - 2024. All rights reserved.