class Brick{
constructor(bc, y){
this.brickColor = bc;
this.yPos = y;
this.xPos = 0;
}
createBrick(){
fill(this.brickColor);
rect(this.xPos, this.yPos, 100, 50);
}
setSpeed(){
this.xSpeed = 1;
}
moveBrick(){
this.xPos+=this.xSpeed;
if(this.xPos+100 >= width || this.xPos <= 0){
this.xSpeed*=-1;
}
}
}
function setup() {
createCanvas(720, 400);
createP("Keep the mouse clicked").style('color','#ffffff');
createP("to check whether the bricks").style('color','#ffffff');
createP("are moving at same speed or not").style('color','#ffffff');
}
let brick1 = new Brick("white",100);
let brick2 = new Brick("black",250);
brick1.setSpeed();
brick2.setSpeed();
function draw () {
background(0);
if(mouseIsPressed){
background(50);
}
brick1.createBrick();
brick1.moveBrick();
if(!mouseIsPressed){
createBars();
}
brick2.createBrick();
brick2.moveBrick();
}
function createBars() {
let len = 12;
for(let i = 0;i<width/len;i++){
fill("white");
if(i%2 === 0)
rect(i*len,height,len,-height);
}
}