Turtle库是Python语言中一个很流行的绘制图像的函数库,想象一个小乌龟,在一个横轴为x、纵轴为y的坐标系原点,(0,0)位置开始,它根据一组函数指令的控制,在这个平面坐标系中移动,从而在它爬行的路径上绘制了图形。
练习代码,在一张图内绘制螺旋线、太阳花、五角星。
# coding=utf-8
import turtle
import time
# 同时设置pencolor=color1, fillcolor=color2
turtle.color("green", "yellow")
turtle.speed("fastest")
turtle.pensize(2)
for x in range(100):
turtle.forward(2*x) #前进2x 点
turtle.left(90) #逆时针转动90度
time.sleep(1)
turtle.goto(100,100)
turtle.speed(5)
turtle.pensize(1)
turtle.pencolor("blue")
turtle.begin_fill()
for _ in range(50):
turtle.forward(200)
turtle.left(170) #逆时针转动170度
turtle.end_fill()
turtle.penup() #抬笔
turtle.goto(300,-100)
turtle.pendown() #落笔画
turtle.pensize(5)
turtle.pencolor("yellow")
turtle.fillcolor("red")
turtle.begin_fill()
for _ in range(5):
turtle.forward(200)
turtle.right(144) #顺时针转动144度,
turtle.end_fill()
time.sleep(2)
turtle.penup() #抬笔
turtle.goto(-150,-120)
turtle.color("violet")
turtle.write("Done", font=('Arial', 40, 'normal'))
turtle.mainloop()