Balls bouncing simultaneously

This is the link.

//var bubbles;
//var bubbles1;
let bubbles = [];

function setup() {
  createCanvas(400, 400);
  for(var i = 0; i < 1000; i++){
    let x = 10 * i;
    let y = 20 + 60 * i;
    bubbles[i] = new Bubble(x, y, 50, 50, 5);
  }
  
}

function draw() {
  background(0, 0, 225);
  for(var i = 0; i < bubbles.length; i++){
    bubbles[i].move();
    bubbles[i].show();
  }
}


class Bubble {
  constructor(x, y, size1, size2, moves){
    this.x = x;
    this.y = y;
    this.size1 = size1;
    this.size2 = size2;
    this.moves = moves;
  }
  
  move(){
    this.moves;
    //x increments by moves
    //x = x + 1
    this.x = this.x + this.moves
    //moves must be reversed
    if(this.x > width || this.x < 0){
      this.moves = this.moves * -1
      //moves = moves * (-1) etc
    }
  }
  
  show(){
    fill(255, 0, 0);
    stroke('red')
    noStroke()
    ellipse(this.x, this.y, this.size1, this.size2)
  }
}

Leave a Reply