| 1 | n/a | """ turtle-example-suite: |
|---|
| 2 | n/a | |
|---|
| 3 | n/a | tdemo_round_dance.py |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | (Needs version 1.1 of the turtle module that |
|---|
| 6 | n/a | comes with Python 3.1) |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | Dancing turtles have a compound shape |
|---|
| 9 | n/a | consisting of a series of triangles of |
|---|
| 10 | n/a | decreasing size. |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | Turtles march along a circle while rotating |
|---|
| 13 | n/a | pairwise in opposite direction, with one |
|---|
| 14 | n/a | exception. Does that breaking of symmetry |
|---|
| 15 | n/a | enhance the attractiveness of the example? |
|---|
| 16 | n/a | |
|---|
| 17 | n/a | Press any key to stop the animation. |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | Technically: demonstrates use of compound |
|---|
| 20 | n/a | shapes, transformation of shapes as well as |
|---|
| 21 | n/a | cloning turtles. The animation is |
|---|
| 22 | n/a | controlled through update(). |
|---|
| 23 | n/a | """ |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | from turtle import * |
|---|
| 26 | n/a | |
|---|
| 27 | n/a | def stop(): |
|---|
| 28 | n/a | global running |
|---|
| 29 | n/a | running = False |
|---|
| 30 | n/a | |
|---|
| 31 | n/a | def main(): |
|---|
| 32 | n/a | global running |
|---|
| 33 | n/a | clearscreen() |
|---|
| 34 | n/a | bgcolor("gray10") |
|---|
| 35 | n/a | tracer(False) |
|---|
| 36 | n/a | shape("triangle") |
|---|
| 37 | n/a | f = 0.793402 |
|---|
| 38 | n/a | phi = 9.064678 |
|---|
| 39 | n/a | s = 5 |
|---|
| 40 | n/a | c = 1 |
|---|
| 41 | n/a | # create compound shape |
|---|
| 42 | n/a | sh = Shape("compound") |
|---|
| 43 | n/a | for i in range(10): |
|---|
| 44 | n/a | shapesize(s) |
|---|
| 45 | n/a | p =get_shapepoly() |
|---|
| 46 | n/a | s *= f |
|---|
| 47 | n/a | c *= f |
|---|
| 48 | n/a | tilt(-phi) |
|---|
| 49 | n/a | sh.addcomponent(p, (c, 0.25, 1-c), "black") |
|---|
| 50 | n/a | register_shape("multitri", sh) |
|---|
| 51 | n/a | # create dancers |
|---|
| 52 | n/a | shapesize(1) |
|---|
| 53 | n/a | shape("multitri") |
|---|
| 54 | n/a | pu() |
|---|
| 55 | n/a | setpos(0, -200) |
|---|
| 56 | n/a | dancers = [] |
|---|
| 57 | n/a | for i in range(180): |
|---|
| 58 | n/a | fd(7) |
|---|
| 59 | n/a | tilt(-4) |
|---|
| 60 | n/a | lt(2) |
|---|
| 61 | n/a | update() |
|---|
| 62 | n/a | if i % 12 == 0: |
|---|
| 63 | n/a | dancers.append(clone()) |
|---|
| 64 | n/a | home() |
|---|
| 65 | n/a | # dance |
|---|
| 66 | n/a | running = True |
|---|
| 67 | n/a | onkeypress(stop) |
|---|
| 68 | n/a | listen() |
|---|
| 69 | n/a | cs = 1 |
|---|
| 70 | n/a | while running: |
|---|
| 71 | n/a | ta = -4 |
|---|
| 72 | n/a | for dancer in dancers: |
|---|
| 73 | n/a | dancer.fd(7) |
|---|
| 74 | n/a | dancer.lt(2) |
|---|
| 75 | n/a | dancer.tilt(ta) |
|---|
| 76 | n/a | ta = -4 if ta > 0 else 2 |
|---|
| 77 | n/a | if cs < 180: |
|---|
| 78 | n/a | right(4) |
|---|
| 79 | n/a | shapesize(cs) |
|---|
| 80 | n/a | cs *= 1.005 |
|---|
| 81 | n/a | update() |
|---|
| 82 | n/a | return "DONE!" |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | if __name__=='__main__': |
|---|
| 85 | n/a | print(main()) |
|---|
| 86 | n/a | mainloop() |
|---|