xxxxxxxxxx
73
let square1, square2;
function setup() {
createCanvas(400, 400);
noFill();
// Create two squares with initial random properties
square1 = {
x: random(width),
y: random(height),
size: 50,
color: color(255, 0, 0),
shrink: false,
};
square2 = {
x: random(width),
y: random(height),
size: 50,
color: color(0, 0, 255),
shrink: false,
};
}
function draw() {
background(255);
// Draw squares
fill(square1.color);
rect(square1.x, square1.y, square1.size, square1.size);
fill(square2.color);
rect(square2.x, square2.y, square2.size, square2.size);
// Move squares towards the mouse position when clicked
if (mouseIsPressed) {
moveSquare(square1, mouseX, mouseY);
moveSquare(square2, mouseX, mouseY);
}
// Check for "embarrassment" (if squares overlap)
let distance = dist(square1.x, square1.y, square2.x, square2.y);
if (distance < square1.size / 2 + square2.size / 2) {
square1.shrink = true;
square2.shrink = true;
}
// If embarrassed, shrink and change color
if (square1.shrink) {
square1.size -= 0.5;
square1.color = color(random(255), random(255), random(255)); // Random color
if (square1.size < 10) {
square1.shrink = false; // Start over
square1.size = 50; // Reset size
}
}
if (square2.shrink) {
square2.size -= 0.5;
square2.color = color(random(255), random(255), random(255)); // Random color
if (square2.size < 10) {
square2.shrink = false; // Start over
square2.size = 50; // Reset size
}
}
}
function moveSquare(square, targetX, targetY) {
// Move the square towards the target (mouse position)
square.x += (targetX - square.x) * 0.05;
square.y += (targetY - square.y) * 0.05;
}