EVENTS:
// 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 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:
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.)
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.
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.