Bouncing Code – Start


int rectWidthHeight;

int rectX;
int rectY;

int direction = 10;

void setup() {
 
  println("setup");
  println("width " + width);
  println("height " + height);
  //frameRate(1);
  
  //rectMode(CENTER);
  
  rectWidthHeight = 50;
  
  rectX = mouseX;
  rectY = mouseY;
  
}

void draw() {
  background(120,120,120);

  rectWidthHeight = mouseX;
  
  rect((width/2)-(rectWidthHeight/2),(height/2)-rectWidthHeight/2,rectWidthHeight,rectWidthHeight);

  //println("draw");
  
  if (mousePressed) {
    rectX = mouseX;
    rectY = mouseY; 
  }
  
  if (rectX < 0 || rectY < 0) {
   direction = direction * -1; 
  } 
    
  if (rectX > width) {
       direction = direction * -1; 

  }

  if (rectY > height) {
    
       direction = direction * -1; 
  }
  
  rectX = rectX + direction;
  rectY = rectY + direction;
  
  rect(rectX,rectY,50,50);
  
}