CST 112 -- Racer Project
- Five "racers" race horizontally across the screen.
- Each racer moves ahead (randomly 0-10) whenever the space bar is pressed.
- The user (player) may click on any "racer" to make it fall back (randomly, 0-50).
- A red hat is show on the head of whichever racer is ahead.
- If any racer reaches the finish line, the game is over, and winners are shown..
- Pressing the "R" key restarts the game (with random setting for the 5 racers).
At the end of the game, display a list ow winners: 1st, 2nd, and 3rd.
Click this link to see an example (jar file)
NOTE:
For this project, you will need to:
-
Create a new class of "racer" objects,
with a class name that is the same as
your Last Name
(capitalized, but you may abbreviate it to three or more letters).
-
Make an array of these objects, with an array name the same as your initials
(two or three letters, but in lower case).
Use your own initals (2 or 3) as the name of an array of five (5) "Racer" objects.
The class name for these "Racer" objects should be your Last Name, starting with a Capital Letter,
(You may abbreviate your last name three or more letters.)
// If your name is "Xample Your Zname"
then you'll probably need some code like this:
Zname[] xyz = new Zname[5]; // Create an array of "racers" //
//...
void setup() { //// Initialize ////
size( 800, 600 );
for (...) {
xyz[i]= new Zname( ..., ..., ... );
}
//...
}
//...
|
// The "draw()" method draws all objects, but does NOT move them.
void draw() {
// Draw all objects, but do not move them;
scene(); // background, scene, and text.
// . . . // Show all objects on the screen.
}
void keyPressed() { //// Handle key-press. //
// Determine WHICH key was pressed, and respond to it.
if ( ... ) {
// Space key moves all objects //
// ...
} else if ( ... ) {
// R key restarts the game. //
// ...
}
}
|
// And you will need a class definition something like this:
//// CLASS DEFINITION: Define the Zname class of "racers" ////
class Zname {
float x,y; // Coordiantes of this "racer"
color c; // Color of this "racer"
// ...
// CONSTRUCTORS //
Zname( float xset, float yset, color cset ) {
// ...
}
// METHODS //
void show() { // Draw the object at (x,y) //
// ...
}
void move() { // Move the object's (x,y) coordinates. //
// ...
}
boolean hit( float mx, float my) {
// Return true if (mx,my) is within the rectangle.
// ...
}
}// END of class Zname //
|
Object Specifications:
Each racer consists of a rectangle for the body,
with a yellow circle of 10-pixel radius for the head,
and two thick, blue legs (strokeWeight 5),
plus an optional triangular "hat".
Object variables include
the color, width, and height of body (rectangle),
as well as its coordinates (x,y),
plus any other values and boolean flags as needed.
Sometimes, a racer may wear a red triangular "hat" on top of the head.
Objects require at least the following methods:
- show( ) // display the object on the screen. (Called by draw() ).
- move( ) // change position on the screen. (Called when SPACE bar is pressed.)
- hit( xx, yy ) // Returns true if these coordinates are within the rectangle.
Note that move( ) is NOT called by draw( );
it is called only when an event occurs (space bar).
When the racer moves,
the legs should "animate"
-- with either the left leg or right leg going off at an angle
(based on x coordinate),
as shown in the example.
Making the Scene.
Background should be a light color,
with a thick green "Starting Line" 50 pixels in from the left edge,
and a thick red "Finish Line" 50 pixels in from the right.
Display your name in the lower-left corner of the screen.
At the start of each race, the five racers are lined up at the starting line.
(However, the player may "cheat" by clicking on any racer,
to move it backward, before the race starts.)
The Race is On!
The five racers race across the screenm, starting at a green "starting line"
left (x=50) and moving to the red "finish line" the right (x= width - 50).
Motion of the racers is controlled by pressing the "space" key.
When the space key is pressed, each racer advances horizontally,
by a random number from 0 to 10.
(There is no vertical motion for the racers.)
Don't forget to animate on each step.
Check to see which racer is ahead
and award the "hat" to the racer who is "winning"
(greatest x coordinate).
Darn that mouse!!!
If the mouse is clicked while over a racer's rectangle,
that racer moves backward (to the left) by a random number of pixels from zero to fifty.
We have a winner.
The first racer to cross the finish line is the "winner".
Draw a red circle around the winner when this happens, and the following text
is displayed in the middle of the screen:
G A M E O V E R
Press the "R" key to restart the game, with all racers at the starting line.
NOTES
- All position checking, object moving, and object drawing
must be done with methods of the object.
-
The draw() method should not include any drawing code
-- use object methods to draw objects.
- Each method must begin with a comment that says what it is supposed to do.