xxxxxxxxxx
68
let balls = [];
class Ball{
constructor(x, y){
this.x = random(400);
this.y = random(400);
this.xspeed = 4;
this.yspeed = 3;
this.n = 1.25;
}
move(){
this.x = this.x + this.xspeed
this.y = this.y + this.yspeed
}
display(){
background((255-r), (255-b), (255-g))
stroke(random(r), random(b), random(g))
fill((r), (b), (g))
strokeWeight(4)
ellipse(this.x, this.y, this.n, this.n)
}
bounce(){
if (this.x > width || this.x < 0){
this.xspeed = (this.xspeed * 1.02) * -1
this.n = this.n * 1.25
}
if (this.y > height || this.y < 0){
this.yspeed = this.yspeed * 1.02 * -1
this.n = this.n * 1.25
}
if (this.n >= 400){
this.n = 1.25
}
}}
function setup() {
createCanvas(600, 400);
r = random(255);
g = random(255);
b = random(255);
for(let i = 0; i < 2; i++) {
balls[i] = new Ball()
}
}
function draw() {
for(let i = 0; i < balls.length; i++) {
balls[i].move()
balls[i].display()
balls[i].bounce()
}
}
function mousePressed() {
// Check if mouse is inside the circle
let d = dist(mouseX, mouseY, 360, 200);
if (d < 100) {
// Pick new random color values
r = random(255);
g = random(255);
b = random(255);
background((255-r), (255-b), (255-g))
}
}