xxxxxxxxxx
41
class Ball {
constructor() {
this.x = random(width)
this.y = random(0, 10)
this.diameter = random(10, 70)
this.speed = random(0, 3)
this.color = [random(255), random(255), random(255), 100]
}
draw() {
circle(this.x, this.y, this.diameter)
fill(this.color)
}
move() {
this.speed += gravity
this.y += this.speed
if (this.y > height - this.diameter / 2) {
this.speed *= -0.85
this.y = height - this.diameter / 2
}
}
}
let balls = []
const gravity = 0.2
function setup() {
createCanvas(400, 400);
noStroke()
while (balls.length < 20) {
balls.push(new Ball())
}
}
function draw() {
background(220);
for (let ball of balls) {
ball.draw()
ball.move()
}
}