ยปCore Development>Code coverage>Lib/turtledemo/bytedesign.py

Python code coverage for Lib/turtledemo/bytedesign.py

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