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