python-分叉树枝
程序员文章站
2022-08-27 11:02:19
import turtledef draw_branch(length): #绘制右侧树枝 if length >5: if length == 10: turtle.pencolor('green') turtle.forward(length) turtle.right(20) draw_bra... ......
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()