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