Modify a text file: $/redBoat1128.pde
$/redBoat1128.pde
// Red boat crosses screen at water level. // bam: '1A28 //// GLOABAL VARIABLES //// float waterLevel=250; int score; float xBoat=0, yBoat=250; // Position of the boat. int nBoat=0; // Boat number. int boatSinking=0; // Countdowns. int bombSplashing=0; float xBirds=0, yBirds=50; // Position of the birds. float xBomb=0, yBomb=1; // Bird & bomb start halfway across the screen. 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 and birds. int manyClouds; float cloudX,cloudY,cloudW,cloudH; void setup() { //// Big screen, initialize. size(800, 600); rectMode(CENTER); ellipseMode(CENTER); waterLevel= 250; frameRate(60); } void draw() { //// update scene, birds, bomb, boat, etc. updateScene(); updateBoat(); updateBirds( ); // Add birds. updateBomb(); } //////// Event handlers (for clicks and keys) void keyPressed() { xBomb= xBirds; yBomb=1; // Bomb starts dropping when y>0; vBomb=1; } //////// 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() { //// Draw scene (sky & water), show title, add clouds. drawScene(); if (score > 10) text( "SCORE: " + score, 400, 10); // Display the score (if > 10); clouds(); // Add some random clouds. } void drawScene() { //// SCENE: background( 127, 191, 255 ); // Pale skies. fill( 63, 127, 63 ); // Ocean. rectMode(CORNERS); rect( 0,waterLevel, width,height ); // Water. rectMode(CENTER); } void updateBirds() { // Birds -- flock of 5. // If birds exist, move them to right. birds(); } void updateBoat() { // moveBoat(); // Move the boat. if (xBoat>width) { // Boat has reached the end. Lower the water level by 50. waterLevel += 50; xBoat=0; nBoat++; // Increase boat number. } drawBoat(xBoat, yBoat); } void moveBoat() { //// Move boat to the right. xBoat += 10; } void drawBoat( float x, float y) { //// Draw red trapezoid. Add stripes and number, later. +++ fill(255,0,0); noStroke(); float boatDeck= waterLevel-50; rectMode(CORNER); rect( xBoat,boatDeck, 200, 50); triangle( xBoat+200,boatDeck, xBoat+250,boatDeck, xBoat+200,waterLevel ); // Add black stripes and white number. stroke(0); strokeWeight(1); float yStripe= boatDeck+3; for (int n=0; n
0 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. xBomb= xBirds; yBomb= yBomb + vBomb; drawBomb( xBomb, yBomb); } /**/ text (xBomb, xBomb, yBomb); /**/ text (yBomb, xBomb, yBomb+10); // If yBomb=0, there is no bomb. } 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; } } void drawBomb(float x, float y) { if (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) { //// 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 birds( ) { //// Flock of (5) birds: triangles 15x5, spaces 15x5. fill( 255, 0, 255); // Magenta. if (xBirds>=width) { //// End of bird-flock. xBirds =0; yBirds= random(waterLevel); // Random height for birds. } else { // Continue flying: increase x by 30; xBirds += 30; /**/ 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 ); // Very light gray. if (frameCount % 30 == 0) { // Make a new set of random clouds (every second). manyClouds = (int) random(10); for (int n=0; n