Back to index

python3.2  3.2.2
bytedesign.py
Go to the documentation of this file.
00001 #!/usr/bin/env python3
00002 """      turtle-example-suite:
00003 
00004         tdemo_bytedesign.py
00005 
00006 An example adapted from the example-suite
00007 of PythonCard's turtle graphics.
00008 
00009 It's based on an article in BYTE magazine
00010 Problem Solving with Logo: Using Turtle
00011 Graphics to Redraw a Design
00012 November 1982, p. 118 - 134
00013 
00014 -------------------------------------------
00015 
00016 Due to the statement
00017 
00018 t.delay(0)
00019 
00020 in line 152, which sets the animation delay
00021 to 0, this animation runs in "line per line"
00022 mode as fast as possible.
00023 """
00024 
00025 import math
00026 from turtle import Turtle, mainloop
00027 from time import clock
00028 
00029 # wrapper for any additional drawing routines
00030 # that need to know about each other
00031 class Designer(Turtle):
00032 
00033     def design(self, homePos, scale):
00034         self.up()
00035         for i in range(5):
00036             self.forward(64.65 * scale)
00037             self.down()
00038             self.wheel(self.position(), scale)
00039             self.up()
00040             self.backward(64.65 * scale)
00041             self.right(72)
00042         self.up()
00043         self.goto(homePos)
00044         self.right(36)
00045         self.forward(24.5 * scale)
00046         self.right(198)
00047         self.down()
00048         self.centerpiece(46 * scale, 143.4, scale)
00049         self.getscreen().tracer(True)
00050 
00051     def wheel(self, initpos, scale):
00052         self.right(54)
00053         for i in range(4):
00054             self.pentpiece(initpos, scale)
00055         self.down()
00056         self.left(36)
00057         for i in range(5):
00058             self.tripiece(initpos, scale)
00059         self.left(36)
00060         for i in range(5):
00061             self.down()
00062             self.right(72)
00063             self.forward(28 * scale)
00064             self.up()
00065             self.backward(28 * scale)
00066         self.left(54)
00067         self.getscreen().update()
00068 
00069     def tripiece(self, initpos, scale):
00070         oldh = self.heading()
00071         self.down()
00072         self.backward(2.5 * scale)
00073         self.tripolyr(31.5 * scale, scale)
00074         self.up()
00075         self.goto(initpos)
00076         self.setheading(oldh)
00077         self.down()
00078         self.backward(2.5 * scale)
00079         self.tripolyl(31.5 * scale, scale)
00080         self.up()
00081         self.goto(initpos)
00082         self.setheading(oldh)
00083         self.left(72)
00084         self.getscreen().update()
00085 
00086     def pentpiece(self, initpos, scale):
00087         oldh = self.heading()
00088         self.up()
00089         self.forward(29 * scale)
00090         self.down()
00091         for i in range(5):
00092             self.forward(18 * scale)
00093             self.right(72)
00094         self.pentr(18 * scale, 75, scale)
00095         self.up()
00096         self.goto(initpos)
00097         self.setheading(oldh)
00098         self.forward(29 * scale)
00099         self.down()
00100         for i in range(5):
00101             self.forward(18 * scale)
00102             self.right(72)
00103         self.pentl(18 * scale, 75, scale)
00104         self.up()
00105         self.goto(initpos)
00106         self.setheading(oldh)
00107         self.left(72)
00108         self.getscreen().update()
00109 
00110     def pentl(self, side, ang, scale):
00111         if side < (2 * scale): return
00112         self.forward(side)
00113         self.left(ang)
00114         self.pentl(side - (.38 * scale), ang, scale)
00115 
00116     def pentr(self, side, ang, scale):
00117         if side < (2 * scale): return
00118         self.forward(side)
00119         self.right(ang)
00120         self.pentr(side - (.38 * scale), ang, scale)
00121 
00122     def tripolyr(self, side, scale):
00123         if side < (4 * scale): return
00124         self.forward(side)
00125         self.right(111)
00126         self.forward(side / 1.78)
00127         self.right(111)
00128         self.forward(side / 1.3)
00129         self.right(146)
00130         self.tripolyr(side * .75, scale)
00131 
00132     def tripolyl(self, side, scale):
00133         if side < (4 * scale): return
00134         self.forward(side)
00135         self.left(111)
00136         self.forward(side / 1.78)
00137         self.left(111)
00138         self.forward(side / 1.3)
00139         self.left(146)
00140         self.tripolyl(side * .75, scale)
00141 
00142     def centerpiece(self, s, a, scale):
00143         self.forward(s); self.left(a)
00144         if s < (7.5 * scale):
00145             return
00146         self.centerpiece(s - (1.2 * scale), a, scale)
00147 
00148 def main():
00149     t = Designer()
00150     t.speed(0)
00151     t.hideturtle()
00152     t.getscreen().delay(0)
00153     t.getscreen().tracer(0)
00154     at = clock()
00155     t.design(t.position(), 2)
00156     et = clock()
00157     return "runtime: %.2f sec." % (et-at)
00158 
00159 if __name__ == '__main__':
00160     msg = main()
00161     print(msg)
00162     mainloop()