CPSC 120 Principles of Computer Science Fall 2025

Lab 3
Animation

Due: Fri 9/26 at the start of class


Introduction

This week's lab deals with animation — things that move (or change in other ways) on their own.

Successfully completing this lab means that you are able to:


Handin and Presentation Meeting

Handin

Hand in a hardcopy (paper) of your worksheet in class.

To hand in your sketches:

It is OK if you copy your files to the handin directory at the very beginning of class.

Presentation Meeting

Presentation meetings for this lab will be the week of Sept 29.

Exercise #4 is the presentation problem. Come to the presentation meeting prepared to discuss your sketch. You may be asked to point out and explain how your code meets the requirements of the problem, explain how portions of your code work, and/or apply skills from the problem to a new situation (for example, to demonstrate how to figure out coordinates and sizes for a different scene).


Policies

The policies on late work and extensions, academic integrity, and the use of AI for this lab are the same as for lab 2. Review them there.


Preliminaries

Reference

Review this week's slides, in-class exercises handouts, and in-class exercise solutions for information, templates, and examples.

Animation Variables

About variable names and comments:

Descriptive names and comments serve two important purposes:

Some additional notes on working with animation variables:

Comments

In addition to commenting variable declarations if there is information not apparent from the name, also include comments within your code to explain portions of it. A prime example of this is when there are multiple steps needed to draw an element of your scene, such as the cloud in exercise #1 — put a comment before that section of code identifying what it draws e.g.

  // cloud 

The System Variables width and height

Processing provides two system variables width and height for the width and height of the drawing window, respectively. (Note that you can only use these values after the window has been opened with size.)

Use these variables to design more flexible sketches. For example, if you want an ellipse to be centered at the lower right corner of a 300x400 window, you could write

  ellipseMode(CENTER);
  ellipse(300,400,50,50);

However, if you change the size of the window but want the ellipse to still be in the lower right corner, you'll have to change both the size command and the coordinates where you've drawn the ellipse. A simpler solution is to instead write

  ellipseMode(CENTER);
  ellipse(width,height,50,50);

This will always center the ellipse at the lower right corner no matter what size the window is, so changing the window size means only change the size command.

Note that this doesn't mean you should always use width and height — think about whether you want something at particular coordinates no matter what size the window is or whether you want it at the same relative position e.g. lower right corner or center. Use width and height only in the latter case.

About Radians

Exercise 3 involves drawing arcs (part of a circle). You may be familiar with measuring angles in degrees — a complete circle is 360 degrees, half a circle is 180 degrees, and so forth.

Angles can also be specified in radians rather than degrees — a whole circle is 2π radians. The picture shows how degrees and radians relate to each other. Important note: Processing measures angles going clockwise rather than counterclockwise — this is reflected in the picture so there's no issue if you use it to determine any necessary angles, but it is important to be aware of this different convention if you are familiar with applications which measure angles in the opposite direction.

Processing uses radians rather than degrees, so when you work with angles (such as with the arc command, which you'll need in exercise #3 below) you'll need to either use radians directly or convert from degrees to radians.

To work with radians directly, you'll need to express the value π (pi). Processing defines constants PI, HALF_PI, QUARTER_PI, and TWO_PI. This means you can just write PI, HALF_PI, etc when you want those values, and you can use them in expressions such as PI/3 (60 degrees) or 7*PI/6 (210 degrees). For example, the documentation for arc includes the following:

  size(400,400);
  arc(200, 200, 320, 320, 0, PI+QUARTER_PI, PIE);

This uses radians directly. (PI+QUARTER_PI = 5π/4)

Alternatively, to convert from degrees to radians, use

  radians(deg)

where deg is the value in degrees. For example:

  size(400,400);
  arc(200, 200, 320, 320, radians(0), radians(225), PIE);

Exercises

  1. In this exercise you'll create a sketch which draws the scene shown — a tall building in a grassy field, with a cloud floating by. (Click the mouse to reset the cloud to its starting position.) The requirements for your sketch:

    • Name the sketch lab3a.

    • Make the drawing window 400x400.

    • Include all of the elements shown — building, cloud, grass. Unless otherwise specified, you don't need to match the sizes, positions, and colors of the example exactly but you should aim for something close.

    • The cloud should be made up of at least three ellipses and should be a very light gray, not white.

    • The cloud should pass behind the building, not above or in front of it.

    • The cloud should start just outside the window on the left side. It should take a few moments (but not very long) to appear.

    • Clicking the mouse should reset the cloud to its starting position.

  2. Your browser does not support the canvas tag.

    To do this:

    • Complete the Exercise 1 section of the lab 3 worksheet.

    • Create a new sketch, add your name and a description of the sketch in comments at the beginning, and save it as lab3a.

    • Start with the right program structure: copy the template for an animated sketch from the "Program Structure Recap" in Wednesday's in-class exercises handout.

    • Add code to draw the fixed part of the scene (building, grass, sky), including comments labeling the sections of code that draw the building and the grass. Start with the positions, sizes, and colors you identified on the worksheet, then make corrections as needed. You can use Tools→Color Selector to adjust your colors if needed.

    • Add the animated cloud, including a comment labeling the section of code that draws the cloud. Slot your answers from the worksheet into the right spots according to the comments in the program structure template you copied and the rectangle-moving-right example in Wednesday's slides.

    • Make the cloud's position reset when the mouse is clicked in the drawing window. "Resetting" means just setting the values of the relevant animation variables — don't draw any shapes! Review the material from last week about interaction for this.

  3. In this exercise you'll create a sketch with a three-circle pattern where the middle circle grows in size. The requirements for your sketch:

    • Name the sketch lab3b.

    • Make the drawing window 400x400.

    • The pattern should be centered in the drawing window.

    • The center circle should start with a width and height of 1 and should grow over time.

    • The left and right circles should be 30x30 and should just touch the center circle, even as the center circle grows.

    • Make use of width and height instead of hardcoding values related to the size of the window so that the sketch still works as required even when the window size is changed.

    You do not need to make the size of the center circle reset when the pattern reaches the edges of the window as shown in the demo; it is fine for the circle to just keep growing indefinitely.

  4. To do this:

    • Complete the Exercise 2 section of the lab 3 worksheet.

    • Create a new sketch, add your name and a description of the sketch in comments at the beginning, and save it as lab3b.

    • Start with the right program structure: copy the template for an animated sketch from the "Program Structure Recap" in Wednesday's in-class exercises handout.

    • Add code for the animated circle pattern. Slot your answers from the worksheet into the right spots according to the comments in the program structure template you copied and the rectangle-moving-right example in Wednesday's slides.

  5. In this exercise you'll create a sketch with a rolling wheel.

    Your browser does not support the canvas tag.

    The requirements for your sketch:

    • Name the sketch lab3c.

    • Make the drawing window 800x200.

    • The wheel should have two different colors in opposite wedges as in the example. Use arc to draw each wedge. (Look up arc in the Processing API to find out how to use it.) You can pick any two colors.

    • The wheel should start in the lower left corner of the window, just touching the left side and bottom of the window, and should move along the bottom edge of the window.

    • The wheel should rotate, and the rotational speed and horizontal motion should match so the wheel appears to be rolling rather than spinning or sliding.

    • Make use of width and height instead of hardcoding values related to the size of the window so that the sketch still works as required when the window size is changed.

    You do not need to make the wheel reset when it reaches the right edge of the window as shown in the demo; it is fine for the wheel to just disappear off the right side of the window.

    To do this:

    • Complete the Exercise 3 section of the lab 3 worksheet.

    • Create a new sketch, add your name and a description of the sketch in comments at the beginning, and save it as lab3c.

    • Start with the right program structure: copy the template for an animated sketch from the "Program Structure Recap" in Wednesday's in-class exercises handout.

    • Draw the wheel in its starting position in the lower left corner of the window.

    • Make the wheel move to the right — add the relevant animation variable(s) (declaration, initialization, use, update) for the horizontal movement from the worksheet.

    • Make the wheel rotate — add the relevant animation variable(s) (declaration, initialization, use, update) for rotation from the worksheet.

    • Make sure the wheel rolls rather than sliding or spinning — you should have already worked out updates for the animation variables with the right relationship between the amount of rightward movement and the amount of rotation in each frame on the worksheet, but if you made any changes to one of these values when writing the sketch, make sure you update the other appropriately.

  6. In this exercise you'll create an animated sketch of your own design. What the sketch depicts is up to you (here's a chance to be creative!) but for full credit it must include the following elements:

    To do this:

    • Complete the Exercise 4 section of the lab 3 worksheet.

    • Create a new sketch, add your name and a description of the sketch in comments at the beginning, and save it as lab3d.

    • Start with the right program structure: copy the template for an animated sketch from the "Program Structure Recap" in Wednesday's in-class exercises handout.

    • Draw any fixed (not animated) elements. Include comments labeling the sections of code that draw each compound or simple thing. Practice incremental development — add code for a few shapes at a time rather than writing everything before you run the program for the first time.

    • Add the animated elements one at a time. For each, include a comment labeling the section of code that draws that element and slot your answers from the worksheet into the right spots according to the comments in the program structure template you copied and the rectangle-moving-right example in Wednesday's slides.

    • Make the sketch reset when the mouse is clicked — the event handler should only set the animation variables back to their initial values. No drawing!

Extra Credit

Challenge yourself and earn extra credit by going substantially beyond the required elements. (See the assignments and evaluation policy for more details on extra credit.) Some possibilities for #4:

It's OK to add on to your lab3d sketch but be sure to include a brief description of what you've done for extra credit in a comment at the beginning of the sketch.