在 Pyglet 中传输/加载 PNG 图像导致 "blotchy" 图像具有奇怪的 alpha 层

Blitting / Loading PNG image in Pyglet resulting in "blotchy" image with an odd alpha layer

我正在尝试在 pyglet 中显示 PNG 图像。 PNG 图像是这样的:

但是,当我尝试使用以下代码呈现它时:

if fullscreen:
    window = pyglet.window.Window(caption = "Forsaken Light, by Damian Heaton",
                                  fullscreen = True)
else:
    window = pyglet.window.Window(caption = "Forsaken Light, by Damian Heaton",
                                  fullscreen = False,
                                  width = 500, height = 250)
window.set_icon(pyglet.image.load('icon.ico'))

logo = pyglet.image.load(
    os.path.join("res", "graphics", "temp", "misc", "logo.png"))

class MenuEventHandler(object):
    def on_draw(self):
        window.clear()
        logo.blit((window.width / 2) - (logo.width / 2),
                  window.height - logo.height)

    def on_key_press(self, symbol, modifiers):
        if symbol == key.ESCAPE:
            sys.exit(0)
        return True

    def on_mouse_press(self, x, y, button, modifiers):
        print("Mouse button pressed in menu")
        return True

menu_handlers = MenuEventHandler()

window.push_handlers(menu_handlers)

def stop_game():
    window.pop_handlers()

pyglet.app.run()

显示如下:

我已经尝试搜索我的问题所在,但所有使用 Pyglet 渲染 PNG 图像的示例脚本似乎都表明我的代码是正确的...

如有任何帮助,我们将不胜感激,在此先感谢您。

如果您仍在寻找它,请看这里。

由于它是 PNG 图片,您不能将它放在 window 上,因为它会失去透明度。您需要像

这样从 pyglet 导入 PNGImageDecoder

from pyglet.image.codecs.png import PNGImageDecoder

然后使用它来加载 PNG 图片,如

kitten = pyglet.image.load('kitten.png', decoder=PNGImageDecoder())

最后使用

在 window 上绘制

kitten.draw(),在指定 x 和 y 坐标之后。

上面的文档可以找到here

将此添加到 on_draw 函数的开头:

glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)