Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
/*
Interactive Toy Assignment
 Target Shot
 By: Jonah GM
 
 How to play:
 
 Objective: Shoot the brown wooden planks for points
 
 CONTROLS:
 -move cursor/rectile to left or right of screen to move left and right
 -Click to shoot
 -To reload, simply Click when out of ammo
 
 Other Info:
 -You have a one minute time limit to recieve a score
 -When the time is up, click the red square to play again
 -Your score is printed as text
 -You can't shoot without a bullet in the chamber :b
 
 
 
 
 
 
 
 Other-Other :b 
 -There is one known bug, that being sometimes the target will not die and the player
 will not recieve a point. To fix that, simply move left or right slightly, then shoot target
 -Dev Cheat: Hold mouse click after shooting a target to recieve highest possible score
 
 */
//User defined variables
//Int(Whole Numbers) Boolean(True/False) Float(Decimal Numbers)
int lookLeftRight;
int bulletsShot = 0;
int score = 0;
int timer = 60000;
boolean bullet1=true;
boolean bullet2=true;
boolean bullet3=true;
boolean bullet4=true;
boolean bullet5=true;
boolean ammo = true;
boolean targetAlive = true;
//Random(Random number between given# and given#)
//w=width h=height
float targetxw = random(5, 100);
float targetyh = random(1, 100);
float targetx = random(60, 740);
float targety = random(60, 290);
float hitBoxx = 0;
float hitBoxy = 0;
float hitBoxxw = 0;
float hitBoxyh = 0;
//Setup (Done Once)
void setup () {
  //Size of applet
  size(400, 400); 
  //Don't show cursor
  noCursor();
  //No outline on screen
  noStroke();
}
//Draw (Done Constantly)
void draw() {
  //Backround Colour of white
  background(255);
  //Call user defined functions
  drawBackground();
  drawTarget();
  drawBooth();
  drawBullets();
  drawCrosshair();
  scrollFunction();
  bulletsFunction();
  targetShotFunction();
  hitBoxFunction();
  scoreFunction();
  timeFunction ();
}
//Define "drawCrosshair"
void drawCrosshair () {

  fill(0);
  rect(mouseX-3, mouseY-3, 5, 5);
  rect(mouseX-3, mouseY-35, 5, 25);
  rect(mouseX-3, mouseY+10, 5, 25);
  rect(mouseX-38, mouseY-3, 25, 5);
  rect(mouseX+13, mouseY-3, 25, 5);
}
//Define "drawBooth"
void drawBooth() {

  //Top Bar
  stroke(1);
  fill(200);
  rect(0, 0, 800, 50);
  //Left Side Bar
  fill(190);
  rect(0+lookLeftRight, 0, 50, 400);
  //Right Side Bar
  rect(750+lookLeftRight, 0, 50, 400);
  //Bottom Bar
  fill(170);
  rect(0, 330, 400, 400);
  //Individual Steel Planks
  fill(0);
  //Front Bar Look Over
  fill(190);
  rect(50+lookLeftRight, 300, 700, 30);
  //Starting at 10, at a limit of 800 pixels on grid, continously draw code
  //below in 20 pixel intervals
  for (int i=10; i<800; i += 20) {
    line(i+lookLeftRight, 330, i+lookLeftRight, 400);
  }
}
//Define "drawBullets"
void drawBullets() {
  //First Bullet
  if (bullet1 == true) {
    fill(191, 22, 22);
    rect(30, 10, 20, 20);
    fill(213, 187, 43);
    rect(30, 30, 20, 10);
  } else if (bullet1 == false) {
    noFill();
    stroke(1);
    rect(30, 10, 20, 30);
  }

  //Second Bullet
  if (bullet2 == true) {
    fill(191, 22, 22);
    rect(60, 10, 20, 20);
    fill(213, 187, 43);
    rect(60, 30, 20, 10);
  } else if (bullet2 == false) {
    noFill();
    stroke(1);
    rect(60, 10, 20, 30);
  }

  //Third Bullet
  if (bullet3 == true) {
    fill(191, 22, 22);
    rect(90, 10, 20, 20);
    fill(213, 187, 43);
    rect(90, 30, 20, 10);
  } else if (bullet3 == false) {
    noFill();
    stroke(1);
    rect(90, 10, 20, 30);
  }

  //Fourth Bullet
  if (bullet4 == true) {
    fill(191, 22, 22);
    rect(120, 10, 20, 20);
    fill(213, 187, 43);
    rect(120, 30, 20, 10);
  } else if (bullet4 == false) {
    noFill();
    stroke(1);
    rect(120, 10, 20, 30);
  }

  //Fifth Bullet
  if (bullet5 == true) {
    fill(191, 22, 22);
    rect(150, 10, 20, 20);
    fill(213, 187, 43);
    rect(150, 30, 20, 10);
  } else if (bullet5 == false) {
    noFill();
    stroke(1);
    rect(150, 10, 20, 30);
  }
  noStroke();
}
//Define "scrollFunction"
void scrollFunction() {

  //If the mouse is less than -NUMBER-, execute code below
  if (mouseX < width/9) {
    lookLeftRight = lookLeftRight +10;
    //If the mouse is more than -NUMBER-, execute code below
  } else if (mouseX > width/1.1) {
    lookLeftRight = lookLeftRight -10;
  }
  //Limit variable lookLeftRight to -400 and 0
  lookLeftRight = constrain(lookLeftRight, -400, 0);
}
//Define "bulletFunction"
void bulletsFunction () {
  //Shooting Bullets
  //frameCount: If -NUMBER- frames pass, execute code below (By default, there is
  //60 frames per second
  if (mousePressed && bullet5 == true && frameCount > bulletsShot + 10) {
    bullet5 = false;
    bulletsShot = frameCount;
  } else if (mousePressed && bullet5 == false && bullet4 == true && frameCount > bulletsShot + 10) {
    bullet4 = false;
    bulletsShot = frameCount;
  } else if (mousePressed && bullet4 == false && bullet3 == true && frameCount > bulletsShot + 10) {
    bullet3 = false;
    bulletsShot = frameCount;
  }

  if (mousePressed && bullet3 == false && bullet2 == true && frameCount > bulletsShot + 10) {
    bullet2 = false;
    bulletsShot = frameCount;
  }

  if (mousePressed && bullet2 == false && bullet1 == true && frameCount > bulletsShot + 10) {
    bullet1 = false;
    bulletsShot = frameCount;
  }
  //Reload
  if (mousePressed && bullet1 == false  && frameCount > bulletsShot + 60) {

    bullet1 = true;
    bullet2 = true;
    bullet3 = true;
    bullet4 = true;
    bullet5 = true;
    bulletsShot = frameCount;
  }
}
//Define "drawTarget"
void drawTarget() {
  if (targetAlive == true) {
    fill(136, 71, 38);
    rect(targetx+lookLeftRight, targety, targetxw, targetyh);
  }
  //Give the target new values to spawn elsewhere
  if (targetAlive == false) {
    targetxw = random(5, 100);
    targetyh = random(1, 100);
    targetx = random(60, 740);
    targety = random(60, 290);
  }
}
//Define "hitBoxFunction"
void hitBoxFunction() {

  if (targetAlive == true) {
    hitBoxx = targetx+lookLeftRight;
    hitBoxy = targety;
    hitBoxxw = targetx + targetxw+lookLeftRight;
    hitBoxyh = targety + targetyh;
  }
}
//Define "targetShotFunction"
void targetShotFunction() {

  //No ammo
  if (bullet1 == false && frameCount > bulletsShot + 60) {
    ammo = false;
    //Has ammo
  } else if (bullet1==true && frameCount > bulletsShot + 60) {
    ammo = true;
  }
  //If specified requirements are met, the target has been shot
  if (mouseX>=hitBoxx && mouseX<=hitBoxxw && mouseY>=hitBoxy && mouseY<=hitBoxyh && mousePressed && targetAlive==true && ammo ==true) {
    fill(255, 253, 57);
    rect(0, 0, 400, 400);
    targetAlive = false;
  }
  //Respawn the target
  if (targetAlive==false && frameCount > bulletsShot + 60) {

    targetAlive=true;
  }
}
//Define "scoreFunction"
void scoreFunction() {
  //Add to player's score when the target has been killed
  if (targetAlive == false && frameCount > bulletsShot + 59) {
    score = score + 1;
    println("Score:" +score);
  }
  //I'd use the code below if it were in Chapter1-7, but sadly it's not. :b
  //text("Score:" +score, 300,20);
}
//Define "timeFunction"
void timeFunction () {
  int time=0;
  time = time+millis();
  if (time > timer) {
    fill(255);
    rect(0, 0, 400, 400);
    cursor();
    println("GG. Your score is "+score+". Click the red box to play again!");
    fill(220, 0, 0);
    rect(150, 150, 100, 100);
    //Reset score, ammo, and have new timer limit so player can play again
    if (mouseX>=150 && mouseX<=250 && mouseY>=150 && mouseY<=250 && mousePressed) {
      timer = 60000 + millis();
      score = 0;
      bullet1 = true;
      bullet2 = true;
      bullet3 = true;
      bullet4 = true;
      bullet5 = true;
      noCursor();
    }
  }
  //Developer tool
  //println(millis());
}
//Define "drawBackground"
void drawBackground() {
  fill(24, 197, 255);
  rect(0, 0, 400, 70);
  fill(55, 193, 239);
  rect(0, 70, 400, 20);
  fill(52, 187, 232);
  rect(0, 90, 400, 20);
  fill(47, 189, 44);
  rect(0, 110, 400, 300);
}
/*
REFERANCE
 
 Understanding how to use millis()
 https://stackoverflow.com/questions/12417937/create-a-simple-countdown-in-processing
 */