rosette.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. """ turtle-example-suite:
  2. tdemo_wikipedia3.py
  3. This example is
  4. inspired by the Wikipedia article on turtle
  5. graphics. (See example wikipedia1 for URLs)
  6. First we create (ne-1) (i.e. 35 in this
  7. example) copies of our first turtle p.
  8. Then we let them perform their steps in
  9. parallel.
  10. Followed by a complete undo().
  11. """
  12. from turtle import Screen, Turtle, mainloop
  13. from time import perf_counter as clock, sleep
  14. def mn_eck(p, ne,sz):
  15. turtlelist = [p]
  16. #create ne-1 additional turtles
  17. for i in range(1,ne):
  18. q = p.clone()
  19. q.rt(360.0/ne)
  20. turtlelist.append(q)
  21. p = q
  22. for i in range(ne):
  23. c = abs(ne/2.0-i)/(ne*.7)
  24. # let those ne turtles make a step
  25. # in parallel:
  26. for t in turtlelist:
  27. t.rt(360./ne)
  28. t.pencolor(1-c,0,c)
  29. t.fd(sz)
  30. def main():
  31. s = Screen()
  32. s.bgcolor("black")
  33. p=Turtle()
  34. p.speed(0)
  35. p.hideturtle()
  36. p.pencolor("red")
  37. p.pensize(3)
  38. s.tracer(36,0)
  39. at = clock()
  40. mn_eck(p, 36, 19)
  41. et = clock()
  42. z1 = et-at
  43. sleep(1)
  44. at = clock()
  45. while any(t.undobufferentries() for t in s.turtles()):
  46. for t in s.turtles():
  47. t.undo()
  48. et = clock()
  49. return "runtime: %.3f sec" % (z1+et-at)
  50. if __name__ == '__main__':
  51. msg = main()
  52. print(msg)
  53. mainloop()