xxxxxxxxxx
74
class Bouncingrectangles {
constructor(xSpeed0, ySpeed0) {
this.xPos = width/50;
this.yPos = random(1000, 50);
this.wPos = random(50, 250);
this.hPos = random(20,399);
this.xSpeed = xSpeed0;
this.ySpeed = ySpeed0;
}
move() {
// move the ball
this.xPos += this.xSpeed;
this.yPos += this.ySpeed;
}
checkForCollisions() {
// check first for left and right wall
if (this.xPos <= 15 || this.xPos >= width - 15) {
this.xSpeed = -this.xSpeed;
}
if (this.yPos <= 40 || this.yPos >= height - 40) {
this.ySpeed = -this.ySpeed;
}
}
draw() {
rect(this.xPos, this.yPos, this.wPos, this.hPos);
let color=(random(250));
fill(color);
if (mouseIsPressed) {
color = fill(random(255), random(255), random(255),160);
}
}
}
let rectangles = [];
function setup() {
createCanvas(400, 400);
for (let i = 0; i < 40; i++) {
rectangles[i] = new Bouncingrectangles(i + 3, 2);
}
}
function draw() {
for (let i = 0; i < 12; i++) {
rectangles[i].move();
rectangles[i].checkForCollisions();
rectangles[i].draw();
}
}