樱花-python
1.导入库
2.基本框架
3.定义花瓣和树主干的函数
import turtle
import time
import random
t = turtle.Turtle()
w = turtle.Screen()
t.up()
t.left(90)
t.backward(200)
t.down()
w.screensize(bg='wheat')
t.color('sienna')
def petal(times,t):
for i in range(times):
a = 200 - 400 * random.random()
b = 10 - 20 * random.random()
print(a,b)
t.up()
t.forward(b)
t.left(90)
t.forward(a)
t.down()
t.color('lightcoral')
t.circle(1)
t.up()
t.backward(a)
t.right(90)
t.backward(b)
t.down()
def body_draw(branchlen,t):
time.sleep(0.005)
if branchlen >3:
if 8<=branchlen<=12:
if random.randint(0,2) == 0:
t.color('snow')
else:
t.color('lightcoral')
t.pensize(branchlen / 3)
elif branchlen < 8:
if random.randint(0,1) == 0:
t.color('snow')
else:
t.color('lightcoral')
t.pensize(branchlen / 2)
else:
t.color('sienna')
t.pensize(branchlen / 10)
t.forward(branchlen)
a = 1.5 * random.random()
t.right(20 * a)
b = 1.5 * random.random()
body_draw(branchlen - 10 * b,t)
t.left(40 * a)
body_draw(branchlen - 10 * b,t)
t.right(20 * a)
t.up()
t.backward(branchlen)
t.down()
body_draw(60,t)
网友评论