Animation#

class superturtle.animation.animate(frames=1, loop=False, debug=False, gif_filename=None)[source]#

Runs an animation, frame by frame, at a fixed frame rate of 20 fps. An animation consists of a bunch of frames shown one after another. Before creating an animation, create a static image and then think about how you would like for it to move. The simplest way to use animate is with no arguments; this produces a static image. (Not much of an animation!):

for frame in animate():
    draw_my_picture(frame)

Once you are happy with your static image, specify frames and animate will run the provided code block over and over, drawing one frame at a time:

for frame in animate(frames=6, debug=True):
    draw_my_picture(frame)

Because we set debug to True, you will see the following output in the Terminal. Additionally, since we are in debug mode, you need to press enter to advance one frame at a time.:

Drawing frame 0
Drawing frame 1
Drawing frame 2
Drawing frame 3
Drawing frame 4
Drawing frame 5
Parameters:
  • frames (int) – The total number of frames in your animation.

  • loop (bool) – When True, the animation will play in a loop.

  • debug (bool) – When True, renders the animation in debug mode.

  • gif_filename (str) – When provided, saves the animation as a gif, with 10 frames per second.

class superturtle.animation.Frame(num_frames, index=0, debug=False)[source]#

Represents one frame in an animation. When creating an animation, animate will yield one Frame for each frame. The Frame can be used to check information, such as the current frame index (the first frame’s index is 0; the tenth frame’s index is 9). The Frame can also be used to create motion through the use of transformations. A transformation is a change to the picture which gradually changes from frame to frame. The three supported transformations are rotate, scale, and translate.

rotate(start, stop=None, first_frame=None, last_frame=None, cycles=1, mirror=False, easing=None)[source]#

Runs the code block within a rotation:

for frame in animate(frames=30):
    with frame.rotate(0, 90):
        square(100)
Parameters:
  • start (int) – the initial value.

  • stop (int) – (optional) the final value. If not provided, this will be a static rotation of start on every frame.

  • first_frame (int) – (optional) The first frame at which this rotation should be interpolated. If given, the rotation will be start at first_frame and all prior frames. If not given, interpolation starts at the beginning of the animation.

  • last_frame (int) – (optional) The last frame at which this rotation should be interpolated. If given, the rotation will be stop at last_frame and all later frames. If not given, interpolation ends at the end of the animation.

  • cycles (int) – (optional) Number of times the animation should be run.

  • mirror (bool) – (optional) When True, the animation runs forward and then backwards between first_frame and last_frame.

  • easing (function) – (optional) An easing function to use.

scale(start, stop=None, first_frame=None, last_frame=None, cycles=1, mirror=False, easing=None)[source]#

Scales the code block. For this to work correctly, make sure you start from the center of the drawing in the code block:

for frame in animate(frames=30):
    with frame.scale(1, 2):
        square(100)
Parameters:
  • start (int) – the initial value.

  • stop (int) – (optional) the final value. If not provided, this will be a static

  • frame. (scaling of start on every) –

  • first_frame (int) – (optional) The first frame at which this scaling should be interpolated. If given, the scaling will be start at first_frame and all prior frames. If not given, interpolation starts at the beginning of the animation.

  • last_frame (int) – (optional) The last frame at which this scaling should be interpolated. If given, the scaling will be stop at last_frame and all later frames. If not given, interpolation ends at the end of the animation.

  • cycles (int) – (optional) Number of times the animation should be run.

  • mirror (bool) – (optional) When True, the animation runs forward and then backwards between first_frame and last_frame.

  • easing (function) – (optional) An easing function to use.

translate(start, stop=None, first_frame=None, last_frame=None, cycles=1, mirror=False, easing=None)[source]#

Translates (moves) the code block in the current coordinate space:

for frame in animate(frames=30):
    with frame.translate([0, 0], [100, 100]):
        square(100)
Parameters:
  • start (int) – the initial value.

  • stop (int) – (optional) the final value. If not provided, this will be a static translation of start on every frame.

  • first_frame (int) – (optional) The first frame at which this translation should be interpolated. If given, the translation will be start at first_frame and all prior frames. If not given, interpolation starts at the beginning of the animation.

  • last_frame (int) – (optional) The last frame at which this translation should be interpolated. If given, the translation will be stop at last_frame and all later frames. If not given, interpolation ends at the end of the animation.

  • cycles (int) – (optional) Number of times the animation should be run.

  • mirror (bool) – (optional) When True, the animation runs forward and then backwards between first_frame and last_frame.

  • easing (function) – (optional) An easing function to use.

interpolate(start, stop=None, first_frame=None, last_frame=None, cycles=1, mirror=False, easing=None)[source]#

Interpolates a value between start and stop. Interpolation is the process of finding a value partway between two known values. In this function, the two known values are start and stop, and we need to find an appropriate value partway between the two endpoints. When the frame is first_frame, the value should be start and when the frame is last_frame the value should be stop. When the frame is halfway in between the first and last frames, the value should be halfway between the endpoints. Interpolation is used internally by all three of the transformations (rotate, scale, and translate), but you can use it directly if you want. For example, if you want to scale just one side of a rectangle:

def rectangle(a, b):
    for _ in range(2):
        forward(a)
        right(90)
        forward(b)
        right(90)
for frame in animate(frames=60):
    height = frame.interpolate(20, 80)
    width = 100 - height
    rectangle(height, width)
Parameters:
  • start (int) – the initial value.

  • stop (int) – (optional) the final value. If not provided, start is returned.

  • first_frame (int) – (optional) The first frame at which interpolation should be used. If given, the value will be start at first_frame and all prior frames. If not given, interpolation starts at the beginning of the animation.

  • last_frame (int) – (optional) The last frame at which interpolation should be used. If given, the value will be stop at last_frame and all later frames. If not given, interpolation ends at the end of the animation.

  • cycles (int) – (optional) Number of times the animation should be run.

  • mirror (bool) – (optional) When True, the interpolated value reaches stop halfway between first_frame and last_frame, then returns to start.

  • easing (function) – (optional) An easing function to use.