xxxxxxxxxx
96
// Circle class
class Circle {
constructor(x, y, maxRadius) {
this.x = x; // X-coordinate of the circle
this.y = y; // Y-coordinate of the circle
this.radius = 0; // Initial radius
this.maxRadius = maxRadius; // Maximum size of circle growth
this.growthRate = random(0.5, 3); // Speed of growth
this.clicked = false; // Check if circle has been clicked
this.color = random(colorArray); // Random color
}
// Method to draw the circle
display() {
noStroke();
fill(this.color);
circle(this.x, this.y, this.radius * 2);
}
// Grow circle
grow() {
if (!this.clicked) { // Only grow if not clicked
this.radius += this.growthRate;
if (this.radius > this.maxRadius) {
this.radius = this.maxRadius; // Capping the radius
}
}
}
// Check point is inside circle
isInside(x, y) {
let d = dist(x, y, this.x, this.y); // Distance between point and circle center
return d < this.radius; // True if point inside circle
}
// Mark circle as clicked
click() {
this.clicked = true;
}
}
// Array to store all circles
let circles = [];
const numCircles = 10;
let colorArray = ['rgb(255,192,212)','purple','rgb(255,255,198)','rgb(129,129,255)','rgb(216,97,216)'];
function setup() {
createCanvas(600, 600);
frameRate(30); // Control growth speed
generateRandomCircles(); // Generate random circles
}
function draw() {
background('rgb(212,246,255)');
// Draw text
textSize(32);
fill('rgb(28,28,160)');
textAlign(CENTER, CENTER);
textFont('Courier New')
text("Pop the balloons!", width / 2, 50);
// Update and display circles
for (let i = circles.length - 1; i >= 0; i--) {
circles[i].grow();
circles[i].display();
// Remove circle if clicked from array
if (circles[i].clicked) {
circles.splice(i, 1);
}
}
}
// Function to generate random circles
function generateRandomCircles() {
for (let i = 0; i < numCircles; i++) {
let x = random(width);
let y = random(height);
let maxRadius = random(50, 150);
let newCircle = new Circle(x, y, maxRadius);
circles.push(newCircle); // Add new circle to array
}
}
// Mouse click function
function mousePressed() {
for (let i = circles.length - 1; i >= 0; i--) {
if (circles[i].isInside(mouseX, mouseY)) {
circles[i].click(); // Mark circle as clicked
}
}
}