round_dance.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. """ turtle-example-suite:
  2. tdemo_round_dance.py
  3. (Needs version 1.1 of the turtle module that
  4. comes with Python 3.1)
  5. Dancing turtles have a compound shape
  6. consisting of a series of triangles of
  7. decreasing size.
  8. Turtles march along a circle while rotating
  9. pairwise in opposite direction, with one
  10. exception. Does that breaking of symmetry
  11. enhance the attractiveness of the example?
  12. Press any key to stop the animation.
  13. Technically: demonstrates use of compound
  14. shapes, transformation of shapes as well as
  15. cloning turtles. The animation is
  16. controlled through update().
  17. """
  18. from turtle import *
  19. def stop():
  20. global running
  21. running = False
  22. def main():
  23. global running
  24. clearscreen()
  25. bgcolor("gray10")
  26. tracer(False)
  27. shape("triangle")
  28. f = 0.793402
  29. phi = 9.064678
  30. s = 5
  31. c = 1
  32. # create compound shape
  33. sh = Shape("compound")
  34. for i in range(10):
  35. shapesize(s)
  36. p =get_shapepoly()
  37. s *= f
  38. c *= f
  39. tilt(-phi)
  40. sh.addcomponent(p, (c, 0.25, 1-c), "black")
  41. register_shape("multitri", sh)
  42. # create dancers
  43. shapesize(1)
  44. shape("multitri")
  45. pu()
  46. setpos(0, -200)
  47. dancers = []
  48. for i in range(180):
  49. fd(7)
  50. tilt(-4)
  51. lt(2)
  52. update()
  53. if i % 12 == 0:
  54. dancers.append(clone())
  55. home()
  56. # dance
  57. running = True
  58. onkeypress(stop)
  59. listen()
  60. cs = 1
  61. while running:
  62. ta = -4
  63. for dancer in dancers:
  64. dancer.fd(7)
  65. dancer.lt(2)
  66. dancer.tilt(ta)
  67. ta = -4 if ta > 0 else 2
  68. if cs < 180:
  69. right(4)
  70. shapesize(cs)
  71. cs *= 1.005
  72. update()
  73. return "DONE!"
  74. if __name__=='__main__':
  75. print(main())
  76. mainloop()