int nf=7,nd=6,nb=nd+2; Fish[] school=new Fish[nf]; Diver[] boats=new Diver[nd]; Button[] clickers=new Button[nb]; color[] colorArray={#FF0000,#FF9900,#FFFF00,#00FF00,#00FFFF,#FF00FF,#6666FF,#999999}; String title="Diver and fish (version 7: arrays)"; String author="B.A.Martin"; float surface=100,z,bottom=480; int score=0;boolean bang=false; void setup(){size(640,480);noStroke();rectMode(CORNER);restart();}void restart(){ float y=surface+50;for(int j=0;j='0'&&key<='9'){int n=key-'0';boats[n-1].dive();}if(key=='!')bang=!bang;} void mousePressed(){ if(clickers[nd].hit(mouseX,mouseY)){restart();} if(clickers[nd+1].hit(mouseX,mouseY)){exit();} bang=false;for(int j=0;jwidth){reset();} }void reset(){x=0;dx=random(0.25,3);randomcolor();}void show(){ fill(r,g,b);ellipse(x,y,w,h); triangle(x-w/2+8,y,x-w/2-10,y-10,x-w/2-10,y+10); ellipse(x+w/2-6,y-6,6,6);fill(255,0,0);ellipse(x+w/2-6,y-6,3,3); }boolean hit(float xx,float yy){return(dist(xx,yy,x,y)< w);}} class Diver{ float x,y,dx=1,dy=0;int r,g,b;int n=0;int kind=1;int caught=0,total=0;color c; Diver(int k,float x,float y,int n,color c){kind=k;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;nd++;this.n=nd; }void show(){ fill(c);rect(x,z,40,-20); if(kind==1)ellipse(x+20,z-25,20,20); else rect(x+10,z-30,20,20); if(dx>0)triangle(x+40,z,x+40,z-20,x+40+20,z-20); if(dx<0)triangle(x,z,x,z-20,x-20,z-20); fill(0);text("#"+n,x+5,z-5);text(total,x+12,z-20); fill(c);if(dy>0){triangle(x,y,x-30,y+20,x+30,y+20);text(caught,x+20,y-10); }if(dy<0){ellipse(x,y,25,35);}} void move(){if(dy==0){if(x<50)dx=-dx;if(x>width-50)dx=-dx;x += dx; }else{y+=dy;if(y>height-20)dy=-3;if(y0){caught++;f.reset(); }else{caught=0;background(255,0,0);}} }boolean hit(float xx,float yy){ return(dist(xx,yy,x,y)< 30); }void reset(){if(dy==0)x=50;if(dy>0)y=z+20;if(dy<0)y=bottom;}} class Button{float x,y;float w=60,h=30;int r=200,g=200,b=200;int kind=2;int n=0; String s="# ";color c;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; }Button(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;nb++;this.n=nb; }Button(String s,int kind,float x,float y){ this.s=s;this.kind=kind;this.x=x;this.y=y; }void show(){fill(c);if(kind==1){ellipse(x,y,w,h); }if(kind==2){rectMode(CENTER);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);}fill(0); if(n>0)text(s+nb,x+10-w/2,y+4.5); else text(s,x+10-w/2,y+10); }boolean hit(float xx,float yy){if(kind==2||kind==5){ return(xx>x-w/2 && xxy-h/2 && yy