绘制分形树

import turtle


def draw_branch(branch_length):
    """
    绘制分形树

    """
    if branch_length > 5:
        # 绘制右侧的树枝
        turtle.forward(branch_length)
        print('向前', branch_length)
        turtle.right(20)
        print('向右 20')
        draw_branch(branch_length - 15)

        # 绘制左侧树枝
        turtle.left(40)
        print('向左 20')
        draw_branch(branch_length - 15)

        # 返回之前的树枝
        turtle.right(20)
        print('向右 20')
        turtle.backward(branch_length)
        print('向右 20', branch_length)


def main():
    """
    主函数

    """
    turtle.left(90)
    turtle.penup()
    turtle.backward(150)
    turtle.pendown()
    turtle.pencolor('purple')
    draw_branch(100)
    turtle.exitonclick()


if __name__ == '__main__':
    main()

 

posted @ 2018-10-21 22:10  逐梦武威  阅读(152)  评论(0编辑  收藏  举报