Modify a text file: $/fishes99.java
$/fishes99.java
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 || x
bottom || y
10) dy /= 10; x += dx; y += dy; } boolean isHit( 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; // ++++ fix this later ++++ } //// CONSTRUCTORS //// Fish() {}; // Default constructor. Fish( String s ) { name=s; restart(); } void restart() { x= random(left,right); // random position y= random(top,bottom); dx= random(1,7); // random position dy= 0; w= random(20,120); // random size h= random(20,50); r= int( random(255) ); // random colors g= int( random(255) ); b= int( random(255) ); } }// 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."; String news="r to reset; q to quit; ? for help."; float left=50, top=50, right=550, bottom=350; Button b1; Fish charlie; int many=0; // How many fish? Fish[] school; // void setup() { // Setup screen; instantiate object(s). size( 600, 400 ); reset(); } void reset() { // Reset everything. charlie= new Fish(); charlie.name= "Charlie"; // Array of many Fish // school= new Fish[many]; for (int j=0; j
0) text( many+" fish", width-50, 40 ); // Tank fill( 150,250,200 ); rectMode( CORNERS ); rect( left, top, right, bottom ); } void showAll() { // Show fish, buttons, etc. b1.show(); charlie.show(); for (int j=0; j
0) many--; } if (key == '0') { many=0; school= new Fish[0]; } } void help() { //// Show instructions int x=100, y=100, j=0; fill(0); text( "I N S T R U C T I O N S", x, y+12*j++ ); text( "- - - - - - - - - - - -", x, y+12*j++ ); x= x+20; text( "q: quit", x, y+12*j++ ); text( "r: reset all fish", x, y+12*j++ ); text( "r: reset all fish", x, y+12*j++ ); text( "+: more fish", x, y+12*j++ ); text( "-: fewer fish", x, y+12*j++ ); text( "0: no fish (except Charlie)", x, y+12*j++ ); }