CPSC 120 Principles of Computer Science Fall 2025

Lab 6
Making Choices

Due: Fri 10/17 10/24 at the start of class


Introduction

This week's lab deals with conditionals (more commonly known as if statements), one of two fundamental control structures in Processing. (We'll get to the other, loops, next.) The ability to make choices — to have different things happen at different times, whether this is something that only happens in some frames and not others or a choice of alternatives for what happens in a frame — is a big step forward in terms of what our sketches can do.

We've identified two main patterns for how those choices are made: on-the-spot decisions, where the decision about which alternative to do is based only on the current value of animation or system variables, and prior happenings, where the decision about which alternative depends on prior events — the current decision is the same as the previous one unless the decision-changing thing has just happened. A mouseover effect is an example of an on-the-spot decision — based on the current position of the mouse with respect to some region within the drawing window, one can decide whether or not to take some action. Click-to-toggle is an example of prior happenings — whether or not to take an action in the current frame depends on whether or not the mouse was clicked at some point in the past. Prior happenings decisions require the introduction of a state variable in order to remember the current action.

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 Oct 27. (Note that this is delayed a week because of fall break Oct 18-21. The lab is still due Oct 17, however!)

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.


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.

Relational and Logical Operators

You'll need the relational operators ==, !=, <, <=, >, >= to express the conditions under which each alternative is chosen and the logical operators &&, ||, and ! to build up more complicated conditions from individual parts. The relational operators are used throughout sections 5.1-5.7 in the textbook. The logical operators are covered in section 5.4. Check out that section as well as the posted examples, especially if the ideas were new to you when discussed in class on Wednesday.

Prior Happenings and State Variables

The prior happenings pattern requires the introduction of a state variable to keep track of the current alternative. With any variable, it is necessary to declare, initialize, use, and update it.

Declaring a variable requires a type. State variables will be either int (whole numbers) or boolean (true or false), depending on the number of alternatives. Use boolean when there are only two alternatives (such as an ellipse moving up or down) and int when there are more than two.

The state variable keeps track of the current alternative, so its values must correspond to the different possible alternatives. Which value corresponds to which alternative is something that must be decided — and described in a comment along with the variable declaration. For example, in the white-red-blue rect example there are three possible colors for the rectangle (white, red, and blue) so the state variable rectColor is of type int and the values 1, 2, and 3 were chosen to represent white, red, and blue respectively. This choice is important to explain in a comment because it is arbitrary — we could just have easily chosen 3, 2, 1 or 2, 3, 1 or ... to represent the same three colors.

For boolean state variables, also name the variable to indicate what a true value means. For example, for a rect moving right and left, there are two alternatives: moving right and moving left. The state variable is named moveRight with true corresponding to moving right. (Alternatively, it could have been named moveLeft with the convention that true corresponds to moving left.)

Mouse Movement

Exercise #3 involves doing something only when the mouse is moving. You can determine if the mouse is moving by checking whether its current position is different from its previous position. The mouse's previous position can be obtained from the system variables pmouseX and pmouseY.


Exercises

  1. In this exercise you'll create a sketch which works like the example — a small ellipse starts in the center of the drawing window and moves away from the mouse when the mouse comes close. The requirements for your sketch:

    • Name the sketch lab6a.

    • Make the drawing window 400x400.

    • Define "close to the mouse" as "the mouse position is within 100 pixels of the center of the ellipse".

    • Treat the horizontal and vertical directions separately — you don't need to compute the straight-line distance between the mouse and the center of the ellipse, but instead check separately if the x coordinates are within 100 pixels and if the y coordinates are within 100 pixels.

    • Since the ellipse can (and probably will!) escape outside the boundaries of the drawing window, clicking the mouse should reset the ellipse's position to the center of the drawing window.

    To do this:

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

    • Start with the basic sketch structure: open a drawing window and clear the background.

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

    • Complete the sketch:

      • Start with the parts not involving decisions: set up for an animated ellipse that starts in the center of the window but doesn't actually move (yet).

      • Make the ellipse move away from the mouse in the horizontal direction. Slot your answers from the worksheet into the appropriate template as discussed in class.

      • Make the ellipse move away from the mouse in the vertical direction. This will be very similar to the horizontal case and should only involve adding to your code, not changing existing parts.

  2. Your browser does not support the canvas tag.

  3. In this exercise you'll create a sketch which works like the example — a small ellipse starts in the center of the drawing window, moving right. It changes direction (from right to down to left to up to right) when the mouse is clicked and/or it reaches the edge of the window. The requirements for your sketch:

    • Name the sketch lab6b.

    • Make the drawing window 400x400.

    • The ellipse should start in the center of the window and be moving right.

    • The sequence of directions should be right to down to left to up to right and so forth.

    • Clicking the mouse should change the direction of movement according to the sequence.

    • The ellipse should also change direction according to the sequence when it reaches the edge of the window in the direction it is travelling e.g. when the ellipse is travelling right and reaches the right side of the window, it should start travelling down instead.

    To do this:

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

    • Start with the basic sketch structure: open a drawing window and clear the background.

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

    • Complete the sketch:

      • Start with the parts not involving decisions: set up for an animated ellipse that starts in the center of the window but doesn't actually move (yet).

      • Make the ellipse move right to start and change directions when it reaches an edge of the window. Slot your answers from the worksheet into the appropriate template as discussed in class.

      • Also make the ellipse change direction when the mouse is clicked. This is still part of the same decision (what direction to move the ellipse in) but is handled in the event handler for the mouse click instead of draw() — you'll need another "update" if. Note that this should only involve adding the event handler — you shouldn't need to change anything in draw() or elsewhere in your sketch.

  4. Your browser does not support the canvas tag.

  5. In this exercise you'll create a sketch where an ellipse starts at the bottom center of the window and moves upward whenever the mouse is moving. The requirements for your sketch:

    To do this:

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

    • Start with the basic sketch structure: open a drawing window and clear the background.

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

    • Complete the sketch:

      • Start with the parts not involving decisions: set up for an animated ellipse that starts at the bottom center of the window but doesn't actually move (yet).

      • Make the ellipse move up when the mouse is moving. Slot your answers from the worksheet into the appropriate template as discussed in class.

      • Make the ellipse's position reset when it moves off the top of the window. Slot your answers from the worksheet into the appropriate template as discussed in class. This should only involve adding to your code, not changing existing parts.

      • Make the ellipse stop moving when the mouse is clicked. Slot your answers from the worksheet into the appropriate template as discussed in class. This should only involve adding to your code, not changing existing parts.

  6. In this exercise you'll create a 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:

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 lab6d 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.