Move the mouse around to pop the balloons. Break your own record. Have fun!
xxxxxxxxxx
/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.sb5Y1F1$PYH/rev.39
*
* authors:
* GoToLoop
*
* This version 03/04/2025 by
* Juan Carlos Ponce Campuzano
*
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
/**************************************************************************\
* Poppable Balloons (v2.70)
* by MalibuGranPrix2000 & Asimes (2012/Nov)
*
* http://forum.processing.org/topic/arrays-28-11-2012
*
* http://studio.processingtogether.com/sp/pad/export/ro.9V7R1wu55fOiN/latest
*/
/**************************************************************************/
let timer, rec = 25, elapsed = rec;
let aliveNum = 0;
const FPS = 30, SPD = 6, BOLD_TEXT = 4, TEXT_SIZE = 25;
const BALL_NUM = 100, BALL_SIZE = 60;
let balloons = []; // Will initialize in setup()
let BG, FG, NAVY, SADDLE_BROWN; // Will define in setup()
function setup() {
createCanvas(800, 600);
frameRate(FPS);
noSmooth();
cursor('pointer');
// Define colors
BG = 255;
//FG = 0;
NAVY = color(134, 143, 193)
SADDLE_BROWN = color(144, 64, 16);
textLeading(TEXT_SIZE);
textSize(TEXT_SIZE);
//stroke(FG);
strokeWeight(BOLD_TEXT);
// Initialize balloons array
for (let i = 0; i < BALL_NUM; i++) {
balloons[i] = new Balloon();
}
}
function draw() {
background(BG);
if (aliveNum === 0) createBalloons();
for (let i = 0; i < BALL_NUM; i++) {
balloons[i].all();
}
showScore();
}
function showScore() {
elapsed = floor((millis() - timer)/1000);
push();
noStroke();
fill(NAVY);
text("Elapsed:\nRecord:\nAlive:", 20, 40);
fill(SADDLE_BROWN);
text(elapsed + "\n" + rec + "\n" + aliveNum, TEXT_SIZE*6, 40);
pop();
}
function createBalloons() {
rec = min(rec, elapsed);
for (let i = 0; i < BALL_NUM; i++) {
let w = floor(random(BALL_SIZE/2, BALL_SIZE + 1));
let h = floor(random(10, BALL_SIZE/2)) + w;
let x = floor(random(w/2, width - w/2));
let y = floor(random(h/2, height - h/2));
let mvX = floor(random(SPD/2, SPD + 1));
let mvY = floor(random(1, SPD/2)) + mvX;
let c = color(random(255), random(255), random(255));
balloons[i].respawn(x, y, w, h, mvX, mvY, c);
}
aliveNum = BALL_NUM;
timer = millis();
}
class Balloon {
constructor(xx, yy, ww, hh, sx, sy, ink) {
if (arguments.length === 0) {
this.isDead = true;
} else {
this.respawn(xx, yy, ww, hh, sx, sy, ink);
}
}
respawn(xx, yy, ww, hh, sx, sy, ink) {
this.x = xx;
this.y = yy;
this.w = ww;
this.h = hh;
this.rw = ww >> 1;
this.rh = hh >> 1;
this.vx = sx;
this.vy = sy;
this.c = ink;
this.isDead = false;
}
display() {
push();
strokeWeight(1);
fill(this.c);
ellipse(this.x, this.y, this.w, this.h);
pop();
}
update() {
if ((this.x += this.vx) < this.rw || this.x > width - this.rw) this.vx *= -1;
if ((this.y += this.vy) < this.rh || this.y > height - this.rh) this.vy *= -1;
}
isHit() {
return sq(mouseX - this.x)/(this.rw*this.rw) + sq(mouseY - this.y)/(this.rh*this.rh) < 1;
}
all() {
if (this.isDead) return;
this.update();
this.display();
if (this.isDead = this.isHit()) aliveNum--;
}
}