tree.py 690 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from turtle import *
  2. colormode(255)
  3. lt(90)
  4. lv = 14
  5. l = 120
  6. s = 45
  7. width(lv)
  8. r = 0
  9. g = 0
  10. b = 0
  11. pencolor(r, g, b)
  12. penup()
  13. bk(l)
  14. pendown()
  15. fd(l)
  16. def draw_tree(l, level):
  17. global r, g, b
  18. # save the current pen width
  19. w = width()
  20. # narrow the pen width
  21. width(w * 3.0 / 4.0)
  22. # set color:
  23. r = r + 1
  24. g = g + 2
  25. b = b + 3
  26. pencolor(r % 200, g % 200, b % 200)
  27. l = 3.0 / 4.0 * l
  28. lt(s)
  29. fd(l)
  30. if level < lv:
  31. draw_tree(l, level + 1)
  32. bk(l)
  33. rt(2 * s)
  34. fd(l)
  35. if level < lv:
  36. draw_tree(l, level + 1)
  37. bk(l)
  38. lt(s)
  39. # restore the previous pen width
  40. width(w)
  41. speed("fastest")
  42. draw_tree(l, 4)
  43. done()