// Diver catches fish -- Final Exam: arrays of fish, divers, buttons. String author= "Byung Kwan Choi"; String title= "Final Exam for "+author; float surface=100, bottom=480; int score=0,sur; //boolean imp = false; int nf=7, nd=6, nb=nd+2; Fish[] fishArray= new Fish[nf]; Diver[] diverArray= new Diver[nd]; Button[] buttonArray= new Button[nb]; color[] colorArray= { #FF0000, #FF9900, #FFFF00, #00FF00, #00FFFF, #FF00FF, #6666FF, #999999 }; // Colors for divers: 1 2 3 4 5 6 7 8 // RED ORANGE YELLOW GREEN CYAN MAGENTA BLUE GRAY void setup() { // setup // size( 640, 480 ); noStroke(); rectMode(CORNER); restart(); } void restart( ) { // Make fish 50 apart, at y=150, 20, 250, ... float y=surface+50; for (int j=0; j='0'&&key<='9'){int n=key-'0';diverArray[n-1].dive();} } void mousePressed() { // Click handler // // Check buttons. // if (buttonArray[nd].hit( mouseX, mouseY )) { restart(); } if (buttonArray[nd+1].hit( mouseX, mouseY )) { exit(); } // imp = false; // if (buttonArray[0].hit( mouseX, mouseY )) { text( "You clicked button #0", 50,100 ); } /**** REMOVE THIS, IF NECCESSARY. ****/ // Check diver buttons. for (int j=0; jwidth) { reset(); } if (y>height){dy = -.5;} if (y<100){dy=.5;} } void reset() { // New fish. x = random(0,20); dx= random(1,3); randomcolor(); /***************** INSERT YOUR CODE HERE ***************/ } void show() { fill(r,g,b); triangle(x-10,y, x-30, y-8, x-30, y+8); fill(r,g,b); noStroke(); ellipseMode(CENTER); ellipse(x,y,w,h); fill(r,g,b); ellipse(x+10,y-5,5,5); /***************** INSERT YOUR CODE HERE ***************/ text( "Fish", x, y ); // REPLACE WITH YOUR OWN CODE.` } boolean hit( float xx, float yy) { //// True if xx,yy is near this fish. return(dist(xx,yy,x,y)< w); } } class Diver { //// Diver (and boat); float x,y, dx=2,dy=0; int n=0; // Boat number. int caught,total; // How many fish? int r,g,b; color c; int a1,b1,c1,d1; /*************** ADD MORE PROPERTIES HERE **********/ // CONSTRUCTORS // Diver( float x, float y, int n, color c ) { this.x= x; this.y= y; this.n= n; this.c= c; } Diver( float x, float y, int r, int g, int b ) { this.x= x; this.y= y; this.r=r; this.g=g; this.b=b; this.n = nd; } /*************** ADD MORE CONSTRUCTORS HERE **********/ // METHODS: move and show (the diver OR the boat). void show() { a1=30; b1=10; c1=-10; d1=0; // Draw boat & diver (if diving). fill(c); rect(x,surface,40,-20); fill(c); rect(x,surface-10,20,-20); fill(r,g,b); for(int i=0; i <3; i++){ ellipse(x-d1,surface-a1,b1,-c1); a1 += 7; d1 += 2; b1 -=3; c1 +=3; } /*************** ADD YOUR CODE HERE **********/ fill(c); if (dx>0) triangle( x+40,surface, x+40,surface-20, x+40+20,surface-20); if (dx<0) triangle( x,surface, x,surface-20, x-20,surface-20); fill(r,g,b); text( "#"+n, x+5,surface-5 ); text( total, x+20,surface-25 ); // Show total //-- fill(r,g,b); // fill(c); // Diver if (dy>0) { /***************** INSERT YOUR CODE HERE ***************/ fill(c); triangle(x,y, x-30,y+20, x+30,y+20 ); text( caught, x+20,y-10 ); // How many fish caught (on this dive). } if (dy<0) { fill(c); ellipse( x,y, 25,35 ); /***************** INSERT YOUR CODE HERE ***************/ } } void move() { // Move boat/diver. if (dy==0) { // Boat is moving. (Diver is not diving.) if (x<50) dx= -dx; if (x>width-50) dx= -dx; x += dx; /***************** INSERT YOUR CODE HERE ***************/ } else { // Diver is still diving. y += dy; if (y>height-20) dy= -3; if (y0) { // Descending: catch the fish.d caught++; f.reset(); /***************** INSERT YOUR CODE HERE ***************/ } else { // Lose the entire catch, if diver bumps into a fish. caught=0; background(255,0,0); /***************** INSERT YOUR CODE HERE ***************/ } } } boolean hit( float xx, float yy) { //// True if xx,yy is near this diver. return (dist(xx,yy,x,y)< 30); /***************** INSERT YOUR CODE HERE ***************/ } void reset() { //// Reset diver. if (dy==0) x=50; // Boat stops.goes back to left side. if (dy>0) y=surface+50; // If diving, start again at surface. if (dy<0) y=bottom; // If ascending, go back to bottom.. } } class Button { //// Button (kind: 1,2,5) float x, y; float w=60, h=30; int r=200,g=200,b=200; int kind=2; // Rectangular button. int n=0; String s="# "; color c; // CONSTRUCTORS // Button( String s, int kind, float x, float y, color c ) { this.s= s; this.kind= kind; this.x= x; this.y= y; this.c=c; } /*************** ADD MORE CONSTRUCTORS HERE **********/ // METHODS: show button, detect click void show() { // Draw the button //-- fill( r,g,b ); fill(c); if (kind==1) { /***************** INSERT YOUR CODE HERE ***************/ ellipse( x,y, w,h ); } if (kind==2) { rectMode(CENTER); /***************** INSERT YOUR CODE HERE ***************/ rect( x,y, w,h, 15 ); } if (kind==3) { triangle( x,y-h, x-w/2,y+15, x+w/2,y+15 ); } if (kind==5) { rect( x,y, w,h, 15 ); } // Text within button. /***************** INSERT YOUR CODE HERE ***************/ fill(0); text( s, x+10-w/2, y+10 ); /**** REMOVE OR MODIFY THIS, IF NECCESSARY. ****/ } boolean hit( float xx, float yy) { //// True if x,y are within this button. boolean result=false; if (kind==2 || kind==5) { //// True if (xx,yy) is within rectangular button. // return(xx>x-w/2 && xxy-h/2 && yy