xxxxxxxxxx
54
//Part 1: Write a class that makes 20 bouncing balls.
//Part 2: Change the balls to something else (another shape? words? a design? etc.) and update the Class name to better describe the new object.
//Part 3: Add a new function (aka method) to the Class so the object changes in a new way.
let bubbles = [];
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 20; i++) {
let bubble = new Bubble(random(300), random(200), random(6), random(10),random(100));
bubbles.push(bubble)
}
}
function draw() {
background(255);
fill(11,6,98,75);
noStroke();
for (let i = 0; i < bubbles.length; i++) {
bubbles[i].display();
bubbles[i].bounce();
bubbles[i].move();
}
}
class Bubble {
constructor(x,y,xs,ys,size) {
this.x = x;
this.y = y;
this.xspeed = xs;
this.yspeed = ys;
this.size = size;
}
display() {
circle(this.x, this.y, this.size);
}
move() {
this.x += this.xspeed;
this.y += this.yspeed;
}
bounce() {
if (this.x > width || this.x < 0) {
this.xspeed *= -1;
}
if (this.y > width || this.y < 0) {
this.yspeed *= -1;
}
}
}