CST 112 Project #5:
"Billiard Balls"
All code for this project should be saved
in files whose name begins with "p5".
See below for NOTES about
Remote Learning,
participation requirements,
and
Code Review.
I N S T R U C T I O N S
- Write code to define a
Ball
class,
as described below.
Properties should be defined within the object class;
do not use global variables for properties of these objects.
- Construct an array of several Ball objects.
- In your setup() function,
construct each of the several Ball objects
using a for loop to traverse the array.
- Write draw() and other funtions to perform actions described below.
- Write keyPressed() and mousePressed()
event handlers,
as needed.
Ball objects
- Ball objects have properties such as:
- number
- color (Random color, when constructed.)
- position and speed.
- a boolean variable to tell whether ball is "dead" or "alive".
-
Ball objects need methods to do the following:
- show()
- Display the ball (with proper color and number).
- move()
- Move the ball, bouncing off the walls of the table.
- kill()
- Kill this ball (and remove it from the table.
- restore()
- Make ball "alive" again (and reset it).
- reset()
- Set random positions and speeds.
- boolean clicked()
- Return true if mouse click is near.
Action:
- The first ball
b[0]
is called the the "cueball",
which should be WHITE.
- Other balls have different colors
(randomly set when constructed).
- Balls move (unless dead).
- Whenever b[0]
("cueball")
gets near any other ball,
that ball is killed
(and score is increased).
- If all other balls are dead
(a "sweep"),
restore all of them,
and add 10 to score.
- Game is over when score reaches 100.
Event-handlers:
-
Pressing a number key will reset() that ball
(to random position and speed)
-- if it is not "dead."
Note that the ball is reset()
but NOT "restored".
In keyPressed(),
subtracting the value of '0'
from the key
will give the ball number,
as illustrated by the following code:
//// "Reset a ball by pressing key 1, 2, 3, etc.
int n = key - '0'; // n = number of the key.
// n=1 for '1', 2 for '2', etc.
-
Clicking on a ball should also reset it.
Click here to view a working demo.
|
Demo Code & Starter-Code:
-
Demo code for a working program without arrays
can be found HERE:
p5demo.pde
-
Starter Code for your array version can be found
HERE:
p5start.pde
-
Your Project #5
must use an array
containing several ball objects.
NOTES
regarding Remote Learning,
participation requirements,
and
Code Review.
Since we are now in a "Remote Learning" mode, you must
save your code in your folder as you work on this project,
to show your progress.
Participate in periodic Code Reviews to discuss your code
(and to obtain help when needed).
- Each time you make changes to your code,
create a new file
(with a name that begins with "p5"),
to save it in your folder.
- Your coding must be discussed directly with me during periodic "Code Reviews"
(at least a couple of per week)!
- If there are no files with preliminary code in your folder,
or if periodic code reviews do not occur,
then the completed code will not be accepted or graded.