Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Wesley Cox
//Star Destroyer
//Credit goes to Khan-ali Ibrahim's paralax interactive drawing for essentially the whole concept and a large mass of code used on mainly the walls and ship



void setup() 
{
  //creates window size
  size(400, 400);
  //removes cursor
  noCursor();
}
//Declaring all necessary variables
int counter;
float tunnelX = 150;
float tunnelY = 150;
float tunnelLength = 100;
float tunnelWidth = 100;
float laserX;
float laserY;
float laserWidth;
float laserHeight;
float position;
float randomStarX = 200;
float randomStarY = 200;
float starWidth = 0;
float starHeight = 0;
float RNG = 0;
boolean laser = false;
boolean star = false;


void draw() 
{
  frameRate(120);

  background(0);  
  if (star == false)
  {
    starStasis();
  }
  if (star == true)
  {
    starAttack();
  }
  if (laser == true)
  {
    laserShoot();
  }
  //Creating the graphics for the background and ship
  if (mouseX<width/2) {
    fill(random(200), 0, 0, 100*sin(frameCount*0.05));
  }
  if (mouseX>width/2) {
    fill(0, random(200), 0, 100*sin(frameCount*0.05));
  }
  if (mouseY<width/2) {
    fill(0, 0, random(200), 100*sin(frameCount*0.05));
  }

  ///Walls and Tunnel
  stroke(100, 100, 255);
  quad(0, 0, 0, 400, 100 - (mouseX - width/2) * 3, 300 - (mouseY - width/2) * 3, 100 - (mouseX - width/2) * 3, 100 - (mouseY - width/2) * 3); //Left Wall
  quad(0, 400, 400, 400, 300 - (mouseX - width/2) * 3, 300 - (mouseY - width/2) * 3, 100 - (mouseX - width/2) * 3, 300 - (mouseY - width/2) * 3); //Lower Wall
  quad(400, 400, 400, 0, 300 - (mouseX - width/2) * 3, 100 - (mouseY - width/2) * 3, 300 - (mouseX - width/2) * 3, 300 - (mouseY - width/2) * 3); //Right Wall
  quad(400, 0, 0, 0, 100 - (mouseX - width/2) * 3, 100 - (mouseY - width/2) * 3, 300 - (mouseX - width/2) * 3, 100 - (mouseY - width/2) * 3); //Upper Wall;
  quad(100-(mouseX-width/2)*3, 100-(mouseY-width/2)*3, 100-(mouseX-width/2)*3, 300-(mouseY-width/2)*3, 300-(mouseX-width/2)*3, 300-(mouseY-width/2)*3, 300-(mouseX-width/2)*3, 100-(mouseY-width/2)*3); //Center Tunnel


  //Body of the ship
  quad(180 + (mouseX - width/2) * 2.5, 180 + (mouseY - width/2) * 2.5, 220 + (mouseX - width/2) * 2.5, 180 + (mouseY - width/2) * 2.5, 220 + (mouseX - width/2) * 2, 200 + (mouseY - width/2) * 2, 180 + (mouseX - width/2) * 2, 200 + (mouseY - width/2) * 2);
  quad(180 + (mouseX - width/2) * 2.5, 220 + (mouseY - width/2) * 2.5, 220 + (mouseX - width/2) * 2.5, 220 + (mouseY - width/2) * 2.5, 220 + (mouseX - width/2) * 2, 200 + (mouseY - width/2) * 2, 180 + (mouseX - width/2) * 2, 200 + (mouseY - width/2) * 2);
  line(180 + (mouseX - width/2) * 2.5, 180 + (mouseY - width/2) * 2.5, 180 + (mouseX - width/2) * 2.5, 220 + (mouseY - width/2) * 2.5);
  line(220 + (mouseX - width/2) * 2.5, 180 + (mouseY - width/2) * 2.5, 220 + (mouseX - width/2) * 2.5, 220 + (mouseY - width/2) * 2.5);
  //Wings of the ship
  triangle(180 + (mouseX - width/2) * 2, 200 + (mouseY - width/2) * 2, 180 + (mouseX - width/2) * 2.5, 200 + (mouseY - width/2) * 2.5, 120 + (mouseX - width/2) * 2.5, 200 + (mouseY - width/2) * 2.5);
  triangle(220 + (mouseX - width/2) * 2, 200 + (mouseY - width/2) * 2, 220 + (mouseX - width/2) * 2.5, 200 + (mouseY - width/2) * 2.5, 280 + (mouseX - width/2) * 2.5, 200 + (mouseY - width/2) * 2.5);


  //Outline of the ship
  strokeWeight(2);
  noFill();
  quad(180 + (mouseX - width/2) * 2.5, 180 + (mouseY - width/2) * 2.5, 220 + (mouseX - width/2) * 2.5, 180 + (mouseY - width/2) * 2.5, 220 + (mouseX - width/2) * 2, 200 + (mouseY - width/2) * 2, 180 + (mouseX - width/2) * 2, 200 + (mouseY - width/2) * 2);
  quad(180 + (mouseX - width/2) * 2.5, 220 + (mouseY - width/2) * 2.5, 220 + (mouseX - width/2) * 2.5, 220 + (mouseY - width/2) * 2.5, 220 + (mouseX - width/2) * 2, 200 + (mouseY - width/2) * 2, 180 + (mouseX - width/2) * 2, 200 + (mouseY - width/2) * 2);
  line(180 + (mouseX - width/2) * 2.5, 180 + (mouseY - width/2) * 2.5, 180 + (mouseX - width/2) * 2.5, 220 + (mouseY - width/2) * 2.5);
  line(220 + (mouseX - width/2) * 2.5, 180 + (mouseY - width/2) * 2.5, 220 + (mouseX - width/2) * 2.5, 220 + (mouseY - width/2) * 2.5);
  triangle(180 + (mouseX - width/2) * 2, 200 + (mouseY - width/2) * 2, 180 + (mouseX - width/2) * 2.5, 200 + (mouseY - width/2) * 2.5, 120 + (mouseX - width/2) * 2.5, 200 + (mouseY - width/2) * 2.5);
  triangle(220 + (mouseX - width/2) * 2, 200 + (mouseY - width/2) * 2, 220 + (mouseX - width/2) * 2.5, 200 + (mouseY - width/2) * 2.5, 280 + (mouseX - width/2) * 2.5, 200 + (mouseY - width/2) * 2.5);
}

//Functions

// Calling functions to enable mouse pressing
void mousePressed() 
{
  if (mousePressed) 
  {
    laserX = mouseX;
    laserY = mouseY;
    laserWidth = 20;
    laserHeight = 20;
    laser = true;
  }
}

//Creates the stars and their movement
void starAttack()
{
  stroke(255, 100, 0);
  fill(255, 255, 100);
  rect(randomStarX, randomStarY, starWidth, starHeight);
  starWidth = starWidth+0.2;
  starHeight = starHeight+0.2;
  strokeWeight(2);
  if (randomStarY >= 350)
  {
    newStar();
  }
}

//Sets a respawn function for the stars
void newStar ()
{
  star = false;
  randomStarX = 100;
  randomStarY = 100;
  starWidth = 0;
  starHeight = 0;
  counter = 0;
}

//Determines initial spawn for stars while also adding a delay before stars begin to spawn
void starStasis()
{
  if (randomStarX >= 0)
  {
    RNG = random(50, 351);
    counter = millis();
  } 
  if (randomStarY >= 0)
  {
    RNG = random(50, 351);
    counter = millis();
  }
  if (counter >= 2000)
  {
    randomStarX = RNG;
    star = true;
  }
}

//Creates a laser for the player to shoot at the incoming stars
void laserShoot ()
{
  if (laserWidth<=0)
  {
    laser=false;
  }
  if ((int)laserY <= (int)randomStarY || (int)laserX <= (int)randomStarX)
  {
    if (laserX <= (randomStarX+(starWidth/2)) && laserX >= (randomStarX-(starWidth/2)) && laserY <=(randomStarY+(starHeight/2)) && laserY >= (randomStarY-(starHeight/2)))
    {
      newStar();
      laser = false;
    }
  }
  strokeWeight(6);
  stroke(255);
  fill(0, 255, 0);
  rect(laserX, laserY-2, laserWidth-0.5, laserHeight-0.5);
  laserY=laserY-2;
  laserWidth=laserWidth-0.4;
  laserHeight=laserHeight-0.4;
  strokeWeight(2);
}