Search code examples
pythonpython-3.xpyglet

pyglet - weird gray border on image


I'm writing a simple game in pyglet (python 3). I have this cloud sprite.

However, when I blit it, it looks like this.

The gray border obviously isn't intended, so I'm wondering how to fix it.

This is what I'm using to enable blitting an image with alpha:

from pyglet import gl    

@window.event
def on_draw():
    glEnable(GL_BLEND)
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

Any help is appreciated. Thanks in advance.


Solution

  • I'm assuming you're using regular resource objects and you're blitting them one by one using image.blit(X, Y).
    This works, but requires a lot of more work and I think processing time as well as you have to set and reset alpha channels for each image you blit.

    However, if you use sprites alpha channels becomes a automated feature and you don't have to worry to much about it.

    This solves the problem, and is also a speed enhancer in the long run:

    import pyglet
    import pyglet.clock
    from pyglet.gl import *
    
    window = pyglet.window.Window()
    window.config.alpha_size = 8
    
    bg = pyglet.sprite.Sprite(pyglet.image.load('bg.png'))
    cloud = pyglet.sprite.Sprite(pyglet.image.load('cloud.png'))
    
    @window.event
    def on_draw():
        window.clear()
        bg.draw()
        cloud.draw()
    
    pyglet.app.run()