我正在使用 Pyglet 開發(fā)一個簡單的燭臺圖表繪圖程序。當(dāng)我嘗試在一個循環(huán)中批處理形狀時,pyglet 只繪制第一個形狀(我認(rèn)為)。我已經(jīng)包含了一些最少的代碼來解釋我的問題。這段代碼應(yīng)該在窗口上顯示 10 個又細(xì)又長的矩形,但我只得到一個矩形。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 批處理繪制圖形數(shù)據(jù)的計劃,我在這個假設(shè)中是否正確?
1 回答

明月笑刀無情
TA貢獻(xiàn)1828條經(jīng)驗 獲得超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
提交
取消