bytedesign.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. import math
  18. from turtle import Turtle, mainloop
  19. from time import clock
  20. # wrapper for any additional drawing routines
  21. # that need to know about each other
  22. class Designer(Turtle):
  23. def design(self, homePos, scale):
  24. self.up()
  25. for i in range(5):
  26. self.forward(64.65 * scale)
  27. self.down()
  28. self.wheel(self.position(), scale)
  29. self.up()
  30. self.backward(64.65 * scale)
  31. self.right(72)
  32. self.up()
  33. self.goto(homePos)
  34. self.right(36)
  35. self.forward(24.5 * scale)
  36. self.right(198)
  37. self.down()
  38. self.centerpiece(46 * scale, 143.4, scale)
  39. self.getscreen().tracer(True)
  40. def wheel(self, initpos, scale):
  41. self.right(54)
  42. for i in range(4):
  43. self.pentpiece(initpos, scale)
  44. self.down()
  45. self.left(36)
  46. for i in range(5):
  47. self.tripiece(initpos, scale)
  48. self.left(36)
  49. for i in range(5):
  50. self.down()
  51. self.right(72)
  52. self.forward(28 * scale)
  53. self.up()
  54. self.backward(28 * scale)
  55. self.left(54)
  56. self.getscreen().update()
  57. def tripiece(self, initpos, scale):
  58. oldh = self.heading()
  59. self.down()
  60. self.backward(2.5 * scale)
  61. self.tripolyr(31.5 * scale, scale)
  62. self.up()
  63. self.goto(initpos)
  64. self.setheading(oldh)
  65. self.down()
  66. self.backward(2.5 * scale)
  67. self.tripolyl(31.5 * scale, scale)
  68. self.up()
  69. self.goto(initpos)
  70. self.setheading(oldh)
  71. self.left(72)
  72. self.getscreen().update()
  73. def pentpiece(self, initpos, scale):
  74. oldh = self.heading()
  75. self.up()
  76. self.forward(29 * scale)
  77. self.down()
  78. for i in range(5):
  79. self.forward(18 * scale)
  80. self.right(72)
  81. self.pentr(18 * scale, 75, scale)
  82. self.up()
  83. self.goto(initpos)
  84. self.setheading(oldh)
  85. self.forward(29 * scale)
  86. self.down()
  87. for i in range(5):
  88. self.forward(18 * scale)
  89. self.right(72)
  90. self.pentl(18 * scale, 75, scale)
  91. self.up()
  92. self.goto(initpos)
  93. self.setheading(oldh)
  94. self.left(72)
  95. self.getscreen().update()
  96. def pentl(self, side, ang, scale):
  97. if side < (2 * scale): return
  98. self.forward(side)
  99. self.left(ang)
  100. self.pentl(side - (.38 * scale), ang, scale)
  101. def pentr(self, side, ang, scale):
  102. if side < (2 * scale): return
  103. self.forward(side)
  104. self.right(ang)
  105. self.pentr(side - (.38 * scale), ang, scale)
  106. def tripolyr(self, side, scale):
  107. if side < (4 * scale): return
  108. self.forward(side)
  109. self.right(111)
  110. self.forward(side / 1.78)
  111. self.right(111)
  112. self.forward(side / 1.3)
  113. self.right(146)
  114. self.tripolyr(side * .75, scale)
  115. def tripolyl(self, side, scale):
  116. if side < (4 * scale): return
  117. self.forward(side)
  118. self.left(111)
  119. self.forward(side / 1.78)
  120. self.left(111)
  121. self.forward(side / 1.3)
  122. self.left(146)
  123. self.tripolyl(side * .75, scale)
  124. def centerpiece(self, s, a, scale):
  125. self.forward(s); self.left(a)
  126. if s < (7.5 * scale):
  127. return
  128. self.centerpiece(s - (1.2 * scale), a, scale)
  129. def main():
  130. t = Designer()
  131. t.speed(0)
  132. t.hideturtle()
  133. t.getscreen().delay(0)
  134. t.getscreen().tracer(0)
  135. at = clock()
  136. t.design(t.position(), 2)
  137. et = clock()
  138. return "runtime: %.2f sec." % (et-at)
  139. if __name__ == '__main__':
  140. msg = main()
  141. print(msg)
  142. mainloop()