为什么我更新 turtle 屏幕的次数越多,turtle 屏幕的更新速度就越慢?
Why does the rate of turtle screen updates get slower as I update the turtle screen more?
我制作了一个测试脚本,使用 turtle.tracer(0)
和 turtle.update()
为动画帧设置动画框。它不断更新,但随着更新总量的增加,屏幕上每次更新的速度越来越慢。
这是我当前的代码:
import turtle as t
t.title("Animation test")
t.tracer(0)
t.hideturtle()
def home():
t.penup()
t.goto(-250,250)
t.color("black")
t.fillcolor("white")
t.pendown()
t.begin_fill()
for i in range(2):
t.forward(500); t.right(90);
t.forward(400); t.right(90);
t.end_fill()
home()
class Square:
def __init__(self):
self.x = -250
self.y = 250
self.xspeed = 5
self.yspeed = 5
def update(self):
home()
t.penup()
t.goto(self.x,self.y)
t.pendown()
if self.x > 200 or self.x < -250:
self.xspeed *= -1
if self.y > 250 or self.y < -100:
self.yspeed *= -1
t.fillcolor("red")
t.begin_fill()
for i in range(4):
t.forward(50)
t.right(90)
t.end_fill()
self.x += self.xspeed
self.y -= self.yspeed
t.update()
s = Square()
for i in range(200):
s.update()
这会导致一开始的动画速度很快,很快变慢然后停止,因为我只使用了 200 帧。我从未改变盒子的速度,但 t.update()
对屏幕产生影响的速度较慢。有什么办法可以防止这种情况发生吗?感谢您的帮助!
您用来“清除”屏幕的方式只会在屏幕上绘画。这意味着旧图纸仍然存在于所有这些旧油漆层下并减慢绘图速度。
要清除屏幕上的所有绘图,您应该在 home()
函数中使用 t.clear()
。
我制作了一个测试脚本,使用 turtle.tracer(0)
和 turtle.update()
为动画帧设置动画框。它不断更新,但随着更新总量的增加,屏幕上每次更新的速度越来越慢。
这是我当前的代码:
import turtle as t
t.title("Animation test")
t.tracer(0)
t.hideturtle()
def home():
t.penup()
t.goto(-250,250)
t.color("black")
t.fillcolor("white")
t.pendown()
t.begin_fill()
for i in range(2):
t.forward(500); t.right(90);
t.forward(400); t.right(90);
t.end_fill()
home()
class Square:
def __init__(self):
self.x = -250
self.y = 250
self.xspeed = 5
self.yspeed = 5
def update(self):
home()
t.penup()
t.goto(self.x,self.y)
t.pendown()
if self.x > 200 or self.x < -250:
self.xspeed *= -1
if self.y > 250 or self.y < -100:
self.yspeed *= -1
t.fillcolor("red")
t.begin_fill()
for i in range(4):
t.forward(50)
t.right(90)
t.end_fill()
self.x += self.xspeed
self.y -= self.yspeed
t.update()
s = Square()
for i in range(200):
s.update()
这会导致一开始的动画速度很快,很快变慢然后停止,因为我只使用了 200 帧。我从未改变盒子的速度,但 t.update()
对屏幕产生影响的速度较慢。有什么办法可以防止这种情况发生吗?感谢您的帮助!
您用来“清除”屏幕的方式只会在屏幕上绘画。这意味着旧图纸仍然存在于所有这些旧油漆层下并减慢绘图速度。
要清除屏幕上的所有绘图,您应该在 home()
函数中使用 t.clear()
。