Commit 014b6244 by BellCodeEditor

auto save

parent 783d936d
Showing with 6 additions and 22 deletions
import turtle # 导入turtle模块
import random # 导入random模块
# 分形树
def tree(n):
if
pen = turtle.Turtle() # 创建画笔
pen.color('sienna') # 画笔颜色
pen.speed(0)
......@@ -14,27 +18,6 @@ pen.up() # 朝向正上方
pen.backward(150) # 后退150
pen.down() # 朝向正下方
# 树枝
def tree(n): # 画树枝
if n >= 0: # 设置终止条件,if条件判断,n的值大于等于0,才执行以下代码
if n <= 12: # if条件判读,n的值小于等于12,才执行以下代码
color_list = ['snow', 'lightcoral']
color = random.choice(color_list)
pen.color(color)
else:
pen.color('sienna') # 赭(zhe)色
pen.pensize(n/10) # 树枝长度除以10设置为画笔大小
pen.forward(n) # 向前移动
angle = random.random() # 角度angle
pen.right(30*angle) # 角度angle乘30作为向右旋转度数
length = 1.5*random.random() # 长度length
tree(n-10*length) # 树枝长度减10乘长度lenght
pen.left(60*angle) # 角度angle乘60作为向左旋转度数
tree(n-10*lenght)
pen.right(30*angle) # 角度angle乘30作为向右旋转度数
pen.up() # 画笔抬起
pen.backward(n) # 原路返回
pen.down() # 画笔落下
tree(60) # 设置树枝长度60
turtle.done()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment