turtle官网:https://docs.python.org/3/library/turtle.html。这个库被介绍为一个最常用的用来给孩子们介绍编程知识的方法库,其主要是用于程序设计入门,是标准库之一,利用turtle可以制作很多复杂的绘图。
下面是几个简单案例:
1. 画三角形
from turtle import forward, right
def triangle(sidelength):
for i in range(3):
forward(sidelength)
right(120)
triangle(300)
2. 画正方形并向右旋转
from turtle import forward, right
def square():
for i in range(4):
forward(100)
right(90)
for i in range(60):
square()
right(10)
3. 画多边形
from turtle import forward, right
def polygon(sides):
for i in range(sides):
forward(100)
right(360/sides)
# 六边形传个6就行,几边形就传几
polygon(6)
4. 画螺旋图案
画正方形并向右旋转,边长依次递增。
from turtle import forward, right
def square(sidelength):
for i in range(4):
forward(sidelength)
right(90)
def spiral():
length = 5
for i in range(60):
square(length)
right(5)
length += 5
spiral()
5. 画五角星并旋转
from turtle import forward, right
def star(sidelength):
for i in range(5):
forward(sidelength)
right(144)
def star_spiral():
length = 5
for i in range(60):
star(length)
right(5)
length += 5
star_spiral()
6. 画5个五角星
from turtle import *
def drawStar(x, y):
pu()
goto(x, y)
pd()
# set heading: 0
seth(0)
for i in range(5):
fd(40)
rt(144)
for x in range(0, 250, 50):
drawStar(x, 0)
done()