Your browser does not support the canvas tag.

previous        Show / Hide Source        Download        next
//Interactive Toy by Zachery Pelletier

/* 
 You move the rectangle left and right to capture the balls. When the balls go over your rectangle
 you receive a few points. Thanks to the help of a few classmates and google, I made this simple toy.
 */


//setting some variables
float spawn = random(40, 360);
float ballX = spawn;
float ballY = 160;
float speed = 4;
int score = 0;
int radius = 10;


void setup() {
  size(400, 400);
  ellipseMode(CENTER);
  rectMode(CORNERS);
}

void draw() {

  display();
  move();
  ball();
  checkpoint();
}


// Moving of the ball
void move() {
  ballY = ballY + speed;
  if (ballY > 440) {
    ballY = 180;
    ballX = spawn = random(40, 360);
  }
}


// Drawing of the ball
void ball() {
  stroke(2);
  fill(random(255), .55, 255);
  ellipse(ballX, ballY, 20, 20);
}


//Displaying the canons and others
void display() {
  rectMode(CORNERS);
  background(255);
  noStroke();

  fill(46, 38, 32);
  rect(0, 0, 400, 120);

  noStroke();
  fill(255);
  triangle(80, 120, 120, 60, 160, 120);
  triangle(240, 120, 280, 60, 320, 120);

  fill(83, 83, 83);
  quad(260, 90, 300, 90, 300, 140, 260, 140);
  quad(100, 90, 140, 90, 140, 140, 100, 140);
  triangle(280, 130, 360, 160, 200, 160);
  triangle(120, 130, 200, 160, 40, 160);
  triangle(120, 60, 140, 90, 100, 90);
  triangle(280, 60, 300, 90, 260, 90);

  rectMode(CENTER);
  rect(mouseX, 360, 40, 10);
}

//displaying score
void displayScore() {
  println(score);
}


//Checking if collision. Adding to score if collision is true
void checkpoint() {
  if (mouseX >= ballX - radius && mouseX <= ballX + radius && mouseY >= ballY - radius && mouseY <= ballY + radius)
  {
    score = score + 1;
    displayScore();
  }
}