Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
 
           ________   ___   ____
          / __   __| / _ \ |  _ \
    ______> \ | |   |  _  ||    /_____________________________
   / _______/ |_|   |_| |_||_|\______________________________ \
  / /                                                        \ \
 | |                 GEONOSIS OFFENSIVE                       | |
 | |    The C.I.S. is retreating from the planet Geonosis.    | |
 | |     Press down any mouse button to shoot your lasers.    | |
 | |       Destroy as much of the droid army as you can!      | |
 | |               Do not let them escape!                    | |
 | |                                                          | |
 | |                 Aidan Paredes-Morris                     | |
 | |                   October, 21,2016                       | |
 | |                                                          | |
  \ \____________________________    _   ___   ____   _______/ /
   \___________________________  |  | | / _ \ |  _ \ / _______/
                               | |/\| ||  _  ||    / > \        LS
                                \_/\_/ |_| |_||_|\_\|__/
 
 */

//declaring global variables
Background bg;
Droid [] droids=new Droid [10];//creates an array of droids
RepublicShip republicShip;

int shipY;

void setup()
{
  size (800, 600);
  background(255);
  noStroke();

  for (int d=0; d<droids.length; d++)
  {
    droids[d]= new Droid (random(1000, 1200), random(400, 500), random(4, 10), 0);
  }

  //initializing global variables
  bg =new Background ();
  republicShip=new RepublicShip();

  shipY=0;
}

void draw()
{
  bg.update();
  for (int d=0; d<droids.length; d++)
  {
    droids[d].update();
    droids[d].laserCollision();
  }
  republicShip.update();


  //draw background
  bg.display(); 
  //draws droids
  for (int d=0; d<droids.length; d++)
  {
    droids[d].display();
  }
  //draw republic ship
  republicShip.display();
}
class Background
{
  //declaring variables
  int bX1;
  int bX2;
  int bX3;
  int bVelocity;

  int fX1;
  int fX2;
  float fY;
  int fVelocity;

  Background()
  {
    //initializing variables
    bX1=0;
    bX2=200;
    bX3=500;
    bVelocity=1;

    fX1=0;
    fX2=400;
    fY=random(500, 590);
    fVelocity=8;
  }

  void display()
  {
    //draw sky
    background(255, 206, 103);

    //draw ground
    fill(161, 79, 39);
    rect(0, 350, 800, 250);
    //draw spire #1
    rect(bX1+20, 320, 100, 40, 10);
    rect (bX1+30, 240, 30, 80);
    rect(bX1+55, 270, 35, 60, 5);
    rect(bX1+35, 180, 15, 70, 3);
    rect(bX1+37.5, 160, 10, 30, 2);
    //draw spire #2
    rect(bX2+50, 270, 160, 90, 5);
    rect(bX2+30, 320, 20, 40, 5);
    rect(bX2+140, 220, 20, 60, 10);
    rect(bX2+140, 240, 30, 40, 5);
    rect(bX2+105, 200, 50, 80, 5);
    rect(bX2+120, 140, 20, 70, 10);
    rect(bX2+125, 110, 10, 40);
    rect(bX2+125, 80, 5, 40);
    //draw spire#3
    rect(bX3+60, 290, 250, 80, 20);
    rect(bX3+60, 220, 35, 90, 10);
    rect(bX3+70, 200, 20, 30);
    rect(bX3+65, 180, 30, 20, 5);
    rect(bX3+120, 260, 60, 40);
    rect(bX3+130, 240, 50, 30);
    rect(bX3+170, 220, 30, 80);
    rect(bX3+190, 160, 50, 140, 5);
    rect(bX3+220, 80, 70, 220);

    //draw foreground
    fill(190, 100, 0);
    rect(0, 500, 800, 150);
    //draw rocks 
    fill(160, 84, 0);
    ellipse(fX1+100, fY, 30, 5);
    ellipse(fX1-40, fY+10, 15, 4);
    ellipse(fX2+10, fY+20, 40, 4);
    ellipse(fX2+100, fY, 40, 3);
    fill(129, 77, 41);
    arc(fX1+140, 500, 40, 100, PI, TWO_PI);
    ellipse(fX1+140, 500, 40, 15);
    arc(fX2+120, 510, 50, 120, PI, TWO_PI);
    ellipse(fX2+120, 510, 50, 25);
    fill(210, 131, 62);  
    arc(fX1+165, 505, 20, 30, PI, TWO_PI);
    ellipse(fX1+165, 505, 20, 10);
    arc(fX2+60, 505, 40, 50, PI, TWO_PI);
    ellipse(fX2+60, 505, 40, 15);
    arc(fX2+160, 516, 40, 45, PI, TWO_PI);
    ellipse(fX2+160, 516, 40, 10);
  }

  void update()
  {
    //moves background and foreground segments from right to left
    bX1-=bVelocity;
    bX2-=bVelocity;
    bX3-=bVelocity;

    fX1-=fVelocity;
    fX2-=fVelocity;

    //if background and foreground segments reach the leftside, reset them to the right
    if (bX1+200 <0)
    {
      bX1=800;
    }
    if (bX2+300<0)
    {
      bX2=800;
    }
    if (bX3+300<0)
    {
      bX3=800;
    }

    if (fX1+150<0)
    {
      fX1=800;
    }
    if (fX2+400<0)
    {
      fX2=800;
    }
  }
}
class Droid
{
  //declaring variables

  PVector position=new PVector();
  PVector velocity=new PVector();
  color droidColour;

  int run;

  Droid(float x, float y, float velocityX, float velocityY)
  {
    //initializing variables
    position.x=x;
    position.y=y;
    velocity.x=velocityX;
    velocity.y=velocityY;

    run=0;

    droidColour=color(random(255), random(255), random(255)); //sets colours to random
  }
  //checks for laser collision
  void laserCollision() 
  {
    if (mousePressed==true)
    {
      if (mouseX-20>=position.x && mouseX-20 <=position.x+50 && mouseY >=position.y &&mouseY<=position.y+160) //if laser hits droids, reset droid position
      {
        position.x=random(1000, 1200);
        position.y=random(400, 500);
        droidColour=color(random(255), random(255), random(255));
      }
    }
  }

  void display()
  {
    //draw droids
    //dark gray
    fill(40, 53, 57); 
    //middle stomach area
    rect(position.x+24, position.y+35, 9, 25);
    arc(position.x+31, position.y+44, 7, 30, PI+HALF_PI, TWO_PI+HALF_PI);
    //armblaster
    rect(position.x+60, position.y+2.7, 10, 1);

    //these IF statements draw the left arm animation
    if (run<10)
    {
      quad(position.x+15, position.y+20, position.x+10, position.y+40, position.x+17, position.y+42, position.x+20, position.y+20);
      ellipse(position.x+12, position.y+44, 7, 7);
      quad(position.x+15, position.y+46, position.x+10, position.y+50, position.x+24.5, position.y+75, position.x+25, position.y+60);
    } else if (run<20)
    {
      quad(position.x+40, position.y+26, position.x+30, position.y+30, position.x+37, position.y+40, position.x+46, position.y+29);
      beginShape();
      vertex(position.x+43, position.y+16);
      vertex(position.x+41, position.y+21);
      vertex(position.x+42, position.y+26);
      vertex(position.x+45.5, position.y+30);
      vertex(position.x+47, position.y+26);
      vertex(position.x+48, position.y+19);
      endShape(CLOSE);
    }

    //darker gray
    fill(droidColour-20);
    //body
    ellipse(position.x+22, position.y+12, 25, 40);
    quad(position.x+15, position.y+30, position.x+23, position.y+42, position.x+35, position.y+35, position.x+36, position.y+10);
    triangle(position.x+35, position.y+15, position.x+35, position.y+35, position.x+37, position.y+33);
    //waist
    rect(position.x+23, position.y+53, 5, 2);
    quad(position.x+28, position.y+53, position.x+33, position.y+55, position.x+33, position.y+57, position.x+28, position.y+55);
    ellipse(position.x+27.5, position.y+65, 5, 10);

    //light gray
    fill(droidColour);
    //armjoint
    ellipse(position.x+22.5, position.y+6.5, 13, 13);
    //upperarm
    quad(position.x+25, position.y, position.x+45, position.y+4, position.x+45, position.y+12, position.x+25, position.y+13);
    rect(position.x+45, position.y+5, 20, 5);//forearm
    rect(position.x+53, position.y+2, 8, 10, 20);
    triangle(position.x+45, position.y+10, position.x+50, position.y+15, position.x+65, position.y+10);
    rect(position.x+65, position.y+5, 3, 12);
    rect(position.x+62, position.y+14, 5, 3);
    //lightest gray for joints
    fill(92, 119, 129, 150);
    ellipse(position.x+22.5, position.y+6.5, 6, 6);
    ellipse(position.x+45, position.y+8, 4, 4);

    //these IF statements create the running legs animation
    if (run<10)
    {
      //dark gray
      fill(40, 53, 57); 
      //pelvisjoint
      ellipse(position.x+27.5, position.y+65, 10, 20); 
      //foot
      triangle(position.x+7.5, position.y+120, position.x+5, position.y+131, position.x+18, position.y+141);
      //(background leg)
      quad(position.x+27.5, position.y+70, position.x+37, position.y+98, position.x+41, position.y+95, position.x+32, position.y+60);
      quad(position.x+25, position.y+70, position.x+30, position.y+85, position.x+35, position.y+80, position.x+30, position.y+70);
      ellipse(position.x+39, position.y+95, 7, 7);
      quad(position.x+37, position.y+100, position.x+47, position.y+128, position.x+51, position.y+120, position.x+42.5, position.y+95);
      quad(position.x+37, position.y+100, position.x+36, position.y+104, position.x+41, position.y+118, position.x+45, position.y+118);
      triangle(position.x+47.5, position.y+122, position.x+51, position.y+134, position.x+60, position.y+118);
      //legs
      //light gray
      fill(droidColour);
      quad(position.x+22.5, position.y+60, position.x+15, position.y+92, position.x+20, position.y+98, position.x+27.5, position.y+65);
      quad(position.x+17, position.y+65, position.x+14.5, position.y+78, position.x+17.5, position.y+85, position.x+25, position.y+65);
      beginShape();
      vertex(position.x+15, position.y+95);
      vertex(position.x+6.5, position.y+124);
      vertex(position.x+8, position.y+129);
      vertex(position.x+12.5, position.y+125);
      vertex(position.x+13, position.y+118);
      vertex(position.x+20, position.y+100);
      endShape(CLOSE);
      quad(position.x+15, position.y+95, position.x+12.5, position.y+96, position.x+8, position.y+110, position.x+10, position.y+114);
      //darker gray
      fill(droidColour-20);
      ellipse(position.x+24, position.y+65, 15, 15);
      //lightest gray for joints
      fill(92, 119, 129, 150);
      ellipse(position.x+24, position.y+65, 9, 9);
      ellipse(position.x+17, position.y+96, 5, 5);
      ellipse(position.x+9, position.y+127, 5, 5);
      run++;
    } else if (run<20)
    {
      //dark gray
      fill(40, 53, 57); 
      //pelvisjoint
      ellipse(position.x+27.5, position.y+65, 10, 20); 
      triangle(position.x+52, position.y+130, position.x+55, position.y+138, position.x+65, position.y+125);
      //(background leg)
      quad(position.x+25, position.y+70, position.x+20, position.y+105, position.x+22.5, position.y+110, position.x+35, position.y+70);
      quad(position.x+23, position.y+69, position.x+20, position.y+85, position.x+25, position.y+90, position.x+30, position.y+70);
      ellipse(position.x+20, position.y+109, 5, 5);
      beginShape();
      vertex(position.x+20, position.y+105);
      vertex(position.x+6, position.y+125);
      vertex(position.x+6.5, position.y+130);
      vertex(position.x+11, position.y+130);
      vertex(position.x+11.5, position.y+126);
      vertex(position.x+22.5, position.y+115);
      endShape(CLOSE);
      quad(position.x+21, position.y+109, position.x+16.5, position.y+106, position.x+10, position.y+113, position.x+12.5, position.y+120);
      triangle(position.x+7.5, position.y+125, position.x+3, position.y+125, position.x+12, position.y+140);
      //legs
      //light gray
      fill(droidColour);
      quad(position.x+35, position.y+65, position.x+45, position.y+100, position.x+41, position.y+103, position.x+27.5, position.y+70);
      quad(position.x+27.5, position.y+75, position.x+32.5, position.y+90, position.x+35, position.y+90, position.x+31, position.y+70);
      beginShape();
      vertex(position.x+42, position.y+105);
      vertex(position.x+51, position.y+131);
      vertex(position.x+55, position.y+132);
      vertex(position.x+55, position.y+125);
      vertex(position.x+46, position.y+100);
      endShape(CLOSE);
      quad(position.x+45, position.y+105, position.x+41, position.y+110, position.x+45, position.y+120, position.x+50, position.y+120);
      //darker gray
      fill(droidColour-20);
      ellipse(position.x+29, position.y+67, 15, 15);
      //lightest gray for joints
      fill(92, 119, 129, 150);
      ellipse(position.x+29, position.y+67, 9, 9);
      ellipse(position.x+43, position.y+102, 5, 5);
      ellipse(position.x+53, position.y+130, 5, 5);
      run++;
    } else if (run==20)
    {
      run=0;
    }
  }

  void update()
  {
    position.sub(velocity);
    //if droids reach left side of screen, reset them to the right
    if (position.x<0-50) 
    {
      position.x=random(1000, 1200);
      position.y=random(400, 500);
      droidColour=color(random(255), random(255), random(255));
    }
  }
}
class RepublicShip
{
  //declaring variables
  int shipVelocity;

  float angle;
  int anchorX;
  int anchorY;

  RepublicShip ()
  {
    //initializing variables
    shipVelocity=5;


    angle=0;
    anchorX=55;
    anchorY=140+shipY;
  } 

  void display()
  {
    //draw republic ship
    //cockpit
    fill(83, 76, 66);
    rect(170, shipY+20, 15, 30);
    rect(90, shipY-10, 16, 30);
    triangle(230, shipY+40, 230, shipY+60, 245, shipY+60);
    triangle(150, shipY+10, 150, shipY+30, 170, shipY+30);
    fill(37, 170, 241, 50);
    ellipse(200, shipY+40, 80, 60);
    ellipse(120, shipY+15, 80, 60);
    //hull
    fill(240, 207, 164);
    triangle(0, shipY+60, 240, shipY+60, 0, shipY-40);
    rect(0, shipY+0, 80, 180);
    rect(120, shipY+50, 130, 130, 20);
    rect(80, shipY+50, 60, 20);
    rect(80, shipY+170, 60, 10);
    //left door corner
    beginShape();
    vertex(80, shipY+70);
    vertex(80, shipY+90);
    vertex(82.5, shipY+85);
    vertex(85, shipY+80);
    vertex(87.5, shipY+77.5);
    vertex(90, shipY+75);
    vertex(95, shipY+72.5);
    vertex(100, shipY+70);
    endShape();
    //right door corner
    beginShape();
    vertex(120, shipY+70);
    vertex(100, shipY+70);
    vertex(105, shipY+72.5);
    vertex(110, shipY+75);
    vertex(112.5, shipY+77.5);
    vertex(115, shipY+80);
    vertex(117.5, shipY+85);
    vertex(120, shipY+90);
    endShape();
    //missile tubes
    rect(0, shipY-90, 30, 20);
    rect(30, shipY-90, 60, 7.5);
    rect(30, shipY-77.5, 60, 7.5);
    rect(80, shipY-90, 10, 20);
    triangle(0, shipY-70, 0, shipY-60, 20, shipY-70);
    //red highlight
    fill(168, 94, 83);
    triangle(0, shipY-100, 0, shipY-90, 20, shipY-90);
    rect(0, shipY-90, 20, 10);
    fill(240, 207, 164);
    //line details
    stroke(175, 154, 133);
    //(verticals)
    line(20, shipY-90, 20, shipY-70);
    line(30, shipY-27, 30, shipY+169);
    line(60, shipY-14, 60, shipY+179);
    line(135, shipY+20, 135, shipY+180);
    line(170, shipY+40, 170, shipY+110);
    line(190, shipY+50, 190, shipY+110);
    arc(150, shipY+164, 20, 30, HALF_PI, PI+HALF_PI);
    //(horizontals)
    line(0, shipY+10, 30, shipY+10);
    line(30, shipY+30, 60, shipY+30);
    line(60, shipY+60, 135, shipY+60);
    line(120, shipY+140, 135, shipY+140);
    line(135, shipY+130, 160, shipY+130);
    strokeWeight(1.5);
    line(0, shipY+50, 30, shipY+50);
    line(0, shipY+170, 30, shipY+170);
    line(30, shipY+100, 80, shipY+100);
    line(120, shipY+100, 250, shipY+100);
    strokeWeight(1);
    noStroke();
    //red
    fill(168, 94, 83);
    quad(80, shipY+0, 235, shipY+67.5, 240, shipY+60, 70, shipY-12);
    quad(90, shipY+0, 90, shipY+10, 110, shipY+20, 115, shipY+15);
    quad(180, shipY+40, 180, shipY+50, 200, shipY+60, 205, shipY+50);
    quad(240, shipY+40, 230, shipY+60, 240, shipY+60, 242.5, shipY+52.5);
    triangle(70, shipY-12, 105, shipY-13, 95, shipY+0);
    triangle(215, shipY+50, 230, shipY+60, 240, shipY+50);
    quad(160, shipY+10, 150, shipY+25, 180, shipY+40, 177.5, shipY+15);
    //green
    fill(181, 155, 55);
    triangle(170, shipY+110, 150, shipY+150, 170, shipY+130);
    rect(170, shipY+110, 60, 20);
    rect(230, shipY+110, 20, 50);
    rect(185, shipY+120, 65, 60, 20);
    fill(240, 207, 164);
    rect(170, shipY+130, 70, 50, 20);
    //front gun mount
    fill(56, 52, 46);
    ellipse(200, shipY+155, 40, 40);
    fill(83, 76, 66);
    ellipse(210, shipY+155, 20, 30);
    //pod mount
    rect(40, shipY+130, 30, 20);
    fill(56, 52, 46);
    rect(45, shipY+135, 20, 10);
    //black stripes
    ellipse(15, shipY+110, 10, 100);
    ellipse(126.25, shipY+162.5, 1.5, 15);
    ellipse(131.25, shipY+162.5, 1.5, 15);
    //front gun barrel
    rect(215, shipY+152.5, 60, 5);
    ellipse(215, shipY+155, 3, 5);
    rect(260, shipY+151, 2.5, 8);


    //draw side turret
    /*
    *UNUSABLE CODE FOR SIDE TURRET ROTATION*
     mouseX=constrain(mouseX, 230, 800);
     mouseY=constrain(mouseY, 0, 580);
     translate(anchorX, anchorY+shipY);
     angle = -atan2(anchorX-mouseX, anchorY-mouseY);
     rotate(angle-HALF_PI);
     */

    fill(83, 76, 66);
    rect(10+(mouseX/50), shipY+130+(mouseY/-50), 10, 25);
    fill(37, 170, 241, 50);
    ellipse(55+(mouseX/50), shipY+140+(mouseY/-50), 80, 80);
    fill(240, 207, 164);
    stroke(175, 154, 133);
    strokeWeight(2);
    triangle(65+(mouseX/50), shipY+110+(mouseY/-50), 88+(mouseX/50), shipY +130+(mouseY/-50), 60+(mouseX/50), shipY+115+(mouseY/-50));
    beginShape();
    vertex(35+(mouseX/50), shipY+140+(mouseY/-50));
    vertex(65+(mouseX/50), shipY +110+(mouseY/-50));
    vertex(75+(mouseX/50), shipY +110+(mouseY/-50));
    vertex(79+(mouseX/50), shipY +110+(mouseY/-50));
    vertex(74+(mouseX/50), shipY +106+(mouseY/-50));
    vertex(68+(mouseX/50), shipY +103+(mouseY/-50));
    vertex(63+(mouseX/50), shipY +101+(mouseY/-50));
    vertex(55+(mouseX/50), shipY +100+(mouseY/-50));
    vertex(44+(mouseX/50), shipY +102+(mouseY/-50));
    vertex(30+(mouseX/50), shipY +110+(mouseY/-50));
    vertex(21+(mouseX/50), shipY +120+(mouseY/-50));
    vertex(19+(mouseX/50), shipY+125+(mouseY/-50));
    vertex(15+(mouseX/50), shipY +140+(mouseY/-50));
    endShape();
    beginShape();
    vertex(35+(mouseX/50), shipY+140+(mouseY/-50));
    vertex(70+(mouseX/50), shipY +177+(mouseY/-50));
    vertex(63+(mouseX/50), shipY+179+(mouseY/-50));
    vertex(55+(mouseX/50), shipY+180+(mouseY/-50));
    vertex(45+(mouseX/50), shipY +178+(mouseY/-50));
    vertex(32+(mouseX/50), shipY +170+(mouseY/-50));
    vertex(22+(mouseX/50), shipY +160+(mouseY/-50));
    vertex(19+(mouseX/50), shipY +155+(mouseY/-50));
    vertex(15+(mouseX/50), shipY +140+(mouseY/-50));
    endShape();
    rect(15+(mouseX/50), shipY+135+(mouseY/-50), 80, 10);
    noStroke();
    quad(87+(mouseX/50), shipY+140+(mouseY/-50), 87+(mouseX/50), shipY+135+(mouseY/-50), 90+(mouseX/50), shipY+130+(mouseY/-50), 90+(mouseX/50), shipY+140+(mouseY/-50));
    quad(87+(mouseX/50), shipY +140+(mouseY/-50), 87+(mouseX/50), shipY +145+(mouseY/-50), 90+(mouseX/50), shipY +150+(mouseY/-50), 90+(mouseX/50), shipY+140+(mouseY/-50));
    rect(90+(mouseX/50), shipY +130+(mouseY/-50), 7, 20);
    //(yellow stripe)
    fill(185, 126, 34);
    rect(55+(mouseX/50), shipY+101+(mouseY/-50), 5, 10);
    //(turret)
    fill(56, 52, 46);
    rect(95+(mouseX/50), shipY+137.5+(mouseY/-50), 7.5, 5);
    rect(97+(mouseX/50), shipY +135+(mouseY/-50), 2.5, 10);
    fill(83, 76, 66);
    rect(99.5+(mouseX/50), shipY +130+(mouseY/-50), 2, 20);
    arc(100+(mouseX/50), shipY +140+(mouseY/-50), 3, 20, HALF_PI, PI+HALF_PI);

    //(laser)
    //if mouse is pressed, draw laser at mouse position
    if (mousePressed==true)
    {
      stroke(78, 242, 22);
      strokeWeight(5);
      line(105+(mouseX/50), shipY+140+(mouseY/-50), mouseX+(mouseX/50)-10, mouseY+(mouseY/-50));
      strokeWeight(2);
      stroke(255);
      line(105+(mouseX/50), shipY+140+(mouseY/-50), mouseX+(mouseX/50)-10, mouseY+(mouseY/-50));
      noStroke();
    }
  }

  void update()
  {
    //if UP arrow is pressed, move ship up, if DOWN arrow is pressed, move ship down
    if (keyPressed)
    {
      if (keyCode==UP)
      {
        shipY=shipY-shipVelocity;
      } else if (keyCode==DOWN)
      {
        shipY=shipY+shipVelocity;
      }
      shipY=constrain(shipY, 0, 250);
    }
  }
}