入门

import turtle
t=turtle.Pen()
for i in range(4):
	t.forward(100)
	t.left(4)
t.reset()
i = 0
while i < 4:
	t.backward(100)
	t.right(90)
	i+=1

正方形

from turtle import *
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
forward(100)
left(90)
exitonclick()

等边三角形

from turtle import *
forward(100)
left(120)
forward(100)
left(120)
forward(100)
left(120)
exitonclick()#点击屏幕后再停止

五角星

from turtle import *
for i in range(5):
    forward(100)
    left(180-36)
exitonclick()#点击屏幕后再停止

向日葵

from turtle import *
from math import sin
angle = 360/8
length=100
speed(0) #画图的速度提到最快
for i in range(8):
  
    if i %2==0:
        color('red')
    else:
        color('blue')
    begin_fill()
    forward(length)
    left(angle)
    forward(length)
    left(180-angle)
    forward(length)
    left(angle)
    forward(length)
    left(180)
    end_fill()
angle2=180-(180-angle)/2.0
forward(length)
left(angle2)
alpha=angle*3.1415926536/180
step =2*length*sin(alpha/2)
color('yellow')
begin_fill()
for i in range(8):
    forward(step)
    left(angle)
end_fill()
exitonclick()#点击屏幕后再停止

L系统

from turtle import *
length = 10
angle = 90
def draw_path(path):
    for symbol in path:
        if symbol =='F':
            forward(length)
        elif symbol =='-':
            left(angle)
        else:
            right(angle)
def apply_rule(path):
    rule = "F-F+F+FF-F-F+F"
    return path.replace("F",rule) #把F换成rule
path ='F-F-F-F'


speed(0)
for i in range(2):
    path = apply_rule(path)
    draw_path(path)
exitonclick()

复杂L系统

from turtle import *
length = 10
angle = 90
def split_path(path):
    i = 0
    lst =[]
    while i < len(path):
        if path[i] =='F':
            lst.append(path[i :i+2])
            i+=2
        else:
            lst.append(path[i])
            i+=1
    return lst
def apply_rule(path,rules):
    lst =split_path(path)
    for i in range(len(lst)):
        symbol = lst[i]
        if symbol in rules:
            lst[i] = rules[symbol]
    path = "".join(symbol for  symbol in lst)
    return path
def  draw_path(path):
    lst =split_path(path)
    for symbol in lst:
        if symbol =='Fl' or symbol == 'Fr':
            forward(length)
        elif symbol =="+":
            left(angle)
        else:
            right(angle)
rules={
    "Fl":"Fl+Fr+",
    "Fr":"-Fl-Fr"
}

path ='Fl+Fr+'
speed(0)
for i in range(10):
    path = apply_rule(path,rules)
    draw_path(path)
exitonclick()