import turtle.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import turtle
  2. import random
  3. from turtle import *
  4. from time import sleep
  5. t = turtle.Turtle()
  6. w = turtle.Screen()
  7. def tree(branchLen, t):
  8. if branchLen > 3:
  9. if 8 <= branchLen <= 12:
  10. if random.randint(0, 2) == 0:
  11. t.color('snow')
  12. else:
  13. t.color('lightcoral')
  14. t.pensize(branchLen / 3)
  15. elif branchLen < 8:
  16. if random.randint(0, 1) == 0:
  17. t.color('snow')
  18. else:
  19. t.color('lightcoral')
  20. t.pensize(branchLen / 2)
  21. else:
  22. t.color('sienna')
  23. t.pensize(branchLen / 10)
  24. t.forward(branchLen)
  25. a = 1.5 * random.random()
  26. t.right(20 * a)
  27. b = 1.5 * random.random()
  28. tree(branchLen - 10 * b, t)
  29. t.left(40 * a)
  30. tree(branchLen - 10 * b, t)
  31. t.right(20 * a)
  32. t.up()
  33. t.backward(branchLen)
  34. t.down()
  35. def petal(m, t): # 树下花瓣
  36. for i in range(m):
  37. a = 200 - 400 * random.random()
  38. b = 10 - 20 * random.random()
  39. t.up()
  40. t.forward(b)
  41. t.left(90)
  42. t.forward(a)
  43. t.down()
  44. t.color("lightcoral")
  45. t.circle(1)
  46. t.up()
  47. t.backward(a)
  48. t.right(90)
  49. t.backward(b)
  50. def main():
  51. t = turtle.Turtle()
  52. myWin = turtle.Screen()
  53. # getscreen().tracer(5, 0)
  54. turtle.screensize(bg='wheat')
  55. t.left(90)
  56. t.up()
  57. t.backward(150)
  58. t.down()
  59. t.color('sienna')
  60. tree(60, t)
  61. petal(100, t)
  62. myWin.exitonclick()
  63. main()