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.
// Use n as a subscript, to select an element of the array.
-
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.
|
|
|
Also, see these DEMO CODE examples:
More examples are available in
folders
"Practice Codes"
and
"Showcase"
.
NOTE:
DEMO CODE is provided for study only
--
to show you how to do similar things in your own code!
Do not include any DEMO CODE
into your submitted code!
(All copied DEMO code is automatically
removed before grading.)
|
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 code changes,
create a new file
(with a name that begins with "p5"),
to save it in your folder.
- Your coding should be discussed directly with me
during regular "Code Reviews"
(a few times per week)!
If there are no files of your preliminary code in your folder,
or if periodic code reviews have not occurred regularly,
then completed code may not be accepted or graded.