// Diver catches fish -- - multiple boats (array). String title= "Boats, Divers and Fishes "; String author= "Adelaida Sanchez"; int numfish = 7 ; int numdiver = 6 ; int numbuttons = 8 ; int nb, nd, nf; int step; Fish[] fishArray = new Fish[numfish]; Diver[] diverArray = new Diver[numdiver]; Button[] buttonArray = new Button[numbuttons]; // Declare and create the Button array float surface=235; float jessStep; float sunX=100, sunY=100; float x, y, w, h; void setup() { // setup // size( 800, 700 ); noStroke(); rectMode(CORNER); x= width/2; y= height/6; w= 10; h= w/3; restart(); //initialize Buttons for (int i = 0; i < buttonArray.length; i++) { buttonArray[0] = new Button("Diver 1",50,80,55,100,190); buttonArray[1] = new Button("Diver 2",150,100,100,55,100); buttonArray[2] = new Button("Diver 3",280,100,200,155,100); buttonArray[3] = new Button("Diver 4",370,100,50,155,100); buttonArray[4] = new Button("Diver 5",480,100,200,255,100); buttonArray[5] = new Button("Diver 6",550,100,50,255,100); buttonArray[6] = new Button("Reset",615,610,250,100,125); buttonArray[7] = new Button("Exit",720,610,255,100,35); } } void restart( ) { // Fish fishArray[0]= new Fish( 0, 65, 400 ); fishArray[1]= new Fish( 0, 55, 300 ); fishArray[2]= new Fish( 0, 50, 270 ); fishArray[3]= new Fish( 0, 60, 340 ); fishArray[4]= new Fish( 0, 70, 380 ); fishArray[5]= new Fish( 0, 75, 420 ); fishArray[6]= new Fish( 0, 78, 450 ); // Diver. diverArray[0]= new Diver( 200, surface, 255,0,0 ); diverArray[1]= new Diver( 250, surface, 155,255,0 ); diverArray[2]= new Diver( 300, surface, 190,255,255 ); diverArray[3]= new Diver( 350, surface, 255,40,255 ); diverArray[4]= new Diver( 400, surface, 0,100,55 ); diverArray[5]= new Diver( 450, surface, 100,100,255 ); } void draw() { // Fish swim back and forth. Divers dive slowly, when button is clicked. scene(); action(); show(); grass (); step = step + 1; if (step % 15 == 0) jessStep = random(-15, 15); // Text. fill(0); text( title, width/3, 30 ); text( author, 20, height-20 ); // Show all the buttons for (int i = 0; i < buttonArray.length; i++) { // buttonArray[i].display(); buttonArray[1].display(); buttonArray[0].showdiver1(); buttonArray[2].showdiver2(); buttonArray[3].display(); buttonArray[4].showdiver2(); buttonArray[5].display(); buttonArray[6].showexre(); buttonArray[7].showexre(); } } void scene() { // Sky & sea. background( 10,200,255 ); // Sea fill( 50,200,100 ); rectMode(CORNER); rect( 0,height/3, width,height ); // Sun if (sunX>width) sunX = 200; fill(255,255,0); ellipse( sunX+600,sunY-60, 40,40); } void show() { // Boat,Diver and Fish. diverArray[0].show(); diverArray[1].show(); diverArray[2].show(); diverArray[3].show(); diverArray[4].show(); diverArray[5].show(); // fishArray[0].show(); fishArray[1].show(); fishArray[2].show(); fishArray[3].show(); fishArray[4].show(); fishArray[5].show(); fishArray[6].show(); } void action() { // Boat,Diver and fish. // Boat,Diver and fish. diverArray[0].move(); diverArray[1].move(); diverArray[2].move(); diverArray[3].move(); diverArray[4].move(); diverArray[5].move(); // fishArray[0].move(); fishArray[1].move(); fishArray[2].move(); fishArray[3].move(); fishArray[4].move(); fishArray[5].move(); fishArray[6].move(); // Check For Divers Net Fish diverArray[0].net( fishArray[0] ); diverArray[0].net( fishArray[1] ); diverArray[0].net( fishArray[2] ); diverArray[0].net( fishArray[3] ); diverArray[0].net( fishArray[4] ); diverArray[0].net( fishArray[5] ); diverArray[0].net( fishArray[6] ); diverArray[1].net( fishArray[0] ); diverArray[1].net( fishArray[1] ); diverArray[1].net( fishArray[2] ); diverArray[1].net( fishArray[3] ); diverArray[1].net( fishArray[4] ); diverArray[1].net( fishArray[5] ); diverArray[1].net( fishArray[6] ); diverArray[2].net( fishArray[0] ); diverArray[2].net( fishArray[1] ); diverArray[2].net( fishArray[2] ); diverArray[2].net( fishArray[3] ); diverArray[2].net( fishArray[4] ); diverArray[2].net( fishArray[5] ); diverArray[2].net( fishArray[6] ); diverArray[3].net( fishArray[0] ); diverArray[3].net( fishArray[1] ); diverArray[3].net( fishArray[2] ); diverArray[3].net( fishArray[3] ); diverArray[3].net( fishArray[4] ); diverArray[3].net( fishArray[5] ); diverArray[3].net( fishArray[6] ); diverArray[4].net( fishArray[0] ); diverArray[4].net( fishArray[1] ); diverArray[4].net( fishArray[2] ); diverArray[4].net( fishArray[3] ); diverArray[4].net( fishArray[4] ); diverArray[4].net( fishArray[5] ); diverArray[4].net( fishArray[6] ); diverArray[5].net( fishArray[0] ); diverArray[5].net( fishArray[1] ); diverArray[5].net( fishArray[2] ); diverArray[5].net( fishArray[3] ); diverArray[5].net( fishArray[4] ); diverArray[5].net( fishArray[5] ); diverArray[5].net( fishArray[6] ); } void grass () { float tilt = 20; if (x < 0) tilt = -20; for (float x = 0; x < width; x += 40) { stroke(0); line(x,height,x+jessStep,height-20); } } class Fish { float x,y, dx=2, dy=0; float w=30,h=10; int r,g,b; int n; // CONSTRUCTORS // Fish( int n, float x, float y ) { this.x= x; this.y= y; this.n= n; this.dx= 2; Color(); } void Color() { r= int( random(0,255) ); g= int( random(0,70) ); b= int( random(10,40) ); } // METHODS: move & show void move() { x = x + dx; y = y + dy; if (x > width) { reset(); } } void reset() { // New fish. x=20; dx= random( 1.50, 3 ); Color(); } void show() { fill(r,g,b); ellipse(x-10,y-5,w,h); triangle( x-w/2+7,y, x-w/2-9,y-9, x-w/2-9, y+9 ); ellipse( x+w/2-5,y-5, 5,5 ); fill(255,0,0); ellipse( x+w/2-5,y-5, 2,2 ); } } class Diver { //// Diver (and boat); float x,y, dx=2,dy=0; int r,g,b; int n=0; int caught=0, total=0; // CONSTRUCTORS // 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; nd++; this.n= nd; } // METHODS: move and show (the diver OR the boat). void show() { // Draw boat & diver (if diving). fill(r,g,b); rect( x,surface, 40,-20 ); // Boat line(x+40,y-50, x+40, y-20); fill(0); text( "#"+n, x+5,surface-5 ); text( total, x+20,surface-25 ); // Show total fill(r,g,b); // Diver if (dy>0) { triangle(x,y, x-30,y+20, x+30,y+20 ); // Diver diving with open net. text( caught, x+20,y-10 ); // How many fish caught (on this dive). } if (dy<0) { ellipse( x,y, 25,35 ); // Diver ascending (with closed net). } } 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; } else { // Diver is still diving. y += dy; if (y>height-20) dy= -3; // Begin ascent! if (y0) { // Descending: catch the fish.d caught++; f.reset(); } else { // Lose the entire catch, if diver bumps into a fish. caught=0; background(255,0,0); // Red flash! } } } } void mousePressed() { if ( buttonArray[0].click(mouseX,mouseY) ) { diverArray[0].dive(); } if ( buttonArray[1].click(mouseX,mouseY) ) { diverArray[1].dive(); } if ( buttonArray[2].click(mouseX,mouseY) ) { diverArray[2].dive(); } if ( buttonArray[3].click(mouseX,mouseY) ) { diverArray[3].dive(); } if ( buttonArray[4].click(mouseX,mouseY) ) { diverArray[4].dive(); } if ( buttonArray[5].click(mouseX,mouseY) ) { diverArray[5].dive(); } if ( buttonArray[6].click(mouseX,mouseY) ) {restart();} if ( buttonArray[7].click(mouseX,mouseY) ) { exit(); } } class Button { // Button Location and Size String s=""; float x = 166; float y = height/2+208 ; float w = 50; float h = 30; int r,g,b; // Constructor All Variables Button( String s, float x, float y, int r, int g, int b ) { this.s= s; this.x= x; this.y= y; this.r= r; this.g= g; this.b= b; } boolean click(float mx, float my) { if (mx < x-w) return false; if (mx > x+w) return false; if (my < y-h) return false; if (my > y+h) return false; return true; } void display() { // Draw the Bottom stroke(0); strokeWeight(2); fill(r,g,b); rectMode(CORNER); // The color changes based on the state of the Button if ( click(mouseX, mouseY)) { fill(200,200,210); } rect(x,y,w,h); text( s, x-w/2+30, y+45 ); } void showexre() { // Draw the Bottom stroke(0); strokeWeight(2); fill(r,g,b); ellipseMode(CORNER); // The color changes based on the state of the Button if ( click(mouseX, mouseY)) { fill(200,200,210); } ellipse(x,y,w,h); text( s, x-w/2+30, y+45 ); } void showdiver1(){ // Draw the Bottom stroke(0); strokeWeight(2); fill(r,g,b); ellipseMode(CORNER); // The color changes based on the state of the Button if ( click(mouseX, mouseY)) { fill(200,200,210); } ellipse(x,y,w,h); text( s, x-w/2+30, y+45 ); } void showdiver2(){ // Draw the Bottom stroke(0); strokeWeight(2); fill(r,g,b); // The color changes based on the state of the Button if ( click(mouseX, mouseY)) { fill(200,200,210); } triangle(x,y-h,x-w/2,y+15,x+w/2,y+15); text( s, x-w/2+30, y+45 ); } }