简体   繁体   English

在pyglet窗口中加载多个PNG

[英]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? 如果我连续加载了两个部分透明的PNG(onDraw),如果第二个图像在该区域是透明的,我仍然能够看到第一个图像吗? These images would be drawn into a window. 这些图像将被绘制到窗口中。 I would use Sprites but I couldn't get them to work. 我会使用Sprites,但无法使它们工作。 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. 您的问题中没有代码,坦率地说,我认为您的问题有很多解决方案,因此您至少可以有10个不同的答案。

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. 没什么,但是它将为您提供一个如何使用Sprite类渲染两个图像的示例。

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. one.png和two.png是透明的并且形状不相同,我能够看到two.png覆盖在one.png上。

and the result is 结果是 这里

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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