xxxxxxxxxx
46
/* My current sketch shows three letters -- "h", "o", "p" that bounce back from the edge of the screen. Help me improve the sketch so that the letters would bounce independently from one another like in the example GIF. */
let bots = [];
let hop = ["h", "o", "p"];
function setup() {
createCanvas(400, 400);
angleMode(DEGREES);
for (let i = 0; i < 4; i++) {
bots[i] = new Bot();
}
}
function draw() {
background(255);
for (let i = 0; i < 3; i++) {
bots[i].move(hop[i]);
}
}
class Bot {
constructor() {
this.x = width / 2;
this.y = height / 2;
this.rx = random(-2, 2);
this.ry = 1;
this.rc = color(random(255), random(255), random(255), random(255));
this.rs = color(random(255), random(255), random(255), random(255));
}
move(word) {
fill(this.rc);
stroke(this.rs);
textSize(200);
text(word, this.x, this.y);
this.x += this.rx;
this.y += this.ry;
if (this.x >= width - 100 || this.x <= -10) {
this.rx = -this.rx;
}
if (this.y >= height || this.y <= 100) {
this.ry = -this.ry;
}
}
}