In-Class Activity: Exploring Simple Animations

 


Introduction

This activity will help you learn how to create simple animations using for loops, how to display them, how to save the images to a folder, and then create an MP4 file. You will also explore using drawing elements to create animations. (See the Drawing Shapes activity and the Introduction to Pictures reading for a refresher on the drawing functions.)



Getting Started with Simple Movement

  1. Download the Exploring Simple Animations notebook and open it in Google Colab.
  2. Add your name, the date, and a description of the activity in the first text cell.
  3. The function moveBox moves a red box across a horizontal line at the top of a yellow background. Run the code cell that tests the moveBox function. Discuss what happens with your peers or an instructor or TA.
  4. (Optional) Write a function similar to moveBox that takes the row number and the color for the box as parameters rather than constants. The function should replace row 10 and 'red' with the values from the parameters.
  5. Write a function similar to the one above that moves the box along a vertical line instead of a horizontal line.
  6. Write a function that moves a box around the perimeter of the canvas. It should move across the top, down the right side, back across the bottom, and then up the left side. (Note: remember you can use the range function to go backwards as well as forwards, as in range(10,-1,-1). This will let us start at 10 and go down by 1's to 0.)
  7. Write a function similar to the ones above that moves a box along the diagonal of the canvas, from the upper left corner to as far down and to the right as you can go.
  8. Optional: Write a function similar to the previous one that moves a box along the other diagonal, from the upper right to as far down and left as you can go.
  9. Optional: You may modify any of these functions so that they take a picture to use as a background as a parameter instead of making a solid color background.

Saving images and Creating a video file

    In order to create a video file, we will save each of the images that were created by calling one of the movement functions. The function savePicsToJPGS takes a list of images, a path to a folder on your Google drive, and the name to be used for the images. It then saves each image from the list into the specified folder, numbering them as it saves them.
  1. To test that it works, do the following:
    1. Create a folder, called Boxes on your Google drive.
    2. Uncomment the appropriate lines in the Code cell that tests the moveBox function so that you will now save the images that get created in the moveBox function to a folder on your Drive. The path of the new folder you created on your Google drive gets saved into the boxPath variable, and then the savePicsToJPGS function will save all of those images into your folder.
  2. Lastly, we can then read all of the jpg files and write them to an mp4 file. The function convertPicsToMP4will do this for us. Uncomment this line of code and then run the Code cell.
  3. Look in your Google drive to make sure the mp4 is there.

Moving Two Boxes

  1. Write a function that simultaneously moves one box across the top of the canvas, and another box across the bottom. You should be able to copy and paste the moveBox functionand add one or two lines of code. (Just add another filled rectangle to the canvas inside the for loop.)
  2. Optional: Write a function that simultaneously moves one box along the top-left-to-bottom-right diagonal, and another box from the top-right-to-bottom-left diagonal. Again, you should be able to modify either exercise #5 or #6 to do this.
  3. This function movingRectangles that is in the notebook represents a more mathematically complicated way to get the boxes moving. One box moves down the canvas in a circular motion, while the other just moves down the diagonal of the canvas. Test it to see how it works.

TickerTape

  1. The tickerTape function that is in the notebook moves a text across a horizontal line. This should be similar to moving a box, except drawing text on the image instead of rectangles.
  2. Write a function that moves text along a straight line, from top to bottom of the canvas.

Submit your results

  1. Submit the file you created in this mini-lab via Kit.


>