Click on objects . Space bar makes bird drop bomb. Also use keys. (Cheat keys include C,T,W,X)
xxxxxxxxxx
// 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; x<width; x += 40) {
//Make two circles: onse sky=color, one watercolor).
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; x<width+10; x += 20) {
//Make two circles: onse sky=color, one watercolor).
fill( 127, 191, 255 ); // Pale-blue circle.
float r= 20 +3 -random(3);
ellipse(x,y, r,r);
}
}
/////////////////////////// BOAT /////////////////////
void updateBoat() {
//
moveBoat(); // Move the boat.
if (xBoat>width) {
// 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; n<nBoat; n++) {
line( xBoat,yStripe, xBoat+200,yStripe);
yStripe= yStripe + 3;
}
//// Captain on deck.
if (boatSinking) {
drawCaptain( xBoat + 150 + random(60), boatDeck+random(20) );
} else {
drawCaptain( xBoat + 150 , boatDeck );
}
fill(255);
text( nBoat, xBoat+220, boatDeck+20); // Boat # on bow (in white);
}
void drawCaptain( float cx, float cy ) {
//// Draw captain at cs,cy.
fill( 0,0,255 );
stroke( 0,0,255 );
strokeWeight(3);
line( cx,cy, cx+5,cy-20 ); // Legs.
line( cx+20,cy, cx+15,cy-20 );
//// Arms.
fill( 0,0,255 );
stroke( 0,0,255 );
strokeWeight(3);
if (boatSinking) {
//// Arms akimbo.
line( cx-5-random(50),cy-60-random(30), cx+5,cy-55 );
line( cx+25+random(50),cy-60-random(30), cx+15,cy-55 );
} else {
//// Arms on hips.
line( cx+2,cy-30, cx-10,cy-40 );
line( cx+2,cy-50, cx-10,cy-40 );
line( cx+18,cy-30, cx+30,cy-40 );
line( cx+18,cy-50, cx+30,cy-40 );
}
//// Captain's body.
strokeWeight(0);
rect( cx+5,cy-50, 10,30 );
ellipse( cx+10,cy-58, 12,12 );
strokeWeight(0);
stroke(255,0,0);
fill( 0,0,63 );
triangle( cx,cy-65, cx+10,cy-72,cx+20,cy-65 );
}
//////////////////////// BOMB ///////////////////
void updateBomb() { // Handle the bomb,
//// yBomb indicates state of bomb: 0 is no bomb; >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.
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<manyClouds; n++) {
// Position somewhere between top & water level; random size (<= 100x50).
cloudX= random(width/2);
cloudY= random(waterLevel);
cloudW= 50 + random(50);
cloudH= 25 + random(25);
}
} else {
//// Move the groups of clouds.
cloudX += random(20);
cloudY -= random(10);
//// Draw the individual clouds.
float x=cloudX, y=cloudY, w=cloudW, h=cloudH; // cloud positions, etc.
for (int n=0; n<manyClouds; n++) {
noStroke();
fill( 250,250,250, 127);
ellipse( x, y, w, h);
x = x + 100 - random(50); // Move next cloud forward by up to 100.
y = y - 20 + random(10); // Up (or down).
w += -5 - random(10); // Vary cloud sizes
h += -5 + random(2);
}
fill(0);
text( manyClouds + " clouds", 300,100 );
}
}
void fish() {
//// Make a few fishs.
fill( 250,0,100, 50 ); // Bluish gray, translucent.
if (frameCount % 30 == 15) {
// Make a new set of random fish (every second).
manyFish = (int) random(20);
for (int n=0; n<manyFish; n++) {
// Position somewhere between water level and bottom; random size (<= 100x50).
fishX= random(width/2);
fishY= waterLevel + random(height-waterLevel);
fishW= 50 + random(50);
fishH= 25 + random(25);
}
} else {
//// Move the groups of fishs.
fishX += random(20);
fishY += random(10);
//// Draw the individual fishs.
float x=fishX, y=fishY, w=fishW, h=fishH; // fish positions, etc.
for (int n=0; n<manyFish; n++) {
noStroke();
fill( 250,0,100, 50);
//ellipse( x, y, w, h);
x = x + 50 - random(25); // Move next fish forward by up to 100.
y = y + 20 + random(10); // Up (or down).
w += -10 - random(20); // Vary fish sizes
h += -5 + random(3);
triangle(x,y, x-w,y-20, x-w,y+20);
}
fill(0);
text( manyFish + " fish", 350,150 );
}
}