簡體   English   中英

Pyglet:批量渲染多個對象

[英]Pyglet : Render multiple object in a batch

我想知道在 pyglet 中如何批量渲染多個對象。

我嘗試從 ball 類編輯 init 但我沒有成功更改它我如何訪問 init 因為如果我放置 batch=batch 它會創建一個錯誤。 不知道清楚了沒有...

這是我的代碼,我有以下錯誤:

__init__() got an unexpected keyword argument 'batch'

我的代碼

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()

謝謝 !

您當前的設置有很多問題。

您需要將批次傳遞給球,並且您的 init 需要支持它。

您不需要run功能。 使用pyglet.app.run() ,它會自動處理所有事情。 從您的設置來看,您似乎正在繪制渲染兩次或更多次。

確保在 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)

然后確保在創建ball時傳遞批次。

看一個實際例子可能會更好: https : //github.com/pyglet/pyglet/blob/ae8228d56ed6f0855f6280cc44b22d057d92dc4d/examples/media/noisy/noisy.py

這是一個使用帶有彈跳球的 Pyglet 並使用批處理的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM