jpayne@69: """ turtle-example-suite: jpayne@69: jpayne@69: tdemo_round_dance.py jpayne@69: jpayne@69: (Needs version 1.1 of the turtle module that jpayne@69: comes with Python 3.1) jpayne@69: jpayne@69: Dancing turtles have a compound shape jpayne@69: consisting of a series of triangles of jpayne@69: decreasing size. jpayne@69: jpayne@69: Turtles march along a circle while rotating jpayne@69: pairwise in opposite direction, with one jpayne@69: exception. Does that breaking of symmetry jpayne@69: enhance the attractiveness of the example? jpayne@69: jpayne@69: Press any key to stop the animation. jpayne@69: jpayne@69: Technically: demonstrates use of compound jpayne@69: shapes, transformation of shapes as well as jpayne@69: cloning turtles. The animation is jpayne@69: controlled through update(). jpayne@69: """ jpayne@69: jpayne@69: from turtle import * jpayne@69: jpayne@69: def stop(): jpayne@69: global running jpayne@69: running = False jpayne@69: jpayne@69: def main(): jpayne@69: global running jpayne@69: clearscreen() jpayne@69: bgcolor("gray10") jpayne@69: tracer(False) jpayne@69: shape("triangle") jpayne@69: f = 0.793402 jpayne@69: phi = 9.064678 jpayne@69: s = 5 jpayne@69: c = 1 jpayne@69: # create compound shape jpayne@69: sh = Shape("compound") jpayne@69: for i in range(10): jpayne@69: shapesize(s) jpayne@69: p =get_shapepoly() jpayne@69: s *= f jpayne@69: c *= f jpayne@69: tilt(-phi) jpayne@69: sh.addcomponent(p, (c, 0.25, 1-c), "black") jpayne@69: register_shape("multitri", sh) jpayne@69: # create dancers jpayne@69: shapesize(1) jpayne@69: shape("multitri") jpayne@69: pu() jpayne@69: setpos(0, -200) jpayne@69: dancers = [] jpayne@69: for i in range(180): jpayne@69: fd(7) jpayne@69: tilt(-4) jpayne@69: lt(2) jpayne@69: update() jpayne@69: if i % 12 == 0: jpayne@69: dancers.append(clone()) jpayne@69: home() jpayne@69: # dance jpayne@69: running = True jpayne@69: onkeypress(stop) jpayne@69: listen() jpayne@69: cs = 1 jpayne@69: while running: jpayne@69: ta = -4 jpayne@69: for dancer in dancers: jpayne@69: dancer.fd(7) jpayne@69: dancer.lt(2) jpayne@69: dancer.tilt(ta) jpayne@69: ta = -4 if ta > 0 else 2 jpayne@69: if cs < 180: jpayne@69: right(4) jpayne@69: shapesize(cs) jpayne@69: cs *= 1.005 jpayne@69: update() jpayne@69: return "DONE!" jpayne@69: jpayne@69: if __name__=='__main__': jpayne@69: print(main()) jpayne@69: mainloop()