pyglet - loading/blitting 带 alpha 的图像
pyglet - loading/blitting image with alpha
我正在尝试使用 pyglet 制作一个简单的应用程序。到目前为止,我的主要问题是我似乎无法用 alpha 对图像进行 blit - 所有透明像素都被转换为黑色像素。我不确定问题是出在图像加载还是块传输上。这是我如何尝试渲染图像的非常基本的概述:
import pyglet
import pyglet.clock
window = pyglet.window.Window()
window.config.alpha_size = 8
#fancy text
text = pyglet.resource.image("text.png")
#background image
bg = pyglet.resource.image("bg.png")
bg.blit(0, 0)
text.blit(100, 100)
pyglet.app.run()
感谢任何帮助。提前致谢。
您很可能只需要启用 GL ALPHA 混合。
from pyglet.gl import *
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
但首先,您的代码无法运行。
主要是因为您没有声明 window.event
函数来处理通常渲染内容的 on_draw
。
其次,你永远不会清除你的window(这会造成混乱)。
这是您的代码的最小工作示例:
import pyglet
import pyglet.clock
window = pyglet.window.Window()
window.config.alpha_size = 8
#fancy text
text = pyglet.resource.image("text.png")
#background image
bg = pyglet.resource.image("bg.png")
@window.event
def on_draw():
window.clear()
bg.blit(0, 0)
text.blit(100, 100)
pyglet.app.run()
现在生成这个:
下面是如何使用 GL_BLEND 功能的工作示例:
import pyglet
import pyglet.clock
from pyglet.gl import *
window = pyglet.window.Window()
window.config.alpha_size = 8
#fancy text
text = pyglet.resource.image("text.png")
#background image
bg = pyglet.resource.image("bg.png")
@window.event
def on_draw():
window.clear()
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
bg.blit(0, 0)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
text.blit(100, 100)
pyglet.app.run()
这会产生如下结果:
但是,这段代码很快就会变得混乱。
所以你可以做两件事。您可以先将图像放入 sprite 对象中。其次,让它更加面向对象。
首先,我们将使用精灵。
self.fancy_background = pyglet.sprite.Sprite(pyglet.image.load('bg.png'))
self.fancy_background.draw() # not blit!
Sprites 自动使用透明度,这让您的生活(和代码)更加轻松。
其次,我们将把这些放入一个批次。
批处理是为了捆绑大量精灵,因此您可以在批处理上调用 .draw()
,并且该批处理中的所有精灵都会被 insta-rendered。
self.background = pyglet.graphics.Batch()
self.fancy_background = pyglet.sprite.Sprite(pyglet.image.load('bg.png'), batch=self.background)
self.background.draw() # background, not fancy_background! And also not blit!!
最后也是最重要的。
我们会将其放入 class 以便我们稍后可以做一些很酷的事情。
import pyglet
import pyglet.clock
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.x, self.y = 0, 0
self.background = pyglet.graphics.Batch()
self.texts = pyglet.graphics.Batch()
self.fancy_background = pyglet.sprite.Sprite(pyglet.image.load('bg.png'), batch=self.background)
self.fancy_text = pyglet.sprite.Sprite(pyglet.image.load('text.png'), batch=self.texts)
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
self.mouse_y = y
def on_mouse_press(self, x, y, button, modifiers):
if button == 1: # Left click
pass
def on_key_press(self, symbol, modifiers):
if symbol == key.ESCAPE: # [ESC]
self.alive = 0
def render(self):
self.clear()
self.background.draw()
self.texts.draw()
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()
BAM.
此代码将使您能够稍后创建自定义函数和自定义 "player objects"。您还可以更轻松地进行碰撞检测,并且代码看起来更加结构化(我加入了一些额外的功能,例如键盘和鼠标事件)。
请注意,精灵的位置将默认为 x=0, y=0
,如最后一张图片所示。您可以在 variable/handle 或创建精灵时使用 x=100
设置位置。
我正在尝试使用 pyglet 制作一个简单的应用程序。到目前为止,我的主要问题是我似乎无法用 alpha 对图像进行 blit - 所有透明像素都被转换为黑色像素。我不确定问题是出在图像加载还是块传输上。这是我如何尝试渲染图像的非常基本的概述:
import pyglet
import pyglet.clock
window = pyglet.window.Window()
window.config.alpha_size = 8
#fancy text
text = pyglet.resource.image("text.png")
#background image
bg = pyglet.resource.image("bg.png")
bg.blit(0, 0)
text.blit(100, 100)
pyglet.app.run()
感谢任何帮助。提前致谢。
您很可能只需要启用 GL ALPHA 混合。
from pyglet.gl import *
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
但首先,您的代码无法运行。
主要是因为您没有声明 window.event
函数来处理通常渲染内容的 on_draw
。
其次,你永远不会清除你的window(这会造成混乱)。
这是您的代码的最小工作示例:
import pyglet
import pyglet.clock
window = pyglet.window.Window()
window.config.alpha_size = 8
#fancy text
text = pyglet.resource.image("text.png")
#background image
bg = pyglet.resource.image("bg.png")
@window.event
def on_draw():
window.clear()
bg.blit(0, 0)
text.blit(100, 100)
pyglet.app.run()
现在生成这个:
下面是如何使用 GL_BLEND 功能的工作示例:
import pyglet
import pyglet.clock
from pyglet.gl import *
window = pyglet.window.Window()
window.config.alpha_size = 8
#fancy text
text = pyglet.resource.image("text.png")
#background image
bg = pyglet.resource.image("bg.png")
@window.event
def on_draw():
window.clear()
glEnable(GL_BLEND)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
bg.blit(0, 0)
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
text.blit(100, 100)
pyglet.app.run()
这会产生如下结果:
但是,这段代码很快就会变得混乱。
所以你可以做两件事。您可以先将图像放入 sprite 对象中。其次,让它更加面向对象。
首先,我们将使用精灵。
self.fancy_background = pyglet.sprite.Sprite(pyglet.image.load('bg.png'))
self.fancy_background.draw() # not blit!
Sprites 自动使用透明度,这让您的生活(和代码)更加轻松。
其次,我们将把这些放入一个批次。
批处理是为了捆绑大量精灵,因此您可以在批处理上调用 .draw()
,并且该批处理中的所有精灵都会被 insta-rendered。
self.background = pyglet.graphics.Batch()
self.fancy_background = pyglet.sprite.Sprite(pyglet.image.load('bg.png'), batch=self.background)
self.background.draw() # background, not fancy_background! And also not blit!!
最后也是最重要的。
我们会将其放入 class 以便我们稍后可以做一些很酷的事情。
import pyglet
import pyglet.clock
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.x, self.y = 0, 0
self.background = pyglet.graphics.Batch()
self.texts = pyglet.graphics.Batch()
self.fancy_background = pyglet.sprite.Sprite(pyglet.image.load('bg.png'), batch=self.background)
self.fancy_text = pyglet.sprite.Sprite(pyglet.image.load('text.png'), batch=self.texts)
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
self.mouse_y = y
def on_mouse_press(self, x, y, button, modifiers):
if button == 1: # Left click
pass
def on_key_press(self, symbol, modifiers):
if symbol == key.ESCAPE: # [ESC]
self.alive = 0
def render(self):
self.clear()
self.background.draw()
self.texts.draw()
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()
BAM.
此代码将使您能够稍后创建自定义函数和自定义 "player objects"。您还可以更轻松地进行碰撞检测,并且代码看起来更加结构化(我加入了一些额外的功能,例如键盘和鼠标事件)。
请注意,精灵的位置将默认为 x=0, y=0
,如最后一张图片所示。您可以在 variable/handle 或创建精灵时使用 x=100
设置位置。