CST 112 -- Dog Race (final exam)

Six dogs race horizontally across the screen.

DOGS: (See below for further details.)

EVENTS:

Click this link to see an example (jar file) of a similar race.


NOTE: For this project, you will need to:
// If your name is "Xample Your Zname" then you'll probably need some code like this:
	Dog[] xyz =  new Dog[6];	// Create an array of "dogs" //
	//...
	void setup() {		//// Initialize ////
		size( 800, 650 );
		xyz[0]=  new 	Dog( 100, "Rover" );
		xyz[1]=  new 	Dog( 200, "Fido" );
		// . . .
		xyz[5]=  new 	Dog( 600, "Spot"  );
		//
		reset();
	}
	void reset() {        //// Start all dogs at x=50; random sizes and colors ////
	  for (int j=0; j<many; j++ ) {
	    xyz[j].x=  50;
	    float w=   random(50,80);		// Body width
	    xyz[j].w=  w;
	    xyz[j].h=  w/2 + random(10);	// Head width
	    xyz[j].c=  color( 100+random(150), 50+random(50), random(50) );
	  }
	  gameover=false;
	}
// 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.
		if ( key == 'd' ) {
			// Display top dog (and position) if 'd' was the last key pressed.
			// ...
		}
		// 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 "Dog" class. ////
	class Dog {
		float x=50;		// X coordinate starts at 50.
		float y;		// Y coordiantes for this "dog"
		String name;	// Name of this dog.
		color c;		// Color of this "dog"
		boolean turnaround=false;		// This dog faces left, when true.
		// ...
		// CONSTRUCTORS //
		Dog( float yset, String nameset ) {
			// ...
		}
		// METHODS //
		void show() { // Draw the object at (x,y) //
			// ...
		}
		void move() { // Move the object's x coordinate. //
			// ...
			// ...
			turnaround=true;	// Also face forward.
		}
		boolean hit( float mx, float my) {
			// Return true if (mx,my) is within the rectangle.
			// ...
		}
	}// END of class Dog //

Object Specifications:

Each dog consists of:

Object variables should include the name, color, width, and height of body (rectangle), as well as its coordinates (x,y), plus any other values and boolean flags as needed.

Object methods -- Objects require at least the following methods:

Note that move( ) is NOT called by draw( ); it is called only when an event occurs (space bar). When the dog moves, the legs should "animate" -- with two of the legs slanting either forward or back, as illustrated in these examples: dograce.jar / racer.jar

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 six dogs are lined up at the starting line. (However, the player may "cheat" by clicking on any dog, to move it backward, before the race starts.)


The Race is On!

The six dogs race across the screen, starting at a green "starting line" left (x=50) and moving to the red "finish line" the right (x= width - 50).

Motion of the dogs is controlled by pressing the "space" key. When the space key is pressed, each dog advances horizontally, by a random number from 0 to 10. (There is no vertical motion for the dogs.)

Don't forget to animate the legs on each step.


Darn that mouse!!!

If the mouse is clicked while over a dog's rectangle, that dog turns around (facing to the left) and moves backward by a random number from zero to double its own width.

We have a winner.

The first dog 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 dogs at the starting line, and random sizes and colors.



NOTES