xxxxxxxxxx
104
let robots = [];
let song;
function preload() {
song = loadSound('around.m4a');
}
function setup() {
createCanvas(800, 600);
background(0);
//play the song
song.play(0, 1, 1, 55);
//keep playing and repeat
song.addCue(145, function() {
song.stop();
song.play(0, 1, 1, 55);
});
}
function draw() {
background(0);
// Create a new robot every 30 frames
if (frameCount % 30 == 0) {
let temp = new Robot(width / 2, height / 2, robots.length * 0.1);
robots.push(temp);
}
// Display and move each robot
for (let i = robots.length - 1; i >= 0; i--) {
robots[i].move();
robots[i].display(); }
}
class Robot {
constructor(x, y, angle) {
this.angle = angle; // Starting angle for circular motion
this.headSize = random(12, 25);
this.bodySize = random(this.headSize + 5, this.headSize + 25);
this.headColor = color(random(255), random(255), random(255));
this.bodyColor = color(random(255), random(255), random(255));
//choose between visor or normal eeys
this.eyeType = floor(random(2));
this.orbitRadius = 200; // Radius of circular motion
this.speed = 0.02; // Speed of rotation
}
display() {
noStroke();
// Robot's body
fill(this.bodyColor);
rectMode(CENTER);
rect(this.x, this.y, this.bodySize, this.bodySize);
// Robot's head
fill(this.headColor);
rect(this.x, this.y - this.bodySize * 0.7, this.headSize, this.headSize);
// Robot's eyes
fill(255);
if (this.eyeType == 0) {
rect(
this.x,
this.y - this.bodySize * 0.85,
this.headSize * 0.7,
this.headSize * 0.2
);
} else {
rect(
this.x - this.headSize * 0.2,
this.y - this.bodySize * 0.75 - this.headSize * 0.2,
this.headSize * 0.2,
this.headSize * 0.2
);
rect(
this.x + this.headSize * 0.2,
this.y - this.bodySize * 0.75 - this.headSize * 0.2,
this.headSize * 0.2,
this.headSize * 0.2
);
}
}
move() {
// Update the angle for circular motion
this.angle += this.speed;
// Calculate the new x and y position
this.x = mouseX + this.orbitRadius * cos(this.angle);
this.y = mouseY + this.orbitRadius * sin(this.angle);
}
}