////David Miele - Midterm String title= "M I D T E R M"; String author= "David Miele"; String instructions="q to quit; r to reset; ? for help"; String news=""; //// GLOBAL DECLARATIONS //// boolean day=true; float surface; float left,right,top,bottom; float sunX, sunY, sunDX=1; float moonX, moonY, moonDX=1; float boatX,boatY,boatDX=1,boatDY=0,boatW=100,boatH=20; float octoX, octoY, octoDX, octoDY= -1, octoW= 40, octoH=25; int spacing= 5; int len=20; int boatCounter=0; int octoCounter=0; int score=0; float fishX,fishY,fishDX=2,fishDY,fishW=70,fishH=30; float fishCount=random(1,10); float fishR=0, fishG=0,fishB=255; //// Setup: size, reset, etc. void setup() { size( 600,400 ); surface= height/2; // Set top,left, etc. top= surface; bottom= height+200; left= 30; right=width; // reset(); } //// reset: initialize all values; randomize some. void reset() { day=true; boatX=left; boatX+=boatDX; boatY=surface; octoX=random(left,right); octoY+=octoDY; sunX=left; sunY=50; moonX=left; moonY=50; fishX=left; fishY=random(surface, 300); score=0; boatCounter=0; octoCounter=0; } ////Next frame: scene, boat, octopus, school void draw() { scene(); // Sky and sea; day and night boat(); // Back & forth on surface (always pointing forward). octo(); // Rises slowly (8 legs waving), sinks quickly. school(); // Random number, each slightly smaller, behind, & below. messages(); // Text on screen: title, author, etc. AND a "score" } //// EVENT HANDLERS: mousePressed(), keyPressed() void keyPressed() { if (key=='q')exit(); if (key=='r')reset(); if (key=='d')day(); //if (key=='n')night(); } void mousePressed() { //// Click on boat to stop or start. boatDX=0; } void mouseClicked(){ boatDX=1; //// Click on octopus to destroy it. } //// Check for hits. boolean hit( float octoX, float octoY, float boatX, float boatY, float boatW, float boatH ) { //// Return true if (x,y) is near (xx,yy) score-=100; boatX=left; boatY=80; return true; //} } //// scene: sky, sun, ocean void scene() { sunX +=sunDX; sunY = surface - 200*sin(PI*sunX/width); moonX+=moonDX; moonY=surface + 200*sin(PI*sunX/width); moonX=surface + 200*sin(PI*moonY/width); background( 51, 153, 255 ); //blue sky fill(255, 255, 0);//yellow sun ellipse( sunX, sunY, 50, 50); if(sunX > right){ background(0); fill(255, 204, 204); ellipse(moonX,moonY, 50,50); moonX=left; } if (moonX > right){ background( 51, 153, 255 ); //blue sky fill(255, 255, 0);//yellow sun ellipse( sunX, sunY, 50, 50); } //green ocean fill( 51, 255, 153 ); rect(0, surface, width, height/2); } /** boat: moves back and forth, across the surface. * Red hull points to direction of travel. * Boat stops while fishing. */ void boat() { fill( 255, 0,0 ); noStroke (); rect( boatX + 33, boatY+100, boatW - 55, boatH - 33 ); rect( boatX, boatY+100, boatW, boatH ); noStroke(); if (boatDX > 0){triangle(boatX+80, boatY+100, boatX+100, boatY+120, boatX+120, boatY+100); } else triangle(boatX-30, boatY+100, boatX, boatY+120, boatX, boatY+100); boatX+=boatDX; boatY=80; // boat goes from right to left if(boatX>width-boatW){ boatDX=-boatDX; //boat goes from left to right } if(boatX<0){ boatDX=-boatDX; score+=100; boatCounter++; } } //// octopus void octo() { octoX= 300; //octoCounter++; if (octoY >-130){ octoDY=-1; octoY+=octoDY; } else { octoDY=2; octoY+=octoDY; } if (octoY > bottom){ fill( 102, 0, 204 ); } fill( 102, 0, 204 ); rect( octoX, octoY + 300, octoW, octoH + 30 ); fill( 102, 0, 204 ); ellipse( octoX + 20, octoY + 300, octoW, octoH);// octopus body fill(255,255,255); ellipse( octoX +20, octoY + 295, octoW-25, octoH-35); fill(0); ellipse( octoX +20, octoY + 295, octoW-30, octoH-30); //octopus legs stroke(0); } /** school of fish: smaller 7 smaller, behind & below. */ void school() { for (int i=0;iwidth){ fishX=left; fishY=random(1,10); fishY=random(surface, bottom); } } } //// Text on screen void messages() { fill( 0 ); text( title, width/3, 20 ); text( author, 10, height-15 ); fill(255); text( score, 550, height- 350 ); text( boatCounter, boatX + 40, boatY + 100 ); text( octoCounter, octoX + 20, octoY + 350); help(); } void help() { float x=width*2/3, y=bottom-100; fill(0); text( "q to quit; r to reset; ? for help", x, y ); text( "Click octopus to remove it.", x, y+15 ); text( "Click boat to catch fish", x, y+30 ); }