Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
target p1;
scope p2;
GunBarrel p3;
int score = 0;
boolean inSight = false;

color[] colorArray = {
color(255,0,0), //red
color(255), //white
color(50,200,200), //blue
color(206, 206, 206) //brown
};

void setup()
{
size (400,400);
p1 = new target(100,100,10);
p2 = new scope(500,200,10);
p3 = new GunBarrel();

}

void draw()
{
  background(colorArray[2]);
  //make the target move
  p1.x +=3;
  
  PVector d = new PVector(dist(p1.x,p1.y,p2.x,p2.y), 0);
  if(d.x < p1.r + p2.r)
  {
    inSight = true;
  
  }
    p2.x = mouseX;
    p2.y = mouseY;

  p1.display();
  p2.display();
  p3.display();
  //reset the target off screen
   if(p1.x >=400)
{
  p1.x = -25;
  p1.y = random(25,345);
}

}
//update score and change background colour
void mouseClicked()
{
if(inSight == true){
  background (colorArray[0]);
  score += 1;
  p1.y *= 10;
  inSight = false;
  //displays the score when it is increased
println("your score is:" + score);
}

}
class GunBarrel
{
  //float x1;
  //float y;
  
  GunBarrel () {
  
    //x1 = mouseX;
    
  }
  void display(){
  
  ellipseMode(CENTER);
  rectMode(CENTER);
  fill(colorArray[3]);
  noStroke();
  ellipse(mouseX, 345, 80, 30);
  rect(mouseX, 395, 80, 100);
  
  
  }
  
}
class scope
{
float x, y;
float r;


scope(float tempX, float tempY, float tempR){
x = tempX;
y = tempY;
r = tempR;
}
void display()
{
  noCursor();
    stroke(0);
    strokeWeight(3);
    fill(colorArray[1], 50);
    ellipse(mouseX,mouseY, 80,80);
    line(mouseX,mouseY-40,mouseX,mouseY-20);
    line(mouseX,mouseY+20,mouseX,mouseY+40);
    line(mouseX-40,mouseY,mouseX-20,mouseY);
    line(mouseX+20,mouseY,mouseX+40,mouseY);
    strokeWeight(1);
    line(mouseX-40,mouseY,mouseX+40,mouseY);
    line(mouseX,mouseY-40,mouseX,mouseY+40);
    ellipse(mouseX,mouseY, 15,15);
}

}
class target
{
float x, y;
float r;


target(float tempX, float tempY, float tempR){
x = tempX;
y = tempY;
r = tempR ;
}
void display()
{
fill(colorArray[0]);
ellipse(x,y,45,45);
fill(colorArray[1]);
ellipse(x,y,30,30);
fill(colorArray[0]);
ellipse(x,y,15,15);
}

}