简体   繁体   中英

Pyglet : Render multiple object in a batch

I would like to know in pyglet how to render multiple object in a batch.

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. 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.

You don't need your run function. Use pyglet.app.run() , this handles all of the things automatically. 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.

    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 .

It might be better for you to see an actual example: 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.

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