turtle.write方法使用说明

turtle.write方法使用说明

 

关于turtle可参见 Python的turtle模块https://blog.csdn.net/cnds123/article/details/108252863

 

turtle.write()方法

在当前乌龟位置写入文本。使用格式:

write(arg,move=false,align='left',font=('arial',8,'normal'))

参数说明

arg:信息,将写入Turtle绘画屏幕。

move(可选):真/假。在默认情况下,move为false。如果move为true,则笔将移动到右下角。

align(可选):字符串“左(left)”、“中(center)”或“右(right)”之一。

font(可选):三个字体(fontname、fontsize、fonttype)。

扫描二维码关注公众号,回复: 12831576 查看本文章

例子

import turtle

info = "你输入的文字"

turtle.penup()

turtle.fd(-300)

turtle.pencolor('red')

for i in info:

    turtle.write(i, font=('Arial',40,'normal'))

    turtle.fd(60)

turtle.hideturtle()

运行效果如下:

 

绘制一朵小花的例子

import turtle as t

t.penup()

t.fd(-200)

t.write("一朵小花\n", align="right", font=("楷体", 16, "bold"))

 

def draw_leaf():

    for i in range(2):

        for j in range(15):

            t.forward(5)

            t.right(6)

        t.right(90)

     

t.goto(0,-150)

t.left(90)

t.down()

t.forward(50)

t.fillcolor("green")

t.begin_fill()

draw_leaf()

t.end_fill()

t.forward(50)

t.right(270)

t.fillcolor("green")

t.begin_fill()

draw_leaf()

t.end_fill()

t.right(90)

t.forward(130)

t.fillcolor("red")

t.begin_fill()

for i in range(6):

    draw_leaf()

    t.right(60)

t.end_fill()

t.down()

运行效果如下:

 

如何使用turtle.write方法将文字显示为一个圆圈?

可近似地将画笔的运动轨迹看为一个正多边形。

根据多边形内角和公式:度数=(边数-2)*180,

那么,每次旋转的度数为:180-度数/角数=180-(边数-2)*180/边数。

易知,边数=角数=文字数

所以每次旋转的度数为:180-(文字数-2)*180/文字数=360/文字数。

例如

#将文字显示为一个圆圈

import turtle

text="你要显示的文字"

turtle.pu()

x=len(text)

for i in text:

    turtle.write(i,font='consolas')

    turtle.rt(360/x)

    turtle.pu()

    turtle.fd(30)

turtle.hideturtle()

运行效果如下:

 

猜你喜欢

转载自blog.csdn.net/cnds123/article/details/113915180