CPSC 120 Principles of Computer Science Fall 2024

Lab 5
Making Choices

Due: Fri 10/4 at the start of class

Labs are due at the start of class. It is OK if you show up and copy your files to the handin directory at the very beginning of class, but this should take at most a couple of minutes and you should not spend the next lab period finishing up the previous week's lab. I will check timestamps, and files handed in more than five minutes after the start of lab will be considered late.

See the policy on late work and extensions.


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, later in the course.) 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: an on-the-spot decision, where one can look at the situation in the current frame (i.e. the values of system variables and animation variables) and determine what to do, and state machines, where additional knowledge about the past or what has been happening is needed in order to determine what to do. 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 a state machine — 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, so we need to introduce a state variable to make that information available in the current frame.

Successfully completing this lab means that you are able to:

Academic Integrity and Collaboration

The short version:

Review the discussion in previous lab handouts and the full collaboration policy for more details.

Handin

To hand in your work:


Preliminaries

Getting Started

Before you start writing code, review the process of working with conditionals — the posted slides and in-class exercises handouts contain the conditionals questions and the syntax for if statements, and the posted examples provide an illustration of answering those questions in a particular situation and how those answers turn into code. Look through the examples and make sure you understand what the code does and how it matches up to the relevant questions.

As always, practice incremental development. You don't have to code the entire sketch at once — start with one piece or a simpler version of the task and add to it bit by bit.


Exercises

Put your name and a description of the sketch in comments at the beginning of each sketch. Also don't forget to Auto Format your code before handing it in. (Refer back to lab 2 for more on Auto Format if needed.)

Exercise 1

Your browser does not support the canvas tag.

The goal in this exercise is to create a sketch named lab5a which works like the example shown — a small ellipse starts in the center of the drawing window and, when the mouse comes close to it, it runs away from the mouse. 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.

Make the drawing window 400x400 and define "close to the mouse" as "the mouse position is within 100 pixels of the center of the ellipse". You can 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.

To create this sketch:

Exercise 2

Your browser does not support the canvas tag.

The goal in this exercise is to create a sketch named lab5b where a small ellipse starts in the center of the drawing window, moving right. There are two situations in which the ellipse's direction can change:

To create this sketch:

Exercise 3

Create a sketch named lab5c which contains an ellipse that starts at the bottom center of the window and moves upward whenever the mouse is moving. The ellipse's position should reset to the bottom when it goes off the top of the window. Clicking the mouse should stop the ellipse — after a click, the ellipse shouldn't move any more, even if the mouse is moved or clicked again.

The system variables pmouseX and pmouseY contain the mouse's previous position — you can determine if the mouse is moving by checking whether the current position is different from the previous position.

To create this sketch, follow a procedure similar to the previous exercises: start with the parts that don't involve making choices (an animated ellipse starting at the bottom center of the window), then work on the elements involve making choices one at a time — first consider the upward movement of the ellipse, then handle resetting at the bottom when it moves off the top of the window, and finally handle the mouse clicks. For each, go through the conditionals questions, starting with determining whether it is an on-the-spot decision or a state machine.

Exercise 4

Create an interactive and/or animated sketch of your own design. (Name this sketch lab5d.) What the sketch depicts is up to you (here's a chance to be creative!) but for full credit it must include the following required elements:

Extra Credit

You can earn extra credit by going substantially beyond the required elements. Add to your sketch for #4 and include a brief description of what you've done for extra credit in a comment at the beginning of your sketch. Some possibilities: