- 积分
- 224
- 贡献
-
- 精华
- 在线时间
- 小时
- 注册时间
- 2017-9-10
- 最后登录
- 1970-1-1
|
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
# -*-coding:utf-8-*-
import turtle
def draw_branch(branch_length):
"""
绘制树干
"""
if branch_length > 5:
if branch_length < 40:
turtle.color('green')
turtle.pensize(1)
else:
turtle.color('red')
turtle.pensize(1.5)
# 绘制右侧树枝
turtle.forward(branch_length)
print '向前', branch_length
turtle.right(25)
print ('右转 25')
draw_branch(branch_length-15)
# 绘制左侧树枝
turtle.left(50)
print ('左转 50')
draw_branch(branch_length-15)
if branch_length < 40:
turtle.color('green')
else:
turtle.color('red')
turtle.pensize(1.5)
# 返回之前的树枝
turtle.right(25)
print ('右转 25')
turtle.backward(branch_length)
print '向后', branch_length
def main():
"""
绘制分形树
"""
turtle.left(90)
turtle.penup()
turtle.backward(150)
turtle.down()
draw_branch(100)
turtle.exitonclick()
if __name__ == '__main__':
main()
|
|