annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/lib/python3.8/turtledemo/bytedesign.py @ 68:5028fdace37b

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