Python中的 turtle.register_shape()函数
turtle 模块以面向对象和面向过程的方式提供海龟图形原语。因为它使用 tkinter 作为底层图形,所以它需要安装一个支持 Tk 的Python版本。
turtle.register_shape()
此函数用于将海龟形状添加到 TurtleScreen 的 shapelist。
句法 :
turtle.register_shape(name, shape=None)
参数:Arguments Description name string shape tuple of pairs of coordinates
下面是上述方法的一个例子的实现:
Python3
# import package
import turtle
# record a polygon
turtle.begin_poly()
# form a polygon
turtle.seth(-45)
turtle.circle(20, 90)
turtle.circle(10, 90)
turtle.circle(20, 90)
turtle.circle(10, 90)
turtle.end_poly()
# get polygon
pairs = turtle.get_poly()
# register shape with
# name : new_shape
# polygon : pairs
turtle.register_shape("new_shape", pairs)
# clear screen
turtle.clearscreen()
# use new shape and
# apply properties
turtle.shape("new_shape")
turtle.fillcolor("blue")
# do some motion
for i in range(50):
turtle.forward(5+2*i)
turtle.right(45)
输出 :