bytedesign.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/usr/bin/env python3
  2. """ turtle-example-suite:
  3. tdemo_bytedesign.py
  4. An example adapted from the example-suite
  5. of PythonCard's turtle graphics.
  6. It's based on an article in BYTE magazine
  7. Problem Solving with Logo: Using Turtle
  8. Graphics to Redraw a Design
  9. November 1982, p. 118 - 134
  10. -------------------------------------------
  11. Due to the statement
  12. t.delay(0)
  13. in line 152, which sets the animation delay
  14. to 0, this animation runs in "line per line"
  15. mode as fast as possible.
  16. """
  17. from turtle import Turtle, mainloop
  18. from time import perf_counter as clock
  19. # wrapper for any additional drawing routines
  20. # that need to know about each other
  21. class Designer(Turtle):
  22. def design(self, homePos, scale):
  23. self.up()
  24. for i in range(5):
  25. self.forward(64.65 * scale)
  26. self.down()
  27. self.wheel(self.position(), scale)
  28. self.up()
  29. self.backward(64.65 * scale)
  30. self.right(72)
  31. self.up()
  32. self.goto(homePos)
  33. self.right(36)
  34. self.forward(24.5 * scale)
  35. self.right(198)
  36. self.down()
  37. self.centerpiece(46 * scale, 143.4, scale)
  38. self.getscreen().tracer(True)
  39. def wheel(self, initpos, scale):
  40. self.right(54)
  41. for i in range(4):
  42. self.pentpiece(initpos, scale)
  43. self.down()
  44. self.left(36)
  45. for i in range(5):
  46. self.tripiece(initpos, scale)
  47. self.left(36)
  48. for i in range(5):
  49. self.down()
  50. self.right(72)
  51. self.forward(28 * scale)
  52. self.up()
  53. self.backward(28 * scale)
  54. self.left(54)
  55. self.getscreen().update()
  56. def tripiece(self, initpos, scale):
  57. oldh = self.heading()
  58. self.down()
  59. self.backward(2.5 * scale)
  60. self.tripolyr(31.5 * scale, scale)
  61. self.up()
  62. self.goto(initpos)
  63. self.setheading(oldh)
  64. self.down()
  65. self.backward(2.5 * scale)
  66. self.tripolyl(31.5 * scale, scale)
  67. self.up()
  68. self.goto(initpos)
  69. self.setheading(oldh)
  70. self.left(72)
  71. self.getscreen().update()
  72. def pentpiece(self, initpos, scale):
  73. oldh = self.heading()
  74. self.up()
  75. self.forward(29 * scale)
  76. self.down()
  77. for i in range(5):
  78. self.forward(18 * scale)
  79. self.right(72)
  80. self.pentr(18 * scale, 75, scale)
  81. self.up()
  82. self.goto(initpos)
  83. self.setheading(oldh)
  84. self.forward(29 * scale)
  85. self.down()
  86. for i in range(5):
  87. self.forward(18 * scale)
  88. self.right(72)
  89. self.pentl(18 * scale, 75, scale)
  90. self.up()
  91. self.goto(initpos)
  92. self.setheading(oldh)
  93. self.left(72)
  94. self.getscreen().update()
  95. def pentl(self, side, ang, scale):
  96. if side < (2 * scale): return
  97. self.forward(side)
  98. self.left(ang)
  99. self.pentl(side - (.38 * scale), ang, scale)
  100. def pentr(self, side, ang, scale):
  101. if side < (2 * scale): return
  102. self.forward(side)
  103. self.right(ang)
  104. self.pentr(side - (.38 * scale), ang, scale)
  105. def tripolyr(self, side, scale):
  106. if side < (4 * scale): return
  107. self.forward(side)
  108. self.right(111)
  109. self.forward(side / 1.78)
  110. self.right(111)
  111. self.forward(side / 1.3)
  112. self.right(146)
  113. self.tripolyr(side * .75, scale)
  114. def tripolyl(self, side, scale):
  115. if side < (4 * scale): return
  116. self.forward(side)
  117. self.left(111)
  118. self.forward(side / 1.78)
  119. self.left(111)
  120. self.forward(side / 1.3)
  121. self.left(146)
  122. self.tripolyl(side * .75, scale)
  123. def centerpiece(self, s, a, scale):
  124. self.forward(s); self.left(a)
  125. if s < (7.5 * scale):
  126. return
  127. self.centerpiece(s - (1.2 * scale), a, scale)
  128. def main():
  129. t = Designer()
  130. t.speed(0)
  131. t.hideturtle()
  132. t.getscreen().delay(0)
  133. t.getscreen().tracer(0)
  134. at = clock()
  135. t.design(t.position(), 2)
  136. et = clock()
  137. return "runtime: %.2f sec." % (et-at)
  138. if __name__ == '__main__':
  139. msg = main()
  140. print(msg)
  141. mainloop()