Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/////////////////////////////////////////////////////////////////
//                                                             //
//                                                             //
//                                                             //
//                    Helicopter vs Alien                      //
//                                                             //
//                                                             //
// - Move the helicopter up and down by pressing 'w' and 's' - //
//                                                             //
//           -  Move to the right by pressing 'd' -            //
//                                                             //
//                - Shoot by pressing 'SPACE' -                //
//                                                             //
//                                                             //
//                                                             //
//                     by: Joshua Jewer                        //
//                                                             //
//                                                             //
//                                                             //
/////////////////////////////////////////////////////////////////

float river = 0;
float streamX = 0;
float streamXRandom = random(0, 200);
float streamY = random(height*2-12.5, height*2-25);
float streamYRandom = random(height*2, height*2+25);
float streamLength = random(0, 100);
float streamLengthPt2 = random(0, 100);
float streamFat = random(5, 25);
float streamFatty = random(5, 25);
float cloudXRandom = random (0, 250);
float cloudX = 0;
float cloudY = random(0, 400);
float cloudYRandom = random (0, height);
float cloudSizeX = random (75, 175);
float cloudSizeXx = random (75, 175);
float cloudSizeY = random (50, 150);
float cloudSizeYy = random (50, 150);
float alienX = 0;
float alienY = random(height/4+25, height-25);
float heliFlicker = random (200, 255);
float heliFlickerPt2 = random(175, 150);
int heliX = 0;
int heliY = 0;

void setup() {
  size (400, 400);
  smooth();
}

void keyPressed() {
  moveAll();
  resetAll();
  moveHeli();
  shootGun();
  shootGun();
}

void draw() {
  frameRate(90);
  rectMode(CENTER);
  ellipseMode(CENTER);
  background(113, 175, 21);
  alienShot();

  //Draw the river
  fill(200);

  for (int riverOut= 0; riverOut <= width/2; riverOut++) { //Shore of river
    strokeWeight(5);
    stroke(206, 135, 53);
    fill(0, 202, 221);
    rect(river+riverOut, height/2, width*2, 100);
  }
  for (int riverIn = 0; riverIn <= width/2; riverIn++) { //River body
    noStroke();
    fill(175);
    fill(0, 233, 255);
    rect(river+riverIn, height/2, width*2, 80);
  }

  //Draw the streams in river
  for (int streamL = 0; streamL <= width/4; streamL++) {
    streamL += 10;
    strokeWeight(streamFat);
    stroke(214, 255, 241, 200);
    line(streamX+width, streamY+sin(frameCount/22.5)*2.5+cos(frameCount/22.5)*2.5, streamX+width+streamLength, streamY+sin(frameCount/22.5)*2.5+cos(frameCount/22.5)*2.5); 
    strokeWeight(1);
  }
  for (int streamL = 0; streamL <= width/4; streamL++) {
    streamL += 10;
    strokeWeight(streamFatty);
    stroke(214, 255, 241, 200);
    line(streamX+streamXRandom+width, streamYRandom+sin(frameCount/22.5)*2.5+cos(frameCount/22.5)*2.5, streamX+streamXRandom+width+streamLengthPt2, streamYRandom+sin(frameCount/22.5)*2.5+cos(frameCount/22.5)*2.5); //the streams sway up and down
    strokeWeight(1);
  }

  if (streamX >= -275-width) {
    streamX -= 0.125;
  }
  //Draw the helicopter
  fill(99, 99, 99);
  stroke(0);
  rect(heliX+width/10+sin(frameCount/22.5)*2.5, heliY+height/2+cos(frameCount/22.5)*5, 10, 15); 
  rect(heliX+width/10-2+sin(frameCount/22.5)*2.5, heliY+height/2+cos(frameCount/22.5)*5, 35, 5);
  strokeWeight(1);
  line(heliX+width/10+15+sin(frameCount/22.5)*2.5, heliY+height/2+cos(frameCount/22.5)*5, heliX+width/10+20+sin(frameCount/22.5)*2.5, heliY+height/2+cos(frameCount/22.5)*5); //sin and cos make helicopter sway up and down
  noStroke();
  fill(heliFlickerPt2, heliFlickerPt2, heliFlickerPt2, heliFlicker);
  ellipse(heliX+width/10+sin(frameCount/22.5)*2.5, heliY+height/2+cos(frameCount/22.5)*5, 25, 25);
  ellipse(heliX+width/18.5+sin(frameCount/22.5)*2.5, heliY+height/2+4.5+cos(frameCount/22.5)*5, 10, 4);
  heliFlicker = random(200, 255);
  heliFlickerPt2 = random(175, 150);

  //Draw the alien
  strokeWeight(2);
  stroke(38, 255, 164);
  fill(167, 170, 169);
  ellipse(alienX+width, alienY+sin(frameCount/22.5)*2.5+cos(frameCount/22.5)*2.5, 50, 50); //alien sways up and down
  strokeWeight(1);
  stroke(0);
  fill(38, 255, 164);
  ellipse(alienX+width, alienY+sin(frameCount/22.5)*2.5+cos(frameCount/22.5)*2.5, 10, 10);

  if (alienX >= -100-width) { //Alien moves closer
    alienX -= 0.75;
  }

  //Draw the clouds
  fill(255, 200);
  noStroke();
  ellipse(cloudX+width, cloudY, cloudSizeX, cloudSizeY);
  ellipse(cloudX+width+cloudXRandom, cloudYRandom, cloudSizeXx, cloudSizeYy);
  stroke(0);
  
  if (cloudX >= -275-width) { //clouds move with wind
    cloudX -= 0.25;
    cloudY += 0.05;
    cloudYRandom += 0.05;
  }
}


//Move the Helicopter right (but actually moving all of the background to the left)
void moveAll() {
  if (key == 'd') {
    river -= 2.5;
    streamX -= 2.5;
    cloudX -= 5;
    alienX -= 5;
  }
}

void moveHeli() {
  if (key == 'w') {
    heliY -= 5;
  }

  if (key == 's') {
    heliY += 5;
  }

  //Helicopter Constraints
  heliY = constrain(heliY, -175, 175);
}

//Reset everything that passes the left of the screen
void resetAll() {
  if (river <= -width/2) {
    river = 0;
  }
  if (streamX <= -width-275) {
    streamX = width/4;
    //make it all random again
    streamXRandom = random(0, 200);
    streamYRandom = random(height/2, height/2+25);
    streamFat = random(5, 25);
    streamFatty = random(5, 25);
  }
  if (alienX <= -600) {
    alienX = 50;
    //random again
    alienY = random(height/4+25, height-25);
  }
  if (cloudX <= -275-width) {
    cloudX = 100;
    //random again
    cloudXRandom = random (0, 250);    
    cloudY = random(0, 400);
    cloudSizeX = random (75, 175);
    cloudSizeY = random (50, 150);
  }
}

//Shoot your guns
void shootGun() {
  if (key == ' ') {
    frameRate(15);
    stroke(255, 187, 0, 125);
    fill(255, 212, 0, 150);
    ellipse(heliX+width/10-2+sin(frameCount/22.5)*2.5+190, heliY+height/2+cos(frameCount/22.5)*5, 340, 5);
    stroke(0);
  }
}

//Gun collision
void alienShot() {
  if (heliY+175 >= (alienY)-(45) && heliY+175 <= (alienY)+(10) && (key == ' ')) {
    alienX = 50;
    alienY = random(height/4+25, height-25);
  }
}