// Diver catches fish -- Final Exam: arrays of fish, divers, buttons. String author= "Christofer Chamorro"; String title= "Final Exam for "+author; float surface=100, bottom=480; 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 ); restart(); } void restart( ) { // Make fish 50 apart, at y=150, 20, 250, ... float y=surface+50; for (int j=0; j width +20){ reset(); } //determines when the fish resets } void reset() { // New fish. x=0; randomcolor(); // random color when reset// dx= random(.5,2); //Random speed when reset// /***************** INSERT YOUR CODE HERE ***************/ } void show() { fill(r,g,b); fish(); } void fish(){ //draws the fish ellipseMode(CENTER); ellipse(x,y,w,h); triangle(x - w / 2 + 3, y, x - w, y - h / 2, x - w, y + h / 2); } boolean hit( float xx, float yy) { //// True if xx,yy is near this fish. boolean result=true; /***************** INSERT YOUR CODE HERE ***************/ if ( dist(mouseX, mouseY, x,y) < 25 ) reset(); return result; } } class Diver { //// Diver (and boat); float x,y, dx=2,dy=0; float h = 40; float w = 30; int n=0; // Boat number. int caught,total; // How many fish? int r,g,b; color c; /*************** 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; } /*************** ADD MORE CONSTRUCTORS HERE **********/ // METHODS: move and show (the diver OR the boat). void show() { // Draw boat & diver (if diving). rectMode(CENTER); fill(0); rect(x,surface,w,h); 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(0); text( "#"+n, x+5,surface-5 ); text( total, x+20,surface-25 ); // Show total fill(r,g,b); fill(0); // Diver if (dy>0) { /***************** INSERT YOUR CODE HERE ***************/ text( caught, x+20,y-10 ); // How many fish caught (on this dive). } if (dy<0) { /***************** INSERT YOUR CODE HERE ***************/ } } void move() { // Move boat/diver. if (dy==0) { // Boat is moving. (Diver is not diving.) /***************** INSERT YOUR CODE HERE ***************/ x+=dx; } else { // Diver is still diving. x=0; /***************** INSERT YOUR CODE HERE ***************/ } } void dive() { //// Stop the boat & start the dive. y= surface+50; dy=1; caught=0; } void net( Fish f ) { //// Check if diver nets fish if (dy==0) return; // Not diving. if (dist(x,y, f.x,f.y) < 50) { // Fish is near. if (dy>0) { // Descending: catch the fish.d /***************** INSERT YOUR CODE HERE ***************/ } else { // Lose the entire catch, if diver bumps into a fish. /***************** INSERT YOUR CODE HERE ***************/ } } } boolean hit( float xx, float yy) { //// True if xx,yy is near this diver. boolean result=true; /***************** INSERT YOUR CODE HERE ***************/ return result; } 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=3; // 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) { /***************** INSERT YOUR CODE HERE ***************/ rect(x,y,w,h); } 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. // /***************** INSERT YOUR CODE HERE ***************/ } else { // Use dist() for other kinds. result= ( dist(xx,yy, x,y) < w/2 ); } return result; } }