//AUTHOR: William Ye + BAM // Diver catches fish -- Final Exam: arrays of fish, divers, buttons. //Perfect clone of example... I'll eat my socks if I don't get a 100 String author= "William Ye"; String title= "Final Exam for "+author; float surface=150, 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 ); noStroke(); restart(); textAlign(CENTER, CENTER); //Centers text } void restart( ) { for (int i=0; i width || x < 0) { dx *= -1; } } void reset() { x = 10; dx = random(1, 3); randomColor(); } void show() { fill(c); ellipse(x, y, 40, 30); if (dx > 0) { //if fish is swimming to the right triangle(x - 20, y - 20, x - 20, y + 20, x, y); } else { //if it's swimming to the left triangle(x + 20, y - 20, x + 20, y + 20, x, y); } } boolean hit( float x, float y) { //// True if xx,yy is near this fish. if ( dist(x, y, this.x, this.y) < 25 ) { reset(); return true; } return false; } } 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; // 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; } // METHODS: move and show (the diver OR the boat). void show() { // Draw boat & diver (if diving). fill(c); if (dx>0) { triangle(x, surface, x + 20, surface - 20, x, surface - 20); rect(x - 20, surface - 20, 20, 20); if (n % 2 == 1) { ellipse(x - 5, surface - 20, 15, 15); } else { rect(x - 10, surface - 30, 15, 15); } } if (dx<0) { triangle(x, surface, x - 20, surface - 20, x, surface - 20); rect(x , surface - 20, 20, 20); if (n % 2 == 1) { ellipse(x + 5, surface - 20, 15, 15); } else { rect(x - 10, surface - 30, 15, 15); } } fill(0); text( "#"+n, x+5,surface-5 ); text( total, x+20,surface-25 ); // Diver fill(c); if (dy>0) { triangle(x, y, x - 30, y + 30, x + 30, y + 30); text( caught, x+20,y-10 ); // How many fish caught (on this dive). } if (dy<0) { ellipse(x, y + 15, 30, 50); text( caught, x+20,y-10 ); } } void move() { // Move boat/diver. if (dy==0) { //!diving x += dx; if (x > width || x < 0) { dx *= -1; } } else {//diving y += dy; if (dy > 0) { //goin down if (y + 20 > height) dy = -5; } else { //goin up if (y <= surface) { dy = 0; total += caught; } } } } 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) { caught++; f.reset(); } else { caught = 0; f.reset(); } } } boolean hit( float xx, float yy) { if (dist(x, y, xx, yy) < 30) { return true; } return false; } 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; } // METHODS: show button, detect click void show() { // Draw the button //-- fill( r,g,b ); fill(c); if (kind==1) { ellipse( x,y, w,h ); } if (kind==2) { rect( x - w/2,y - h/2, w,h ); } // Text within button. if (kind == 3) { triangle(x, y - 30, x + 40, y + 20, x - 40, y + 20); } fill(0); textSize(15); text( s, x, y ); } boolean hit( float xx, float yy) { //// True if x,y are within this button. if (kind==2) { if (xx > x - w/2 && xx < x - w/2 + w && yy > y - h/2 && yy < y - h/2 + h) { return true; } } else {// Use dist() for other kinds. fill( 0, 0, 0); if ( dist(xx,yy, x,y) < w/2 ) return true; } return false; } }