我做了一個測驗腳本,使用turtle.tracer(0)和turtle.update()來制作一個盒子的影片。它不斷地更新,但隨著更新總量的增加,螢屏上每個更新的速度越來越慢。
以下是我目前的代碼:
import turtle as t
t.title("影片測驗")
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()
首頁()
class Square。
def __init__(self):
self.x = -250.
self.y =250。
self.xspeed = 5
self.yspeed =5
def update(self)。
首頁()
t.penup()
t.goto(self.x,self.y)
t.pendown()
if self.x > 200 or self.x < -250:
self.xspeed *= -1: self.xspeed *= -1.
if self.y > 250 or self.y < -100:
self.yspeed *= -1: 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()對螢屏產生影響的速度卻比較慢。有什么方法可以防止這種情況嗎?謝謝你的幫助!
uj5u.com熱心網友回復:
你用來 "清除 "螢屏的方法只是在它上面畫畫。這意味著在所有這些舊的油漆層下,舊的繪圖仍然存在,并減緩了繪圖的速度。
為了清除螢屏上的所有繪圖,你應該在你的home()函式中使用t.clear()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/319380.html
標籤:
下一篇:圍繞Svg路徑順時針閃爍顏色
