python-分叉树枝

发布时间:2019-05-20 22:49:55编辑:auto阅读(2308)

    import turtle
    
    def draw_branch(length):
        #绘制右侧树枝
      if length >5:
        if length == 10:
              turtle.pencolor('green')
        turtle.forward(length)
        turtle.right(20)
        draw_branch(length-15)
        #绘制左侧树枝
        turtle.left(40)
        draw_branch(length-15)
        #返回之前树枝
        turtle.right(20)
        turtle.backward(length)
        if length == 10:
            turtle.pencolor('yellow')
        else:
            turtle.pencolor('red')
    def main():
    
        turtle.pencolor('red')
        turtle.pensize(5)
        turtle.left(90)
        turtle.penup()
        turtle.backward(150)
        turtle.pendown()
        draw_branch(100)
        turtle.exitonclick()
    if __name__=='__main__':
        main()

     



     



关键字