> show canvas only <


/* built with Studio Sketchpad: 
 *   https://sketchpad.cc
 * 
 * observe the evolution of this sketch: 
 *   https://studio.sketchpad.cc/sp/pad/view/ro.n3zAlnxKVUP/rev.11
 * 
 * authors: 
 *   GoToLoop

 * license (unless otherwise specified): 
 *   creative commons attribution-share alike 3.0 license.
 *   https://creativecommons.org/licenses/by-sa/3.0/ 
 */ 



/*
 * Bouncing Balloons (v3.37)
 *
 * forum.processing.org/topic/using-arrays-to-create-and-change-circles
 * http://forum.processing.org/topic/object
 * http://forum.processing.org/topic/balloon-string
 *
 * studio.processingtogether.com/sp/pad/export/ro.9FqnMDLUZqQCV/latest
 *
 */

final static color BG = 0, FG = -1;
final static byte  FPS = 60;

final static byte NUM = 3;
final static myBalloon[] balloons = new myBalloon[NUM];

void setup() {
  size (500, 500);
  smooth();
  frameRate(FPS);

  balloons[0] = new myBalloon(
  120, 70, // initX, initY
  50, 40, // sizeX, sizeY
  7, 1, // moveX, moveY
  #0000FF); // colour

  balloons[1] = new myBalloon(
  320, 400, // initX, initY
  40, 50, // sizeX, sizeY
  2, -9, // moveX, moveY
  #00A000); // colour

  balloons[2] = new myBalloon(
  250, 250, // initX, initY
  30, 30, // sizeX, sizeY
  -7, 4, // moveX, moveY
  #D00000); // colour
}

void draw() {
  background(BG);
  for (myBalloon ball: balloons)   ball.all();
}

final class myBalloon {
  int x, y;
  final int szX, szY, hfX, hfY;
  int mvX, mvY;
  final color cor;

  myBalloon (
  int initX, int initY, 
  int sizeX, int sizeY, 
  int moveX, int moveY, 
  color colour ) {

    x   = initX;
    y   = initY;
    szX = sizeX;
    szY = sizeY;
    mvX = moveX;
    mvY = moveY;
    cor = colour;

    hfX = sizeX >> 1;
    hfY = sizeY >> 1;
  }

  void all() {
    updateBalloon();
    displayBalloon();
    displayBalloonString();
  }

  void updateBalloon() {
    if (x < hfX | x > width  - hfX)   mvX *= -1;
    if (y < hfY | y > height - hfY)   mvY *= -1;

    x += mvX;
    y += mvY;
  }

  void displayBalloon() {
    fill(cor);
    ellipse(x, y, szX, szY);
  }

  void displayBalloonString() {
    stroke(FG);
    line( x, y + hfY, x - mvX, y + szY );
    noStroke();
  }
}