// Red boat crosses screen at water level. // bam: '1A28 //// GLOABAL VARIABLES //// float waterLevel; int gameFrame=1; int gameNumber=0; float xBoat, yBoat; // Position of the boat. int nBoat=0; // Boat number. boolean boatSinking; // Countdowns. int bombSplashing; float xBirds, yBirds; // Position of the birds. (Offshore, to avoid bombing boats in port.) float xBomb=100, yBomb=0; // Bird & bomb float vBomb=9.81; // Velocity of bomb (downward) -- increases with acceleration of gravity. float gravity= 9.81 / frameRate; // Acceleration of gravity is 9.81 meters per second. //// Additional variables for the clouds, fish, and birds. int manyClouds; float cloudX,cloudY,cloudW,cloudH; int manyFish; float fishX,fishY,fishW,fishH; void setup() { //// Big screen, initialize. size(800, 600); rectMode(CENTER); ellipseMode(CENTER); frameRate(30); restart(); } void restart() { //// Initialize for new game. waterLevel= 250; xBoat=0; yBoat=250; // Position of the boat. nBoat=0; // Boat number. boatSinking=false; bombSplashing=0; // (countdown) xBirds=100; yBirds=50; // Position of the birds. (Offshore, to avoid bombing boats in port.) xBomb=100; yBomb=0; // Bird & bomb vBomb=9.81; // Velocity of bomb (downward) -- increases with acceleration of gravity. gravity= 9.81 / frameRate; // Acceleration of gravity is 9.81 meters per second. } void draw() { //// update scene, birds, bomb, boat, etc. if (gameFrame <= 0) { text( "(Press G key for new game.)", width/2 - 50, height/2 + 100 ); } else { if (key == 'P') { // text( "GAME PAUSED" 200,200); } else { gameFrame++; updateScene(); updateBoat(); updateBirds( ); // Add birds. updateBomb(); } } } //////// Event handlers (for clicks and keys) void mousePressed() { xBirds= 100; // Start a flock of birds! (Offshore.) } void keyPressed() { xBomb= xBirds; yBomb= yBirds; // Bomb starts dropping when y>0; vBomb=1; // Check for specific characters if (gameFrame== 0 && key == 'g') { restart(); gameNumber++; gameFrame=1; } //// Debugging/cheating. if (key == 'C') xBoat = width; if (key == 'S') boatSinking=true; // Sink boat if (key == 'T') {boatSinking=true; yBoat += 150;} // Sink fast to bottom! if (key == 'W') waterLevel=0; // Force a win. if (key == 'X') waterLevel=height; // Force a loss. } //////// Methods to compute "over" etc. //////// boolean isOver( float x1, float y1, float x2, float y2, float margin ) { // Return true if 1 is over 2. if (abs(x1-x2) < margin && abs(y1-y2) < margin ) { return true; // True if within this margin. } else { return false; } } //////// Methods to update -- move & draw. void updateScene() { //// Game over if waterLevel reaches top (0) or bottom (height. if( waterLevel < 50 ) { background( 200 ); text( "GAME OVER -- Player wins!", width/2 - 100, height/2 ); gameFrame=0; // Stops the action. } else if (waterLevel > height-50) { background( 100 ); text( "GAME OVER -- Player loses!", width/2 - 100, height/2 ); gameFrame=0; } else { //// Draw scene (sky & water), show title, add fishs & fish. drawScene(); clouds(); // Add some random clouds. fish(); // Add some random fish. } } void drawScene() { //// SCENE: background( 127, 191, 255 ); // Pale skies. fill( 63, 127, 63 ); // Ocean. rectMode(CORNERS); rect( 0,waterLevel, width,height ); // Water. rectMode(CENTER); drawWaves( waterLevel, 20); } void drawWaves( float y, float w) { //// make waves. noStroke(); text( "HI", 100,100); float x=0; fill( 127, 191, 255 ); // Pale-blue circle. ellipse(x,y, 20,20); fill( 63, 127, 63 ); // Ocean-green circle. ellipse(x+20,y, 20,20); /* for ( x=0; xwidth) { // Boat has reached the end. Lower the water level by 50. New boat. waterLevel += 50; newBoat(); } else if( yBoat>height ) { // Boat finished sinking. Raise water level. New boat. waterLevel -= 50; newBoat(); } drawBoat(xBoat, yBoat); } void newBoat() { // New boat. nBoat++; //Increase boat number. xBoat=0; yBoat= waterLevel; boatSinking=false; } void moveBoat() { //// Move boat to the right. if (boatSinking) { yBoat += 6; } else { xBoat += 10; } } void drawBoat( float x, float y) { //// Draw red trapezoid. Add stripes and number, later. +++ fill(255,0,0); // Draw red boat. if (boatSinking) { fill(127,0,0); // Dark-red if boat is sinking. } noStroke(); float boatDeck= y - 30 - random(10); rectMode(CORNER); rect( xBoat,boatDeck, 200, 50); triangle( xBoat+200,boatDeck, xBoat+250,boatDeck, xBoat+200,boatDeck+50 ); // Add black stripes and white number. stroke(0); strokeWeight(1); float yStripe= boatDeck+3; for (int n=0; n0 is dropping; >= waterLevel splashing. if (yBomb > waterLevel + 20) { // If bomb has finished splashing -- eliminate it. yBomb=0; } else if (yBomb > waterLevel-20) { // If bomb hits water, begin splash, and splash for 5 frames. drawSplash( xBomb, waterLevel); yBomb= yBomb + 1; } else if (yBomb > 0) { // Bomb continues falling. // Bomb is dropping, continue downward. moveBomb(); // Accelerate the bomb. drawBomb( xBomb, yBomb); } if (xBomb > 100 && isOver( xBomb,yBomb, xBoat,yBoat, 50)) { // Check if bomb hit boat (but bombs don't work near start of trip). boatSinking= true; } else { // If yBomb<=0, there is no bomb. -- do nothing. } /**/ text (xBomb, xBomb, yBomb); /**/ text (yBomb, xBomb, yBomb+10); } void moveBomb() { // Make the bomb fall. (Acceleration of gravity is 10 m/s. Let 1o pixels = 10 meters. if (yBomb > 0 && yBomb < height) { // Accelerate bomb, unless it is off the screen. vBomb= vBomb + gravity; yBomb = yBomb + vBomb; xBomb= xBirds; } } void drawBomb(float x, float y) { if (x>100 && y < height) { // Draw bomb, unless it is off the screen. fill( 0, 127, 127 ); ellipse( x, y, 30, 35); } } void drawSplash( float x, float y) { if (x>100 ) { // Draw splash (except when too close to port). //// Draw a splash! fill(255); stroke(255); strokeWeight(2); for (int j=-50; j<50; j+=10) { line(x,y, x-j,y-50); } } } //======================== ADD BIRDS ===========================// void updateBirds() { // Birds -- flock of 5. // If birds exist, move them to right (unless off-screen) if (xBirds >= width) { //// End of bird-flock. xBirds =0; // Birds start offshore. yBirds= random(waterLevel); // Random height for birds. } else if (xBirds > 0) { // Continue flying: increase x by 30; xBirds += 50; drawBirds(); } else { // No birds if xBirds <= 0. (Nothing to do, here.) } } void drawBirds( ) { //// Create a flock of (5) birds: triangles 15x5, spaces 15x5. fill( 255, 0, 255); // Magenta. /**/ stroke(0); text( xBirds, 500,10); float x= (int) xBirds; float y= yBirds; for (int j=0; j<5; j++) { triangle( x,y, x+25,y, x,y+10 ); // Draw one bird. x += 25; // Shift next bird. y += 10; } } //======================== ADD clouds TO SKY ===========================// void clouds() { //// Make a few clouds. fill( 233,233,233, 127 ); // Very light gray. if (frameCount % 30 == 1) { // Make a new set of random clouds (every second). manyClouds = (int) random(10); for (int n=0; n