EVENTS: Three buttons: MOVE, RESET, QUIT
// If your name is "Xample Your Zname"
then you'll probably need some code like this:
int many=5;
Dog[] xyz = new Dog[5] // Create an array of "dogs" //
Button bmove, breset, bquit; // Buttons. //
float start=50;
// ...
void setup() { //// Initialize ////
size( 800, 650 );
xyz[0]= new Dog( 100, "Rover" );
xyz[1]= new Dog( 200, "Fido" );
// . . .
xyz[4]= new Dog( 500, "Spot" );
reset();
}
void reset() { //// Start all dogs at x=xxistart50; random sizes and colors ////
for (int j=0; j<many; j++ ) {
xyz[j].x= start;
// . . .
|
// The "draw()" method draws all objects, but does NOT move them.
void draw() {
scene(); // background, scene, and text.
// Show all dogs, but do not move them;
for (int j=0; j<many; j++ ) {
xyz[j].show();
}
// Show buttons
bmove.show(); // . . .
void mousePressed() {
if ( bmove.hit( mouseX, mouseY ) ) moveall();
// If any dog was clicked, turn himn around.
for (int j=0; j<many; j++) {
if ( xyz[j].hit( mouseX, mouseY ) ) { // . . .
void keyPressed() { //// Handle key-press. //
if ( key == ' ' ) { moveall(); } // . . .
void moveall() {
// Move all objects (unless game over) //
if (! gameover ) {
for (int j=0; j<xyz.length; j++) {
xyz[j].move();
}
}
|
// And you will need a class definition something like this:
//// CLASS DEFINITION: Define the "Dog" class. ////
class Dog {
float x=start,y;
String name; // Name of this dog.
color c; // Color of this "dog"
// ...
//// CONSTRUCTOR(S) ////
Dog( float yset, String nameset ) {
this.y= yset;
this.name= nameset;
}
//// METHODS ////
void show() {
// Draw the object at (x,y) // . . . }
void move() {
// Move the object's x coordinate. // . . . }
boolean hit( float mx, float my) {
// Return true if (mx,my) is within button; // . . . }
}// END of class Dog //
|
Dog properties: name, color, width & height of body (rectangle); coordinates (x,y); plus other values and booleans as needed. Each dog has random size (50-100 wide), and random (brownish) color.
Dog methods -- Objects require at least the following methods: