Python中的 turtle.tracer()函数
turtle 模块以面向对象和面向过程的方式提供海龟图形原语。因为它使用 Tkinter 作为底层图形,所以它需要安装一个支持 Tk 的Python版本。
海龟.tracer()
此函数用于打开或关闭海龟动画并设置更新图纸的延迟。
Syntax : turtle.tracer(n=None, delay=None)
Parameters:
- n: If n is given, only each n-th regular screen update is really performed.
- delay: sets delay value
以下是上述方法的实现以及一些示例:
示例 1:
Python3
# importing package
import turtle
# check default value
print(turtle.tracer())
Python3
# importing package
import turtle
# loop for motion with
# default tracer as 1
for i in range(20):
turtle.forward(1+1*i)
turtle.right(45)
# set tracer values as (2,0)
# 2 -> for screen update
# 0 -> delay
turtle.tracer(n=2, delay=0)
# loop for motion with
# above tracer values
for i in range(20, 40):
turtle.forward(1+1*i)
turtle.right(45)
# set tracer values as (1,50)
# 1 -> for screen update
# 50 -> delay
turtle.tracer(n=1, delay=50)
# loop for motion with
# above tracer values
for i in range(40, 60):
turtle.forward(1+1*i)
turtle.right(45)
输出 :
1
示例 2:
Python3
# importing package
import turtle
# loop for motion with
# default tracer as 1
for i in range(20):
turtle.forward(1+1*i)
turtle.right(45)
# set tracer values as (2,0)
# 2 -> for screen update
# 0 -> delay
turtle.tracer(n=2, delay=0)
# loop for motion with
# above tracer values
for i in range(20, 40):
turtle.forward(1+1*i)
turtle.right(45)
# set tracer values as (1,50)
# 1 -> for screen update
# 50 -> delay
turtle.tracer(n=1, delay=50)
# loop for motion with
# above tracer values
for i in range(40, 60):
turtle.forward(1+1*i)
turtle.right(45)
输出 :