学习完了turtle模块,花了两小时画了一幅樱花,希望大家喜欢。
迭代过程有点头疼,需要耐心思考,动笔画一下。
源代码如下:
import turtle
import random as rm
angle1 = [55, 35, 45]
angle2 = [15, 30, 45]
size = [3, 5, 7, 9]
color2 = ['pink']
color = ['#00FF00', '#00CED1', '#FFD700', '#4A708B', '#FFE4B5']
def treebranch(branchLen, t, ag):
t.right(ag)
# t.color(cr)
t.forward(branchLen)
tree(branchLen, t)
t.backward(branchLen)
t.left(ag)
# t.color(cr)
t.forward(branchLen)
tree(branchLen, t)
t.backward(branchLen)
t.left(ag)
# t.color(cr)
t.forward(branchLen)
tree(branchLen, t)
t.backward(branchLen)
t.right(ag)
t.pensize(branchLen / 10)
def tree(branchLen, t):
cr = rm.choice(color)
t.pensize(3)
if branchLen > 105:
# ag = rm.choice(35)
treebranch(branchLen - 5, t, 35)
elif branchLen > 90:
ag = rm.choice(angle1)
treebranch(branchLen-15, t, ag)
elif branchLen > 50:
ag = rm.choice(angle2)
treebranch(branchLen-10, t, ag)
else:
# 画?
t.color('pink')
t.fillcolor('pink')
t.begin_fill()
sz = rm.choice(size)
t.circle(sz, 360)
t.end_fill()
t.color(cr)
def main():
t = turtle.Turtle()
myWin = turtle.Screen()
t.left(90)
t.up()
t.backward(150)
t.down()
# t.speed(50)
turtle.tracer(0)
tree(110, t)
turtle.update()
myWin.exitonclick()
main()
关于turtle的教程,网上很多,我就不做具体介绍。
我参考的是这篇:https://www.jianshu.com/p/7118a1784f46