BEGHAR
let cursor;
class Module {
constructor(xOff,
yOff, x, y, speed, unit) {
this.xOff = xOff;
this.yOff = yOff;
this.x = x;
this.y = y;
this.speed = speed;
this.unit = unit;
this.xDir = 1;
this.yDir = 1;
}
// Custom method for updating
the variables
update() {
this.x = this.x
+ this.speed * this.xDir;
this.y = this.y
+ this.speed * this.yDir;
if (this.x
>= this.unit || this.x
<= 0) {
this.xDir *= -1;
this.x = this.x
+ 1 * this.xDir;
// this.y = this.y
+ 1 * this.yDir;
}
if (this.y
>= this.unit || this.y
<= 0) {
this.yDir *= -1;
this.y = this.y
+ 1 * this.yDir;
}
}
// Custom method for drawing
the object
draw() {
fill(255);
image(cursor,this.xOff
+ this.x, this.yOff
+ this.y, 25, 25);
}
}
let unit = 20;
let count;
let mods = [];
function
setup() {
createCanvas(windowWidth,600);
cursor = loadImage
(
“https://va-scce-aud.in/wp-content/uploads/2020/05/cursor2.png”
);
let wideCount = width
/ unit;
let highCount = height
/ unit;
count = wideCount *
highCount;
let index = 0;
for (let y = 0; y < highCount; y++) {
for (let x = 0; x <
wideCount; x++) {
mods[index++] =
new Module(
x *
unit,
y *
unit,
unit /
2,
unit /
2,
random(0.2,2),
unit
);
}
}
}
function
draw() {
background(0);
for (let i = 0; i < count; i++) {
mods[i].update();
mods[i].draw();
}
}