1 | n/a | """ turtle-example-suite: |
---|
2 | n/a | |
---|
3 | n/a | tdemo_wikipedia3.py |
---|
4 | n/a | |
---|
5 | n/a | This example is |
---|
6 | n/a | inspired by the Wikipedia article on turtle |
---|
7 | n/a | graphics. (See example wikipedia1 for URLs) |
---|
8 | n/a | |
---|
9 | n/a | First we create (ne-1) (i.e. 35 in this |
---|
10 | n/a | example) copies of our first turtle p. |
---|
11 | n/a | Then we let them perform their steps in |
---|
12 | n/a | parallel. |
---|
13 | n/a | |
---|
14 | n/a | Followed by a complete undo(). |
---|
15 | n/a | """ |
---|
16 | n/a | from turtle import Screen, Turtle, mainloop |
---|
17 | n/a | from time import clock, sleep |
---|
18 | n/a | |
---|
19 | n/a | def mn_eck(p, ne,sz): |
---|
20 | n/a | turtlelist = [p] |
---|
21 | n/a | #create ne-1 additional turtles |
---|
22 | n/a | for i in range(1,ne): |
---|
23 | n/a | q = p.clone() |
---|
24 | n/a | q.rt(360.0/ne) |
---|
25 | n/a | turtlelist.append(q) |
---|
26 | n/a | p = q |
---|
27 | n/a | for i in range(ne): |
---|
28 | n/a | c = abs(ne/2.0-i)/(ne*.7) |
---|
29 | n/a | # let those ne turtles make a step |
---|
30 | n/a | # in parallel: |
---|
31 | n/a | for t in turtlelist: |
---|
32 | n/a | t.rt(360./ne) |
---|
33 | n/a | t.pencolor(1-c,0,c) |
---|
34 | n/a | t.fd(sz) |
---|
35 | n/a | |
---|
36 | n/a | def main(): |
---|
37 | n/a | s = Screen() |
---|
38 | n/a | s.bgcolor("black") |
---|
39 | n/a | p=Turtle() |
---|
40 | n/a | p.speed(0) |
---|
41 | n/a | p.hideturtle() |
---|
42 | n/a | p.pencolor("red") |
---|
43 | n/a | p.pensize(3) |
---|
44 | n/a | |
---|
45 | n/a | s.tracer(36,0) |
---|
46 | n/a | |
---|
47 | n/a | at = clock() |
---|
48 | n/a | mn_eck(p, 36, 19) |
---|
49 | n/a | et = clock() |
---|
50 | n/a | z1 = et-at |
---|
51 | n/a | |
---|
52 | n/a | sleep(1) |
---|
53 | n/a | |
---|
54 | n/a | at = clock() |
---|
55 | n/a | while any([t.undobufferentries() for t in s.turtles()]): |
---|
56 | n/a | for t in s.turtles(): |
---|
57 | n/a | t.undo() |
---|
58 | n/a | et = clock() |
---|
59 | n/a | return "runtime: %.3f sec" % (z1+et-at) |
---|
60 | n/a | |
---|
61 | n/a | |
---|
62 | n/a | if __name__ == '__main__': |
---|
63 | n/a | msg = main() |
---|
64 | n/a | print(msg) |
---|
65 | n/a | mainloop() |
---|