为了账号安全,请及时绑定邮箱和手机立即绑定

在循环中使用并添加到 pygame 批处理

在循环中使用并添加到 pygame 批处理

HUWWW 2023-06-06 16:42:47
我正在使用 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)]



查看完整回答
反对 回复 2023-06-06
  • 1 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信