Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/* Interactive Toy Assinment 
 Rachel Theil
 PROG Lindy 
 991396531
 */

//declare variables
boolean targetVisable = true;
int shapeSize = 50;
int timer = 0;
int visableTime = 60;
int numberOfTargets = int(random(2, 6));
int backgroundColo = 255;

float x = 0;
float y = 0;

//positions of all of targets
PVector[] positions = new PVector[numberOfTargets];

// the save data for if targets are hit in different spots
boolean[] targetHit = new boolean[numberOfTargets];
boolean[] targetSuperHit = new boolean[numberOfTargets];
boolean[] targetCritHit = new boolean[numberOfTargets];

//check if a target is hit or not
boolean[] hit = new boolean[numberOfTargets];

//setup+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void setup() {
  //create the window
  size (400, 400);
  background(255);
}

//draw the program ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void draw() {

  //clear the background every frame and redraw it
  //clear();
  background(backgroundColo);

  //make targets that appear and disapear after a certian time  
  if (targetVisable) {

    //when the time is at the beginning make a new set of targets and cooardinates for each
    if (timer == 0) {
      //create a random number of target each round 
      numberOfTargets = int(random(2, 6));

      //replace the number of targets in the the arrays to get the proper length for each array
      positions = new PVector[numberOfTargets];
      targetHit = new boolean[numberOfTargets];
      targetSuperHit = new boolean[numberOfTargets];
      targetCritHit = new boolean[numberOfTargets];
      hit = new boolean[numberOfTargets];

      //draw the number of targets spesified by numberOfTargets
      for (int i=0; i<positions.length; i++) {

        //set the x and y values for each target and then draw them on the first frame
        x = random(35, 366);
        y = random(35, 366);
        positions[i] = new PVector(x, y);

        //check the staus of each target and draw the approiate one 
        checkTargets(i);
      }
    } 
    //redraw each target for each frame
    else if (timer > 0) {
      for (int i=0; i<positions.length; i++) {
        //check the staus of each target and draw the approiate one 
        checkTargets(i);
      }
    }
    //make the background colour darken over time to make a visual timer
    backgroundColo = backgroundColo - 2;
  } 

  //if the timer dose not equal the total time then increase the timer
  if (timer != visableTime *3.5) {
    timer++;
  }
  //once the target is visable for a short time make it invisable 
  if (timer == visableTime * 2) {
    targetVisable = false;

    //make the backgound colour black when the time is up
    backgroundColo = 0;

    //reset all targets upon the cut to balck
    for (int k = 0; k < positions.length; k++) {
      targetHit[k] = false;
      targetSuperHit[k] = false;
      targetCritHit[k] = false;
      hit[k] = false;
    }
  } 
  //then make new targets after a short time and reset the timer
  else if (timer == visableTime * 3.5) {
    targetVisable = true;
    timer = 0;

    //reset the background colour
    backgroundColo = 255;
  }
}

//check the status of each target++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void checkTargets(int arrayNumber) {
  //if the target is not hit then make a gray target  
  if (targetHit[arrayNumber] == false) {
    drawTarget(positions[arrayNumber]);
  }
  //if the target is hit in the centre turn it green
  else if (targetCritHit[arrayNumber] == true) {
    drawCritTarget(positions[arrayNumber]);
    
    // make it so the target can only be hit once
    hit[arrayNumber] = true;
  }
  //if the target is hit in the middle ring turn it cyan
  else if (targetSuperHit[arrayNumber] == true) {
    drawSuperTarget(positions[arrayNumber]);
    
    // make it so the target can only be hit once
    hit[arrayNumber] = true;
  } 
  //if the target is hit in the outer ring then make a dark blue target
  else if (targetHit[arrayNumber] == true) {
    drawHitTarget(positions[arrayNumber]);
    
    // make it so the target can only be hit once
    hit[arrayNumber] = true;
  }

  //if all the targets are clicked then make them yellow
  if (checkAllValues(targetHit) == true) {
    drawAllHitTarget(positions[arrayNumber]);
  }
}


//Make a square gray target+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void drawTarget(PVector position) {  
  //if the target is not hit then make a gray target 
  fill(70);
  rect(position.x-(shapeSize/2), position.y-(shapeSize/2), shapeSize, shapeSize );
  fill(180);
  rect(position.x -(shapeSize/3), position.y -(shapeSize/3), shapeSize/1.5, shapeSize/1.5);
  fill(255);
  rect(position.x-(shapeSize/8), position.y-(shapeSize/8), shapeSize/4, shapeSize/4);
}

//make a hit blue target++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void drawHitTarget(PVector position) {
  //if the target is hit in the outer ring make it blue
  fill(124, 149, 245);
  rect(position.x-(shapeSize/2), position.y-(shapeSize/2), shapeSize, shapeSize );
  fill(43, 62, 137);
  rect(position.x -(shapeSize/3), position.y -(shapeSize/3), shapeSize/1.5, shapeSize/1.5);
  fill(31, 31, 64);
  rect(position.x-(shapeSize/8), position.y-(shapeSize/8), shapeSize/4, shapeSize/4);
}
//make a hit cyan target++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void drawSuperTarget(PVector position) {
  //if the target is hit in the middle make it cyan
  fill(124, 245, 242);
  rect(position.x-(shapeSize/2), position.y-(shapeSize/2), shapeSize, shapeSize );
  fill(60, 152, 150);
  rect(position.x -(shapeSize/3), position.y -(shapeSize/3), shapeSize/1.5, shapeSize/1.5);
  fill(23, 72, 80);
  rect(position.x-(shapeSize/8), position.y-(shapeSize/8), shapeSize/4, shapeSize/4);
}

//make a hit green target++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void drawCritTarget(PVector position) {
  //if the target is hit in the centre ring make it green
  fill(198, 255, 220);
  rect(position.x-(shapeSize/2), position.y-(shapeSize/2), shapeSize, shapeSize );
  fill(98, 191, 134);
  rect(position.x -(shapeSize/3), position.y -(shapeSize/3), shapeSize/1.5, shapeSize/1.5);
  fill(32, 100, 59);
  rect(position.x-(shapeSize/8), position.y-(shapeSize/8), shapeSize/4, shapeSize/4);
}
//make a yellow target when all targets are hit++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void drawAllHitTarget(PVector position) {
  //if all targets are hit make them yellow
  fill(251, 252, 156);
  rect(position.x-(shapeSize/2), position.y-(shapeSize/2), shapeSize, shapeSize );
  fill(183, 150, 78);
  rect(position.x -(shapeSize/3), position.y -(shapeSize/3), shapeSize/1.5, shapeSize/1.5);
  fill(106, 88, 50);
  rect(position.x-(shapeSize/8), position.y-(shapeSize/8), shapeSize/4, shapeSize/4);
}

//checked if the targets are hit++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
void mousePressed() {
  //if a target is clicked then make that target's state be hit depending on where its clicked 
  for (int j = 0; j<positions.length; j++) {
    if(hit[j] == false) {
      //check for outer ring boundaries 
      if (targetVisable && mouseX >= positions[j].x-(shapeSize/2)&& mouseX <= positions[j].x+(shapeSize/2) && mouseY <= positions[j].y+(shapeSize/2) && mouseY >= positions[j].y-(shapeSize/2)) {
        targetHit[j] = true;
      }
      //check middle ring boundaries 
      if (targetVisable && mouseX >= positions[j].x-(shapeSize/3)&& mouseX <= positions[j].x+(shapeSize/3) && mouseY <= positions[j].y+(shapeSize/3) && mouseY >= positions[j].y-(shapeSize/3)) {
        targetSuperHit[j] = true;
      }
      //check centre ring boundaries 
      if (targetVisable && mouseX >= positions[j].x-(shapeSize/8)&& mouseX <= positions[j].x+(shapeSize/8) && mouseY <= positions[j].y+(shapeSize/8) && mouseY >= positions[j].y-(shapeSize/8)) {
        targetCritHit[j] = true;
      }
    }
  }
}

// check if all the targets get hit ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
boolean checkAllValues(boolean [] array) {
  for (int i = 0; i < array.length; i ++) {
    // if a target is not hit then return false 
    if (array[i] == false) {
      return false;
    }
  }
  // if all targets are hit then return true
  return true;
}