简体   繁体   English

Pyglet:批量渲染多个对象

[英]Pyglet : Render multiple object in a batch

I would like to know in pyglet how to render multiple object in a batch.我想知道在 pyglet 中如何批量渲染多个对象。

I have try to edit init from the ball class but i don't success to change it How can I access to the init because if i put batch=batch it create an error.我尝试从 ball 类编辑 init 但我没有成功更改它我如何访问 init 因为如果我放置 batch=batch 它会创建一个错误。 I don't know if its clear...不知道清楚了没有...

Here is my code and i have this following error :这是我的代码,我有以下错误:

__init__() got an unexpected keyword argument 'batch'

my code我的代码

from pyglet import *
from pyglet.gl import *
import particles as prt

# Indented oddly on purpose to show the pattern:
UP    = 0b0001
DOWN  = 0b0010
LEFT  = 0b0100
RIGHT = 0b1000

class ball(pyglet.sprite.Sprite):
    def __init__(self, parent, image='Red_Circle.png', x=0, y=0):
        self.texture = pyglet.image.load(image)
        pyglet.sprite.Sprite.__init__(self, self.texture, x=x, y=y)
        self.parent = parent
        self.direction = UP | RIGHT # Starting direction


    def update(self,scalef=None):

        if scalef is not None:
            self._scale = scalef
        # We can use the pattern above with bitwise operations.
        # That way, one direction can be merged with another without collision.
        if self.direction & UP:
            self.y += 1
        if self.direction & DOWN:
            self.y -= 1
        if self.direction & LEFT:
            self.x -= 1
        if self.direction & RIGHT:
            self.x += 1

        if self.x+self.width > self.parent.width:
            self.direction = self.direction ^ RIGHT # Remove the RIGHT indicator
            self.direction = self.direction ^ LEFT # Start moving to the LEFT
        if self.y+self.height > self.parent.height:
            self.direction = self.direction ^ UP # Remove the UP indicator
            self.direction = self.direction ^ DOWN # Start moving DOWN
        if self.y < 0:
            self.direction = self.direction ^ DOWN
            self.direction = self.direction ^ UP
        if self.x < 0:
            self.direction = self.direction ^ LEFT
            self.direction = self.direction ^ RIGHT

    def render(self):
        self.draw()

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

        batch = pyglet.graphics.Batch()
        ball_sprites = []
        for i in range(100):
            x, y = i * 10, 50
            ball1= ball(x, y)
            ball_sprites.append(ball1)
            print(ball_sprites)




        self.ball = ball(self, x=100, y=100)
        self.alive = 1

    def on_draw(self):
        self.render()

    def on_close(self):
        self.alive = 0

    def render(self):
        
        self.clear()
        self.ball.update(0.01)
        self.ball.update()
        self.ball.render()
        
        self.flip()

    def run(self):
        while self.alive == 1:
            self.render()
            event = self.dispatch_events()

if __name__ == '__main__':
    x = main()
    x.run()

Thanks !谢谢 !

There are quite a few things wrong with your current setup.您当前的设置有很多问题。

You need to pass the batch to the ball and your init needs to support it.您需要将批次传递给球,并且您的 init 需要支持它。

You don't need your run function.您不需要run功能。 Use pyglet.app.run() , this handles all of the things automatically.使用pyglet.app.run() ,它会自动处理所有事情。 From your setup it looks like you are drawing rendering twice or more.从您的设置来看,您似乎正在绘制渲染两次或更多次。

Make sure you are allowing batch on both init and the sprite init.确保在 init 和 sprite init 上都允许批处理。

    def __init__(self, parent, image='Red_Circle.png', x=0, y=0, batch=batch):
        self.texture = pyglet.image.load(image)
        pyglet.sprite.Sprite.__init__(self, self.texture, x=x, y=y, batch=batch)

Then make sure you are passing batch when you create a ball .然后确保在创建ball时传递批次。

It might be better for you to see an actual example: https://github.com/pyglet/pyglet/blob/ae8228d56ed6f0855f6280cc44b22d057d92dc4d/examples/media/noisy/noisy.py看一个实际例子可能会更好: https : //github.com/pyglet/pyglet/blob/ae8228d56ed6f0855f6280cc44b22d057d92dc4d/examples/media/noisy/noisy.py

Here is an example using Pyglet with bouncing balls and uses a batch.这是一个使用带有弹跳球的 Pyglet 并使用批处理的示例。

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

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