python-分叉樹枝
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()