一、科赫曲线
运用递归,一阶一阶分隔,如2阶的每条小线是1阶(所以代码为n-1)
import turtle
def koch(size,n):
if n == 0:
turtle.fd(size)
else:
for angle in [0,60,-120,60]:
turtle.left(angle)
koch(size/3,n-1)
def main():
turtle.setup(800,400)
turtle.penup()
turtle.goto(-300,-50)
turtle.pendown()
turtle.pensize(2)
koch(600,3) #3阶,设置阶数
turtle.hideturtle()
main()
二、科赫雪花
import turtle
def koch(size,n):
if n == 0:
turtle.fd(size)
else:
for angle in [0,60,-120,60]:
turtle.left(angle)
koch(size/3,n-1)
def main():
turtle.setup(800,800)
turtle.penup()
turtle.goto(-300,-50)
turtle.pendown()
turtle.pensize(2)
level = 3
koch(400,level)
turtle.right(120) #向右旋转120度
koch(400,level)
turtle.right(120)
koch(400,level)
turtle.hideturtle()
main()
三、打包
ico在线制作或免费下载 https://www.easyicon.net/
cmd命令:进入文件所在目录
pyinstaller -i bauble.ico -F koch.py #bauble.ico 下载的图标文件