import turtle.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import turtle as T
  2. import random
  3. import time
  4. # 画樱花的躯干(60,t)
  5. def Tree(branch, t):
  6. time.sleep(0.0005)
  7. if branch > 3:
  8. if 8 <= branch <= 12:
  9. if random.randint(0, 2) == 0:
  10. t.color('snow') # 白
  11. else:
  12. t.color('lightcoral') # 淡珊瑚色
  13. t.pensize(branch / 3)
  14. elif branch < 8:
  15. if random.randint(0, 1) == 0:
  16. t.color('snow')
  17. else:
  18. t.color('lightcoral') # 淡珊瑚色
  19. t.pensize(branch / 2)
  20. else:
  21. t.color('sienna') # 赭(zhě)色
  22. t.pensize(branch / 10) # 6
  23. t.forward(branch)
  24. a = 1.5 * random.random()
  25. t.right(20 * a)
  26. b = 1.5 * random.random()
  27. Tree(branch - 10 * b, t)
  28. t.left(40 * a)
  29. Tree(branch - 10 * b, t)
  30. t.right(20 * a)
  31. t.up()
  32. t.backward(branch)
  33. t.down()
  34. # 掉落的花瓣
  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. # 绘图区域
  51. t = T.Turtle()
  52. # 画布大小
  53. w = T.Screen()
  54. t.hideturtle() # 隐藏画笔
  55. t.getscreen().tracer(5, 0)
  56. w.screensize(bg='wheat') # wheat小麦
  57. t.left(90)
  58. t.up()
  59. t.backward(150)
  60. t.down()
  61. t.color('sienna')
  62. # 画樱花的躯干
  63. Tree(60, t)
  64. # 掉落的花瓣
  65. Petal(200, t)
  66. w.exitonclick()