class Fish { // Fish gotta swim, birds gotta fly ... float x=100,y=100, dx=3,dy=0, w=80,h=30; int r,g,b; String name=""; // METHODS // void show() { // Draw the fish. fill(r,g,b); ellipse( x,y, w,h ); float front=x+w/2, back=x-w/2; if ( dx>0 ) { triangle( back,y, back-20,y-10, back-20,y+10 ); } else { triangle( front,y, front+20,y-10, front+20,y+10 ); } float eye; eye= ( dx>0 ) ? front-15 : back+15; fill(255); // eye strokeWeight(1); ellipse( eye, y-10, 10, 10 ); text( name, x-10,y ); } void move() { //... if (x>right || xbottom || y 10) dy /= 10; x += dx; y += dy; // Fish out of tank -- go to center. if (xright+10) x= (left+right) / 2; if (ybottom+10) y= (left+right) / 2; } boolean hit( float x, float y ) { // Return true if (x,y) is within this fish! if (x < this.x-w/2 || x > this.x+w/2) return false; if (y < this.y-h/2 || y > this.y+h/2) return false; return true; // Hit detected. } boolean overlap( Fish other ) { // Return true fish overlap each other. if (this.x+this.w/2 < other.x-other.w/2 ) return false; // My right end < other left end. if (this.x-this.w/2 > other.x+other.w/2 ) return false; if (this.y+this.h/2 < other.y-other.h/2 ) return false; // My top < other bottom if (this.y-this.h/2 > other.y+other.h/2 ) return false; return true; // Overlap detected. } void bounce( Fish other ) { /// exchange velocities (dx and dy). Also flash my color. float tmp; tmp= this.dx; this.dx= other.dx; other.dx= tmp; tmp= this.dy; this.dy= other.dy; other.dy= tmp; // Big fish goes up & little fish goes down. if (this.h > other.h) { this.y -= this.h/2; other.y -= other.h/2; } // Also move away this.move(); other.move(); // bgr= this.r; // Change background to last collision color. bgg= this.g; bgb= this.b; } //// CONSTRUCTORS //// Fish() {}; // Default constructor. Fish( String s ) { name=s; restart(); } void restart() { r= int( random(255) ); // random colors g= int( random(255) ); b= int( random(255) ); w= random(20,120); // random size h= random(20,50); reposition(); } void reposition() { x= random(left,right); // random position y= random(top,bottom); dx= random(1,7); // random speed dy= 0; } }// END class Fish // //We'll also need a "Button" class, but leave it empty for now. class Button { // Button float x=20,y=20, w=80,h=30; String name="Click me!"; void show() { // Show the button // fill( 256 ); strokeWeight(4); rectMode( CORNER ); fill(255,255,0); rect( x,y, w,h ); fill(0,0,255); text( name, x+10, y+20 ); } } //Just make one fish, for now. //See how it swims and bounces, see how it looks, make sure the eye and tail are OK. // GLOBALS: just the objects and the scene (boundaries, etc.) String title="Practice project // Object-based fish - collisions."; String news="r to reset; q to quit; ? for help."; int bgr=100, bgg=150, bgb=200; float left=50, top=50, right=550, bottom=350; Button b1; int many=3; // How many fish? Fish[] school; // void setup() { // Setup screen; instantiate object(s). size( 600, 400 ); reset(); } void reset() { // Reset everything. // Array of many Fish // school= new Fish[many]; for (int j=0; j0) text( many+" fish", width-50, 40 ); // Tank fill( 150,250,200 ); rectMode( CORNERS ); rect( left, top, right, bottom ); } void showAll() { // Show fish, buttons, etc. for (int j=0; j0) many--; } if (key == '0') { many=0; school= new Fish[0]; } if (key == 's') { // Slow down all fish by -25% for (int i=0; i