作为一个世界上非著名非专业的画家,当然得让大家先欣赏下我的世纪大作:
当然作为一个知名画家必须得掌握不同的画风,现在大家欣赏下我的另一大作:
众所周知,作为一个程序员,当然不会是一个手残党,日夜敲击键盘和晚上去厕所锻炼出来的手速,画这么个樱花树不是分分钟?这棵樱花树,我们使用了python的turtle模块,利用一些数学函数,将随机的画出不同的树枝以及飘落的花瓣,我们每次运行都会生成一棵相对独一无二的树(假如随机数不会相同)。废话不多说,看源码吧:
import turtle
from random import *
from math import *
def tree(n, l):
turtle.pendown()
t = cos(radians(turtle.heading() + 45)) / 8 + 0.25
turtle.pencolor(t, t, t)
turtle.pensize(n / 3)
turtle.forward(l)
if n > 0:
b = random() * 15 + 10
c = random() * 15 + 10
d= l * (random() * 0.25 + 0.7)
turtle.right(b)
tree(n - 1, d)
turtle.left(b + c)
tree(n - 1, d)
turtle.right(c)
else:
turtle.right(90)
n = cos(radians(turtle.heading() - 45)) / 4 + 0.5
turtle.pencolor(n, n*0.8, n*0.8)
turtle.circle(3)
turtle.left(90)
z=random()
if z>0.7:
turtle.up()
t = turtle.heading()
an = -40 + random()*40
turtle.setheading(an)
dis = int(800*random()*0.5 + 400*random()*0.3 + 200*random()*0.2)
turtle.forward(dis)
turtle.setheading(t)
turtle.down()
turtle.right(90)
n = cos(radians(turtle.heading() - 45)) / 4 + 0.5
turtle.pencolor(n*0.5+0.5, 0.4+n*0.4, 0.4+n*0.4)
turtle.circle(2)
turtle.left(90)
turtle.up()
t = turtle.heading()
turtle.setheading(an)
turtle.backward(dis)
turtle.setheading(t)
turtle.up()
turtle.backward(l)
turtle.bgcolor(0.5, 0.5, 0.5) # 背景色
turtle.hideturtle() # 隐藏turtle
turtle.speed(0) # 速度,1-10渐进,0最快
turtle.tracer(0, 0)
turtle.up() # 抬笔
turtle.backward(100)
turtle.left(90) # 左转90度
turtle.up() # 抬笔
turtle.backward(300) # 后退300
tree(12, 100) # 递归7层
turtle.done()