xxxxxxxxxx
59
// Create an array to store ball objects
let balls = [];
function setup() {
createCanvas(400, 400);
}
function draw() {
background(220);
// Iterate through all the ball objects and run them
for(let ball of balls) {
ball.run();
}
}
// Create new balls with a mousepress
function mousePressed() {
balls.push(new Ball(mouseX, mouseY, random(-5, 5), random(-5,5)));
}
// Ball class definition
class Ball {
// Construct a new ball object with these parameters
constructor(x, y, xspeed, yspeed) {
this.x = x;
this.y = y;
this.xspeed = xspeed;
this.yspeed = yspeed;
}
run() {
this.move();
this.bounce();
this.display();
}
move() {
this.x+=this.xspeed;
this.y+=this.yspeed;
}
bounce() {
if(this.x < 0 || this.x > width) {
this.xspeed *= -1;
}
if(this.y < 0 || this.y > height) {
this.yspeed *= -1;
}
}
display() {
ellipse(this.x, this.y, 50, 50);
}
}