我正在使用 Pyglet 開發一個簡單的燭臺圖表繪圖程序。當我嘗試在一個循環中批處理形狀時,pyglet 只繪制第一個形狀(我認為)。我已經包含了一些最少的代碼來解釋我的問題。這段代碼應該在窗口上顯示 10 個又細又長的矩形,但我只得到一個矩形。import pygletfrom pyglet import shapeswindow = pyglet.window.Window(960, 540)batch = pyglet.graphics.Batch()for i in range(10): rectangle = shapes.Rectangle(10*i, 100, 5, 100, color=(0,255,0), batch=batch)@window.eventdef on_draw(): window.clear() batch.draw()pyglet.app.run()print(batch)這樣的事情很好用:rectangle1 = shapes.Rectangle(10, 100, 5, 100, color=(0,255,0), batch=batch)rectangle2 = shapes.Rectangle(20, 100, 5, 100, color=(0,255,0), batch=batch)rectangle3 = shapes.Rectangle(30, 100, 5, 100, color=(0,255,0), batch=batch)rectangle4 = shapes.Rectangle(40, 100, 5, 100, color=(0,255,0), batch=batch)rectangle5 = shapes.Rectangle(50, 100, 5, 100, color=(0,255,0), batch=batch)但這不會:rectangle = shapes.Rectangle(10, 100, 5, 100, color=(0,255,0), batch=batch)rectangle = shapes.Rectangle(20, 100, 5, 100, color=(0,255,0), batch=batch)rectangle = shapes.Rectangle(30, 100, 5, 100, color=(0,255,0), batch=batch)rectangle = shapes.Rectangle(40, 100, 5, 100, color=(0,255,0), batch=batch)rectangle = shapes.Rectangle(50, 100, 5, 100, color=(0,255,0), batch=batch)這對我來說意味著批處理對象只是指批處理中的形狀對象,這將使我無法使用 pyglet 批處理繪制圖形數據的計劃,我在這個假設中是否正確?
1 回答

明月笑刀無情
TA貢獻1828條經驗 獲得超4個贊
我建議將形狀添加到列表中:
rectangles = [] for i in range(10): rectangles.append(shapes.Rectangle(10*i, 100, 5, 100, color=(0,255,0), batch=batch))
分別
rectangles = [shapes.Rectangle(10*i, 100, 5, 100, color=(0,255,0), batch=batch) for i in range(10)]
添加回答
舉報
0/150
提交
取消