简体   繁体   中英

Loading multiple PNGs in pyglet window

If I loaded two partially transparent PNGs in succession (onDraw), would I still be able to see the first image if the second image was transparent in that area? These images would be drawn into a window. I would use Sprites but I couldn't get them to work. Thanks!

There's no code in your question and quite frankly there's so many solutions to what I think is your problem that you could have at least 10 different answers here.

I'll vote to close this question but seeing as these are so rare and they usually don't get much response on either closing requests or down votes enough for people to care.. I'll give a example snippet of code that might get you on your way towards a working solution:

import pyglet
from pyglet.gl import *

class main(pyglet.window.Window):
    def __init__ (self):
        super(main, self).__init__(300, 300, fullscreen = False)
        self.alive = 1

        self.image_one = pyglet.sprite.Sprite(pyglet.image.load('./image_one.png'))
        self.image_two = pyglet.sprite.Sprite(pyglet.image.load('./image_two.png'))

        self.image_one.x = 100
        self.image_one.y = 100

        self.image_two.x = 70
        self.image_two.y = 80

    def on_draw(self):
        self.render()

    def on_close(self):
        self.alive = 0

    def render(self):
        self.clear()

        self.image_one.draw()
        self.image_two.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()

x = main()
x.run()

I used these two images:

  1. 在此处输入图片说明

  2. 在此处输入图片说明

Nothing fancy but it will give you an example of how to render two images using the Sprite class.

Here is some sample code that I wrote:

import  pyglet

window = pyglet.window.Window()
one_image = pyglet.image.load("one.png") 
two_image = pyglet.image.load("two.png") 
one = pyglet.sprite.Sprite(one_image)
two = pyglet.sprite.Sprite(two_image)


@window.event
def on_draw():
    one.x = 0
    one.y = 0
    one.draw()
    two.x = 365
    two.y = 305
    two.draw()

pyglet.app.run()

one.png and two.png being transparent and not the same shape, I was able to see two.png overlaid on one.png.

and the result is 这里

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM