Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
By: klye romano 
*/
// Nail one varibles for the x and y cordinates of the nail 
int nailX = 0;
int nailY = 0;
//nail height
int nailStem = 70;
//nail head height
int nailHead = nailY;
//varible for how many times a nail has been hit 
int nailHit =0;
//a varible to make sure this is the first run of the nail function
int firstRun =0;
//revison varible used in the cloud loop 
int revison = 0; 
//x and y of the clouds, there first set of x and ys are preset
int cloudX = 50;
int cloudY = 30;
//stops the secound if stament in the nailHit if statement from running multiples per nail
boolean stop = false;


void setup() {
  size(600, 500);
}

void draw() {
 drawBackground();
  
  drawNails();
  
  //if the mouse button isnt pressed draw the normal hammer
  if (mousePressed != true) {
    drawHammer();
  }
  //other wise draw the tapped version
  else {
    drawHammerTapped();
  }
}




//this draws the normal version of the hammer
void drawHammer ()
{
   fill(193, 116, 0);
  //handle
  rect (mouseX+30, mouseY-45, 108, 20);
  fill(50); 
  noStroke();
  //face of the hammer
  rect (mouseX-30, mouseY-30, 70, 18);
  // the base bbehind the face of the hammer
  rect(mouseX-25, mouseY-100, 60,80);
 
}



//this function draws the tapped version of the hammer
void drawHammerTapped()
{
   fill(193, 116, 0);
  //handle
  rect (mouseX+30, mouseY-10, 108, 20);
  fill(50); 
  noStroke();
  //face of the hammer
  rect (mouseX-30, mouseY+30, 70, 18);
  // the base bbehind the face of the hammer
  rect(mouseX-25, mouseY-50, 60,80);
}

void drawNails() 
{

  //repostions the nail if it gets to 3 hits from the hammer 
  if (nailHit == 3) 
  {
    nailX = int(random(50, 550)); 
    nailY = int(random(340, 430));
    nailHit = 0;
    nailHead = nailY; 
    nailStem = 70; 
    }

  //this should only run the first time the programs started
  else if (firstRun == 0)
  {
    nailX = int(random(50, 550)); 
    nailY = int(random(340, 430));
    firstRun = 1;
    nailHead = nailY;
  // shrinks the stem of the nail after the program confirms you hit 
  //the nail and that its the first time it was hit  
  } 
  else if ( nailHit == 1)
  {
  nailStem = 60; 
  //moves the nail body down
    if (stop == false) 
      {
      nailY = nailY+10;
      nailHead = nailHead+10; 
      //changes the boolean so that this if statement doesnt loop
      stop = true;
      }
  }
  //same idea but this is for the secound time the stem is shrunk even more
  else if ( nailHit == 2){
  nailStem = 40;
  //moves the nail body down
  if (stop == true)
      {
      nailY = nailY+25;
      nailHead = nailHead+25; 
      //resets the boolean so it can be used again with the new nail
      stop = false;
      }  
  }


  //this chunk makes the nail appear on screen
  //stem of nail
  fill(30);
  noStroke();
  rect(nailX, nailY, 20, nailStem);
  //head of nail
  fill(80);
  noStroke();
  rect(nailX-15, nailHead, 50, 11, 0);
}

//checks to see if the mouse was cliked inside the parameters of the nail
void mouseClicked() {
  if (mouseX > nailX && mouseY > nailY && mouseX < nailX+20 && mouseY < nailY+70)
  {
    nailHit += 1;
    
  }
}




void drawBackground (){
  
  
 //sky
  background (2,219,237);
  //baord
  fill(209, 170, 15);
  noStroke();
  rect (0, 323, 599, 189);
  //horizontal lines in board 
  fill(188, 146, 2);
  noStroke();
  rect (0, 324, 599, 4);
  rect (0, 340, 599, 4);
  rect (0, 360, 599, 4);
  rect (0, 383, 599, 4);
  rect (0, 415, 599, 4);
  rect (0, 454, 599, 4);
  rect (0, 490, 600, 4);
  //vertical lines in the board
  rect (101, 324, 5, 16);
  rect (283, 342, 5, 20);
  rect (64, 362, 5, 22);
  rect (532, 419, 5, 35);
  rect (202, 384, 5, 34);
  
  //creates clouds across the screen by using
  // a for loop to incrmentally draw them across the screen 
  
    for(int i2 = 0; i2 <= 4; i2++)
    {
    fill(255);
    noStroke(); 
    ellipse(cloudX,cloudY,250,40);
    //if the loop has drawn the first cloud change the x and y for the next cloud 
    if (i2 == 1)
    {
      cloudX += 120;
      cloudY += 15;
    }
    //same idea for the next two as well 
    else if (i2 == 2)
    {
      cloudX += 300;
      cloudY += 80;
    }
    else if (i2 == 3)
    {
      cloudX += 80;
      cloudY += -25;
    }
    //finally reset the cloud postion to the orginal area it started so that when 
    //draw loop loops back around the clouds will start at were they orginally were
    else if (i2 == 4)
    {
      cloudX = 50;
      cloudY = 30; 
    }
    
    }
   //instructions for the player 
  text ("hit the nails into the wood it takes three hits", 20,100);
  }