官方文档:https://docs.python.org/zh-cn/3.9/library/turtle.html
引入turtle库:
窗口控制
1 2 3 4 5 6
| turtle.bgcolor('orange') turtle.bgpic("") turtle.clearscreen() turtle.resetscreen() turtle.screensize(10,10) turtle.setworldcoordinates()
|
动画控制
1 2 3
| turtle.tracer(0) turtle.update() turtle.delay(1000)
|
画笔控制
颜色控制
1 2 3
| turtle.color('blue') turtle.pencolor('red') turtle.fillcolor('orange')
|
填充
1 2 3
| turtle.filling() turtle.begin_fill() turtle.end_fill()
|
绘图状态
1 2 3 4 5 6 7 8 9 10 11 12
| turtle.showturtle() turtle.hideturtle()
turtle.penup() turtle.pendown() turtle.pen()
turtle.pensize(10) turtle.speed(2)
turtle.isdown() turtle.isvisible()
|
其他控制
1 2 3 4 5 6
| turtle.reset() turtle.clear() turtle.hideturtle() turtle.showturtle() turtle.write('Hello, World!', False, "left", ("Arial", 20, "normal")) turtle.done()
|
移动和绘制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| turtle.forward(100) turtle.backward(100) turtle.right(90) turtle.left(90) turtle.goto(x, y) turtle.setx(100) turtle.sety(100) turtle.setheading(90) turtle.home()
turtle.circle(100,360,6) turtle.dot(10,'red')
turtle.undo() turtle.speed(1)
|
获取当前状态
1 2 3 4 5 6
| turtle.position() turtle.towards() turtle.xcor() turtle.ycor() turtle.heading() turtle.distance(x, y)
|
设置度量单位
正方形
1 2 3
| for i in range(4): turtle.forward(100) turtle.right(90)
|
圆形
多边形
1 2 3 4 5
| n = 5 angle = 360 / n for _ in range(n): turtle.forward(100) turtle.right(angle)
|
矩形
1 2 3
| width, height = 100, 50
|
1 2 3 4 5 6 7 8 9 10 11
| x, y = turtle.pos()
turtle.forword(100)
turtle.right(90)
turtle.left(90)
turtle.done()
|