海龟作图---用Python绘图
#向前移动距离degree代表距离
import turtle
turtle.width(15) #画笔粗细
turtle.color(“blue”)
turtle.circle(50)
turtle.penup()
turtle.goto(120,0)
turtle.down()
turtle.color(“black”)
turtle.circle(50)
turtle.penup()
turtle.goto(240,0)
turtle.down()
turtle.color(“red”)
turtle.circle(50)
turtle.penup()
turtle.goto(60,-50)
turtle.down()
turtle.color(“yellow”)
turtle.circle(50)
turtle.penup()
turtle.goto(180,-50)
turtle.down()
turtle.color(“green”)
turtle.circle(50)
from turtle import *
# 设置色彩模式是RGB:
colormode(255)
lt(90)
lv = 14
l = 120
s = 45
width(lv)
# 初始化RGB颜色:
r = 0
g = 0
b = 0
pencolor(r, g, b)
penup()
bk(l)
pendown()
fd(l)
def draw_tree(l, level):
global r, g, b
# save the current pen width
w = width()
# narrow the pen width
width(w * 3.0 / 4.0)
# set color:
r = r + 1
g = g + 2
b = b + 3
pencolor(r % 200, g % 200, b % 200)
l = 3.0 / 4.0 * l
lt(s)
fd(l)
if level < lv:
draw_tree(l, level + 1)
bk(l)
rt(2 * s)
fd(l)
if level < lv:
draw_tree(l, level + 1)
bk(l)
lt(s)
# restore the previous pen width
width(w)
speed("fastest")
draw_tree(l, 4)
done()
显示效果:执行上述程序需要花费一定的时间,最后的效果如下