Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Alien Invasion\\
//Joseph Tran\\
//Student Number: 991520039\\

//Objective: Shoot the attacking aliens!!!\\
//Instructions: move the crosshair with the mouse. Click the mouse to fire the laser.\\

//global variables 
float saucerX = random(30,400);
float saucerY = random(40,260);
int moveX = 1;
int moveY = 30;
int saucerSizeX = 60;
int saucerSizeY = 20;
int score = 1;

void setup () {
  size (400,400);
  noCursor();
  noStroke();
}

void draw () {
  background (255);
  
  //setting up functions
  drawBackground();
  drawTreeBark();
  drawTrees();
  drawSaucer();
  drawLaserCannon();
  drawLaser();
  drawCrosshair();
  laserFunction();
  updateSaucer();
}

void drawBackground () {
  //draw the night sky
  noStroke() ;
  fill (102,74,189);
  rect (0,320,width,80);
  fill (81,59,150);
  rect (0,240,width,80);
  fill (65,47,120);
  rect (0,160,width,80);
  fill (56,41,105);
  rect (0,80,width,80);
  fill (47,34,87);
  rect (0,0,width,80);
}

void drawTreeBark () {
  //local variable for the tree bark
  float barkDrawingPositionX = 15; 
  noStroke();
  fill (116,56,49);
  
  //loop for the tree bark
  while (barkDrawingPositionX<width) {
    rect (barkDrawingPositionX,380,10,20);
    
    barkDrawingPositionX = barkDrawingPositionX + 40;
  } 
}

void drawTrees () {
  //local variable for the tree
  float treeDrawingPositionX = 0;
  noStroke() ;
  fill (46,192,8);
  
  //loop for the tree
  while (treeDrawingPositionX<width) {
    triangle (treeDrawingPositionX,320,treeDrawingPositionX+20,300,treeDrawingPositionX+40,320);
    triangle (treeDrawingPositionX,340,treeDrawingPositionX+20,320,treeDrawingPositionX+40,340);
    triangle (treeDrawingPositionX,360,treeDrawingPositionX+20,340,treeDrawingPositionX+40,360);
    triangle (treeDrawingPositionX,380,treeDrawingPositionX+20,360,treeDrawingPositionX+40,380);
    treeDrawingPositionX+=40;
  }
}

void drawSaucer () {
  noStroke();
  //upper saucer
  fill (168,191,204);
  ellipse (saucerX+moveX,saucerY-40,saucerSizeX-30,saucerSizeY+10);
  //lower saucer
  fill (133,151,161);
  ellipse (saucerX+moveX,saucerY-moveY,saucerSizeX,saucerSizeY);
  //saucer window
  fill (24,34,166);
  rect (saucerX-10,saucerY-50,saucerSizeX-38,saucerSizeY-15);
}

void drawLaserCannon () {
  noStroke();
  fill (127);
  //Bottom Base
  rect (mouseX+-30,360,60,40);
  triangle (mouseX+-60,height,mouseX+-30,360,mouseX+-30,height);
  triangle (mouseX+30,360,mouseX+30,height,mouseX+60,height);
  //Top Base
  rect (mouseX+-10,330,20,30);
  rect (mouseX+-20,340,10,30);
  rect (mouseX+10,340,10,30);
  triangle (mouseX+-20,340,mouseX+-10,330,mouseX+-10,340);
  triangle (mouseX+10,330,mouseX+10,340,mouseX+20,340);
  //Tip
  rect (mouseX+-5,280,10,50);
  triangle (mouseX+-5,280,mouseX+-10,290,mouseX+-5,290);
  triangle (mouseX+5,280,mouseX+5,290,mouseX+10,290);
  triangle (mouseX+-10,290,mouseX+-5,290,mouseX+-5,300);
  triangle (mouseX+5,290,mouseX+10,290,mouseX+5,300);
  //outlines of the cannon
  stroke(0);
  strokeWeight(1);
  line (mouseX+-60,height,mouseX+-30,360);
  line (mouseX+-30,360,mouseX+30,360);
  line (mouseX+30,360,mouseX+60,height);
  line (mouseX+-20,360,mouseX+-20,340);
  line (mouseX+20,360,mouseX+20,340);
  line (mouseX+-20,340,mouseX+-10,330);
  line (mouseX+10,330,mouseX+20,340);
  line (mouseX+-10,330,mouseX+10,330);
  line (mouseX+-5,300,mouseX+-5,330);
  line (mouseX+5,300,mouseX+5,330);
  line (mouseX+-5,300,mouseX+5,300);
  line (mouseX+-5,280,mouseX+5,280);
  line (mouseX+-10,290,mouseX+-5,300);
  line (mouseX+-10,290,mouseX+-5,280);
  line (mouseX+5,300,mouseX+10,290);
  line (mouseX+5,280,mouseX+10,290);
  line (mouseX+-10,310,mouseX+10,310);
  line (mouseX+-10,320,mouseX+10,320);
}
  
void drawLaser() {
  //laser
  if (mousePressed) {
    if (mouseY>=280) {
      mouseY=280;
    }
    noStroke();
    fill (110,248,255);
    ellipse (mouseX,280,10,10);
    stroke(110,248,255);
    strokeWeight(3);
    line (mouseX,280,mouseX,mouseY);
  }
}

void drawCrosshair () {
  //mouseY will not go over 280
  if (mouseY>=280) {
    mouseY=280;
  }
  //crosshair shape
  noStroke();
  fill (25,212,35);
  ellipse (mouseX,mouseY,10,10);
  rect (mouseX+10,mouseY+-5,20,10);
  rect (mouseX+-30,mouseY+-5,20,10);
  rect (mouseX+-5,mouseY+10,10,20);
  rect (mouseX+-5,mouseY+-30,10,20);
}

void laserFunction() {
   //if laser cannon shoots saucer, saucer disappears 
   if (mouseX>=saucerX-30 && mouseX<=saucerX+30 && mouseY>=saucerY-40 && mouseY<=saucerY-20 && mousePressed) {
    println ("Pew! Aliens Destroyed:"+score++);
    //saucer relocates outside grid
    saucerX= -250;
    saucerY= 800;
    //explosion on impact
    fill (255,0,0);
    ellipse (mouseX,mouseY,50,50);
    fill (255,199,16);
    ellipse (mouseX,mouseY,40,40);
  }
}

void updateSaucer() {
  //respawn another saucer when laser cannon destroys one
  if (saucerX<width && saucerY>height) {
    //new saucer is randomly drawn when saucer relocates outside grid
    saucerX = random(30,400);
    saucerY = random(40,260);
  }
}

//references: 
//http://www-acad.sheridanc.on.ca/PROG14998/2017/interactive-toy/jiveen_liew_interactive_toy/index.html
//http://www-acad.sheridanc.on.ca/PROG14998/2017/interactive-toy/jonah_gonzalez_martinez_interactive_toy/index.html
//http://www-acad.sheridanc.on.ca/PROG14998/2017/interactive-toy/alexander_vaucrosson_interactive_toy/index.html