Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*//////////////////////////////////////////////////////////////////////////////////////////////
 ** Frail Man - Interactive toy by Firaas Khan
 **
 ** Aliens are invading earth!
 ** The only who can stop them is Frail Man; the frailest man in the world!
 ** Avoid the aliens so they run into the force field.
 ** Becarful, as one hit ends Frail's Mans life.
 ** Survive for as long as you can!
/*//////////////////////////////////////////////////////////////////////////////////////////////
//Global variables
int starsX = 0;
int starsY = 0;
int alienX=(int) random(0,400);
int alienY=0;
int alienW = 40;
int alienH = 100;
int screenHit = 0;
float move = 4;
int frailX = 180;
int frailY = 300;
int frailW = 10;
int frailH = 100;
boolean left = false;
boolean right = false;
//Setting up screen size
void setup(){
  size(400,400);
}
//Draw functions, plus collision and key presses
void draw(){
  drawBackGround();
  drawFrailMan();
  drawAlien();
  checkCollision();
  keyPressed();
  keyReleased();
  
}

void drawBackGround(){
  background(0); //Make background black
  //Draw white stars with random X and Y values
  for (int i = 0; i<5; i=i+1);{
    starsX= (int) random(0,400);
    starsY= (int) random(0,400);
    stroke(255);
    point(starsX,starsY);
  }
}

void drawFrailMan(){
  //Drawing Frailman
  stroke(255);
  line(frailX+10,360,frailX+10,400);
  line(frailX-10,360,frailX-10,400);
  line(frailX,360,frailX,320);
  line(frailX+10,360,frailX-10,360);
  line(frailX+10,340,frailX,320);
  line(frailX,320,frailX-10,340);
  ellipseMode(CENTER);
  fill(255);
  ellipse(frailX,310,20,20);
  //Frailman moves 10 pixels left or right if pressing a or d
  if (left == true){
    frailX-=10;
  }
   else if (right == true){
     frailX+=10;
   }
   //Confine Frailman within window
   if (frailX<=0){
     frailX=1;
   }
   else if (frailX>=400){
     frailX=399;
   }
}
void drawAlien(){
  //Drawing Alien
    triangle(alienX,alienY,alienX,alienY+10,alienX+20,alienY);
    triangle(alienX+20,alienY,alienX+40,alienY,alienX+40,alienY+10);
    stroke(60,122,9);
    fill(106,179,46);
    rectMode(CORNERS);
    rect(alienX,alienY,alienX+40,alienY-40);
    stroke(0);
    fill(0);
    ellipseMode(CENTER);
    ellipse(alienX+10,alienY-30,5,5);
    ellipse(alienX+30,alienY-30,5,5);
    //Alien moves 4 pixels initially, based on variable "move"
    if (alienY<=400){
      alienY+=move;
    }
    //Reset alien's position when if it hits the bottom screen
    else if (alienY>=401){
      alienX = (int) random(0,400);
      alienY = -20;
      screenHit+=1;//Counts how many times alien hit bottom screen
    }
    //Change value of "move" if "screenHit" is at specific values
      if (screenHit == 3){
        move= 5.5;
      }
      
      if (screenHit == 10){
        move = 7;
      }
      
      if (screenHit == 15){
        move = 10;
      }
}
//Controls for moving Frailman
//Determines if movement is true
void keyPressed(){
  if (key=='a'){
    left = true;
    right = false;
  }
  else if (key =='d'){
    left = false;
    right = true;
  }

}
//Determines if key is releases, so Frailman isn't moving constantly
void keyReleased(){
  if (key=='a'){
    left = false;
    right = false;
  }
    else if (key =='d'){
      left = false;
      right = false;
    }

}
//Collision check for alien and Frailman
void checkCollision(){
  if (frailX+10>alienX-20&&
      frailX-10< alienX+20 &&
      frailY>alienY-20 &&
      frailY-10<alienY+20){
        //Center Frailman if collision occurs
        frailX=180;
        //Reset position, speed, and screenHit if collsion occurs
        alienX = (int) random(0,400);
        alienY = -20;
        move=4;
        screenHit=0;
        
      }
}