【发布时间】:2021-01-31 16:45:58
【问题描述】:
我正在尝试使用 Python 和 Pyglet 制作 Langton 的 ant 程序。在 350 x 250 的网格中重绘所有 62500 个图块需要很长时间,所以我所做的是每当我迭代 Langton 的 Ant 部分时,我将更新的图块添加到列表中,当调用 on_draw() 时,我绘制更新矩形并清除列表。
@window.event
def on_draw():
Ant.batch.draw() # Draw the grid spaces that were changed
Ant.buffer = [] # Clear the updated grid space buffer
def update(dt):
my_ant.iterate(1) # Run one iteration of Langton's Ant
pyglet.clock.schedule_interval(update, 1/1000)
pyglet.app.run()
但是,这会导致输出闪烁、颜色混合以及模拟运行的速度几乎没有尽可能快。我还尝试不将on_draw() 绑定到window.event 循环中,而是每秒调用60 次,但它导致了同样的闪烁和速度问题。
def draw(): # Don't tie drawing the screen into the event loop
Ant.batch.draw() # Draw the grid spaces that were changed
Ant.buffer = [] # Clear the updated grid space buffer
def update(dt):
my_ant.iterate(1) # Run one iteration of Langton's Ant
pyglet.clock.schedule_interval(update, 1/1000)
pyglet.clock.schedule_interval(draw, 1/60) # Update the screen 60 times a second
pyglet.app.run()
我真的很喜欢第二种方法对帧时间和模拟的控制,所以如果有任何方法可以保持相同的通用方法但修复闪烁,那就太好了。 我是 python/pyglet 的新手,所以如果有任何方法可以在不编写自定义事件循环的情况下做我想做的事,那也很好。
Here 是所有代码的链接,它只需要安装 pyglet。提前致谢!
编辑:添加链接
【问题讨论】: