xxxxxxxxxx
65
let ball;
let balls = [];
class Orbison {
constructor() {
this.x = random(width);
this.y = random(100);
this.rad = 25;
this.o = random(255);
this.velo = createVector(1, 1);
this.accel = createVector(0.25, 0.25);
}
show() {
noStroke();
fill(0,0,255, this.o);
ellipse(this.x, this.y, this.rad * 2, this.rad * 2);
}
add() {
// Apply acceleration to velocity
this.velo.add(this.accel);
// Update position by velocity
this.x += this.velo.x;
this.y += this.velo.y;
// Bounce off the bottom edge
if (this.y >= height - this.rad) {
this.y = height - this.rad; // Make sure it stays within bounds
this.velo.y *= -0.9; // Reverse and dampen velocity
}
// Bounce off the top edge
if (this.y <= this.rad) {
this.y = this.rad;
this.velo.y *= -0.9;
}
// Bounce off the left or right edges
if (this.x >= width - this.rad || this.x <= this.rad) {
this.velo.x *= -0.9;
}
}
}
function setup() {
createCanvas(windowWidth * .9, windowHeight * .9);
ball = new Orbison();
for (let i = 0; i < 10; i++) {
balls.push(new Orbison());
}
}
function draw() {
background(245, 245, 255);
ball.show();
ball.add();
for (let i = 0; i < balls.length; i++) {
balls[i].show();
balls[i].add();
}
}