1、递归及旋转圆形的画法
import turtle
import random
pen = turtle.Turtle()
turtle.colormode(255)
pen.speed(0)
def circle(radius):
if radius == 200:
return
else:
pen.color(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
pen.circle(radius)
pen.right(2)
circle(radius + 1)
circle(50)
turtle.done()
2、递归及分形树的画法
import turtle
pen = turtle.Turtle()
turtle.colormode(255)
pen.speed(0)
r, g, b = 0, 0, 0
length = 120
pen_size = 14
pen.pensize(pen_size)
pen.penup()
pen.left(90)
pen.backward(length)
pen.pendown()
pen.forward(length)
def draw_tree(l, lv):
l = l * 3/4
size = pen.pensize()
pen.pensize(size * 3/4)
global r, g, b
r += 1
g += 2
b += 3
pen.color(r % 200, g % 200, b % 200)
pen.left(45)
pen.forward(l)
if lv < 14:
draw_tree(l, lv + 1)
pen.backward(l)
pen.right(90)
pen.forward(l)
if lv < 14:
draw_tree(l, lv + 1)
pen.backward(l)
pen.left(45)
pen.pensize(size)
draw_tree(length, 13)
turtle.done()