CPSC 120: Lab Assignment 2

Due by the start of class on Friday, 02/10/2017

Home | Calendar | Announcements | Assignments | Example Code | Notes

A Bouncing Ball

Begin by downloading and running the following code example: ball.pde.

This sketch simulates a ball ricocheting around the inside of a box. It makes use of if/else statements to detect when the ball has reached an edge of the box. When it does hit an edge, it "bounces",either vertically or horizontally.

Take a few minutes to study this code, run it, and understand how it works.


Your Job

You are to modify this sketch in four ways:

  1. When the mouse cursor moves outside of the box, the background of the entire window should turn bright red.

  2. By default, the transparency of the ball should be 0, rather than 255. (You may want to comment out the noStroke() command until you're done testing everything.

  3. When the mouse cursor is within a distance of 100 pixels from the center of the ball, the transparency should increase to 10.

  4. When the mouse cursor is inside the ball, the transparency should increas to 255.

For items 3 and 4, here's some code to help with calculating the distance between points (a,b) and (c,d):

float distance = sqrt((c-a)*(c-a)  +  (d-b)*(d-b));

Bonus Fun (strictly optional)

Turn In

Make a folder in your turn in directory named "lab2". Copy all four of your solutions in to this folder. Consult the Linux tutorial from Lab #0 if you need a reminder of the details.


Standards

John H. E. Lasseter