Your browser does not support the canvas tag.

previous        Show / Hide Source    Download        next
///////////////////////////////////////////
/////           BOUNCE TOY        /////////
//////////////////////////////////////////

//This toy bounces between two bumpers depending on how fast you push them, and the bumpers glow when they're struck. Delightful!
//By: Daniel Jones

//Note to self: translate(mousex, mousey) will draw everything under it relative to mousex and mousey

//Declare variables
float mouseSpeed = 0.0, rectSpeedX = 0.0, rectSpeedY = 0.0;
int rectPosX = width/2, rectPosY = height/2, rectPosX2 = width/2, rectPosY2 = height/2, blueValLeft = 0, blueValRight = 0, blueValLeft2 = 0, blueValRight2 = 0;

//Set frame rate, window size, etc.
void setup() {
  frameRate(60);
  size(1000, 1000);
  rectPosY = height/2;
  rectMode(CENTER);
}

void draw() {
  drawBackground();
  horizSetup();
  verticalSetup();
  //diagLeftSetup();
  //diagRightSetup();

}

void drawBackground(){
  //Draw the lightest shade of green on the BG
  background(0, 255, 175);
  //Draw increasingly bright rectangles as gradients
  noStroke();
  fill(0, 100, 0);
  rect(width/2, 900, 1000, 200);
  fill(0, 150, 40);
  rect(width/2, 700, 1000, 200);
  fill(0, 200, 80);
  rect(width/2, 500, 1000, 200);
  fill(0, 230, 115);
  rect(width/2, 300, 1000, 200);
}

void horizSetup(){
  //Set the line color
  stroke(0, 0, 0);
  
  //Calculate the mouse speed
  mouseSpeed = mouseX - pmouseX;
  
  //Determine if the mouse is moving within the confines of the square
  if (mouseX < rectPosX + 50 && mouseX > rectPosX - 50 && mouseY < rectPosY + 50 && mouseY > rectPosY - 50)
  {
    //Set the rectangle's horizontal speed to a fraction of the mouse speed
    rectSpeedX = mouseSpeed / 1.5;
  }
  
  //Allow the speed variable to affect the square's position
  rectPosX += rectSpeedX;
  
  //Check if the square has collided with the left bumper
  if(rectPosX < 150)
  {
    rectPosX = 150;
    rectSpeedX = rectSpeedX * -1;
    blueValLeft = 255;
  }
  
  //Check if the square has collided with the right bumper
  if(rectPosX > 850)
  {
    rectPosX = 850;
    rectSpeedX = rectSpeedX * -1;
    blueValRight = 255;
  }
  
  // Make the square decelerate if it is in motion
  if (rectSpeedX > 0)
  {
    rectSpeedX -= .1;
  }
  
  if (rectSpeedX < 0);
  {
    rectSpeedX += .1;
  }
  
  //Draw the bumpers
  fill(255, blueValLeft, 0);
  rect(50, rectPosY, 100, 100);
  fill(255, blueValRight, 0);
  rect(950, rectPosY, 100, 100);
  
  //Draw the square
  fill(255, 150, 150);
  rect(rectPosX, rectPosY, 100, 100);
  
  //Slowly decrease the brightness of a bumper after they've been hit.
  if (blueValLeft > 0)
  {
    blueValLeft -= 6;
  }
  
  if (blueValRight > 0)
  {
    blueValRight -=6;
  }
  
}

void verticalSetup(){
  //Set the line color
  stroke(0, 0, 0);
  
  //Calculate the mouse speed
  mouseSpeed = mouseY - pmouseY;
  
  //Determine if the mouse is moving within the confines of the square
  if (mouseX < rectPosX2 + 50 && mouseX > rectPosX2 - 50 && mouseY < rectPosY2 + 50 && mouseY > rectPosY2 - 50)
  {
    //Set the rectangle's horizontal speed to a fraction of the mouse speed
    rectSpeedY = mouseSpeed / 1.5;
  }
  
  //Allow the speed variable to affect the square's position
  rectPosY2 += rectSpeedY;
  
  //Check if the square has collided with the left bumper
  if(rectPosY2 < 150)
  {
    rectPosY2 = 150;
    rectSpeedY = rectSpeedY * -1;
    blueValLeft2 = 255;
  }
  
  //Check if the square has collided with the right bumper
  if(rectPosY2 > 850)
  {
    rectPosY2 = 850;
    rectSpeedY = rectSpeedY * -1;
    blueValRight2 = 255;
  }
  
  // Make the square decelerate if it is in motion
  if (rectSpeedY > 0)
  {
    rectSpeedY -= .1;
  }
  
  if (rectSpeedY < 0);
  {
    rectSpeedY += .1;
  }
  
  // Set the horizontal position of the mechanism
  rectPosX2 = width/2;
  
  //Draw the bumpers
  fill(0, blueValLeft2, 255);
  rect(rectPosX2, 50, 100, 100);
  fill(0, blueValRight2, 255);
  rect(rectPosX2, 950, 100, 100);
  
  //Draw the square
  fill(150, 150, 255);
  rect(rectPosX2, rectPosY2, 100, 100);
  
    //Slowly decrease the brightness of a bumper after they've been hit.
  if (blueValLeft2 > 0)
  {
    blueValLeft2 -= 6;
  }
  
  if (blueValRight2 > 0)
  {
    blueValRight2 -=6;
  }
  
}