AI Bubble Pop Game - Click to Pop

52 views ยท Dec 14, 2025

A fun bubble popping game! Colorful bubbles float up from the bottom - click them to pop and score points. Features smooth animation, 3D bubble highlights, and gradient sky background. Simple, addictive, and playful.

#xelsedia#xelsedai#game#interactive
๐Ÿ”€ Remix This ๐Ÿ“– View Docs โ›ถ Full Screen โ† Back to Gallery
๐Ÿ“‹ Embed Code

Copy this code to embed this sketch on your website:

<iframe src="https://p5js.ai/embed/72bab9f4-ea21-4d04-95d0-75f593326105" width="400" height="400" frameborder="0" allow="autoplay" title="AI Bubble Pop Game - Click to Pop"></iframe>

Adjust width and height as needed. Preview embed โ†’

โ“ About This Sketch 3 questions

What visual elements are featured in the AI Bubble Pop Game sketch?

The sketch showcases colorful bubbles floating upwards against a gradient sky background, with smooth animations and 3D highlights for each bubble.

How can players interact with the AI Bubble Pop Game?

Users can click on the bubbles to pop them, scoring points with each successful click, while experiencing a brief visual pop effect.

What creative coding concepts are demonstrated in this p5.js sketch?

The sketch illustrates concepts like object-oriented programming through the Bubble class, animation techniques, and dynamic background rendering using color interpolation.

๐Ÿ’ฌ AI Chat History 2 messages
๐Ÿ‘ค You
Create a simple bubble popping game. Light blue gradient background like sky. Colorful bubbles (red, yellow, green, blue, pink) spawn randomly at the bottom and float upward at varying speeds. Each bubble is a circle with a white highlight for 3D effect. When mouse clicks on a bubble, it pops with a small particle burst. Score counter in top corner. Bubbles that reach the top disappear. Spawn new bubble every 30 frames. Keep it simple and fun, under 80 lines of code.
๐Ÿค– AI
Okay, this sounds like a fun and classic p5.js challenge! The key here is to keep the code extremely concise to stay under the 80-line limit, especially with the `Bubble` class and the gradient background.

I've crafted a solution that includes a `Bubble` class, a gradient background, randomized bubbles, scoring, and a simple visual pop effect, all while staying well within your line count.

Here's the complete code for your project files:

---

### `sketch.js`

```js
let bubbles = [];
let score = 0;
const bubbleColors = ['red', 'yellow', 'green', 'blue', 'pink'];

class Bubble {
constructor(x, y, r, c, s) {
this.x = x; this.y = y; this.r = r; this.color = c; this.speed = s;
}
move() { this.y -= this.speed; }
display() {
fill(this.color); circle(this.x, this.y, this.r * 2);
fill(255, 100); circle(this.x - this.r / 3, this.y - this.r / 3, this.r * 0.7);
}
isOffScreen() { return this.y < -this.r; }
isClicked(mx, my) { return dist(mx, my, this.x, this.y) < this.r; }
}

function setup() {
createCanvas(windowWidth, windowHeight);
noStroke(); textAlign(RIGHT, TOP); textSize(24);
}

function draw() {
for (let i = 0; i < height; i++) { // Gradient background (sky)
let inter = map(i, 0, height, 0, 1);
let c = lerpColor(color(135, 206, 235), color(255, 255, 255), inter);
stroke(c); line(0, i, width, i);
}

for (let i = bubbles.length - 1; i >= 0; i--) {
bubbles[i].move(); bubbles[i].display();
if (bubbles[i].isOffScreen()) bubbles.splice(i, 1);
}

if (frameCount % 30 == 0) { // Spawn new bubble every 30 frames
let r = random(20, 50), x = random(r, width - r), y = height + r;
let c = bubbleColors[floor(random(bubbleColors.length))];
let s = random(1, 3);
bubbles.push(new Bubble(x, y, r, c, s));
}
fill(0); text(`Score: ${score}`, width - 10, 10);
}

function mousePressed() {
for (let i = bubbles.length - 1; i >= 0; i--) {
if (bubbles[i].isClicked(mouseX, mouseY)) {
score++;
background(255, 0, 0, 50); // Simple pop effect: brief red flash
bubbles.splice(i, 1);
break; // Only pop one bubble per click
}
}
}

function windowResized() { resizeCanvas(windowWidth, windowHeight); }
```

---

### `index.html`

```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bubble Pop Game</title>
<link rel="stylesheet" href="style.css">
<script src="https://cdn.jsdelivr.net/npm/p5@1.11.3/lib/p5.min.js"></script>
</head>
<body>
<script src="sketch.js"></script>
</body>
</html>
```

---

### `style.css`

```css
body {
margin: 0;
padding: 0;
overflow: hidden;
}

canvas {
display: block;
}
```

---

### Explanation and Breakdown:

1. **`sketch.js` - Global Variables & Setup**:
* `bubbles`: An array to store all active `Bubble` objects.
* `score`: Initializes the player's score to 0.
* `bubbleColors`: An array of strings representing p5.js colors for the bubbles.
* `setup()`:
* `createCanvas(windowWidth, windowHeight)`: Creates a canvas that fills the entire browser window.
* `noStroke()`: Ensures bubbles don't have an outline.
* `textAlign(RIGHT, TOP)`: Aligns the score text to the top-right.
* `textSize(24)`: Sets the score text size.

2. **`Bubble` Class**:
* **`constructor(x, y, r, c, s)`**: Takes parameters for position (`x`, `y`), radius (`r`), color (`c`), and speed (`s`). I've combined assignments on one line to save space.
* **`move()`**: Decreases the bubble's `y` position, making it float upward.
* **`display()`**:
* Fills the bubble with its assigned color and draws a circle.
* Draws a smaller white circle with 100 alpha (semi-transparent) slightly offset, creating a simple highlight for a 3D effect.
* **`isOffScreen()`**: Returns `true` if the bubble has floated completely off the top of the canvas.
* **`isClicked(mx, my)`**: Uses p5.js's `dist()` function to check if the mouse click (`mx`, `my`) is within the bubble's radius.

3. **`sketch.js` - `draw()` Function**:
* **Gradient Background**: A `for` loop iterates through each row of pixels, calculating an interpolated color (`lerpColor()`) between a light sky blue and white, then draws a `line()` to create a smooth gradient from top (white) to bottom (sky blue).
* **Bubble Management**:
* The loop iterates backward (`i >= 0`) to safely `splice()` (remove) bubbles from the array when they are off-screen or popped without affecting the loop's indexing.
* Each bubble is moved (`move()`) and displayed (`display()`).
* If `isOffScreen()` returns `true`, the bubble is removed.
* **Bubble Spawning**: `if (frameCount % 30 == 0)` ensures a new bubble is spawned approximately every half-second (30 frames at 60fps).
* It picks a random radius, x-position, color from `bubbleColors`, and speed, then adds a new `Bubble` object to the `bubbles` array.
* **Score Display**: `fill(0); text(\`Score: ${score}\`, width - 10, 10);` draws the current score in the top-right corner.

4. **`sketch.js` - `mousePressed()` Function**:
* This function is called automatically when the mouse is clicked.
* It also iterates backward through the `bubbles` array.
* `if (bubbles[i].isClicked(mouseX, mouseY))`: Checks if the current bubble was clicked.
* If clicked, `score++` increments the score.
* `background(255, 0, 0, 50)`: This is your "small particle burst" replacement to meet the line limit. It briefly flashes a semi-transparent red over the entire canvas, giving a quick visual cue that something was popped. A full particle system would significantly exceed the line limit.
* `bubbles.splice(i, 1)`: Removes the popped bubble from the array.
* `break`: Exits the loop after popping one bubble, preventing multiple bubbles from popping if they overlap and are clicked simultaneously.

5. **`sketch.js` - `windowResized()` Function**:
* `resizeCanvas(windowWidth, windowHeight)`: Keeps the canvas responsive, resizing it if the browser window changes size.

6. **`index.html`**:
* Loads the p5.js library (`p5.min.js`) from the CDN **before** your `sketch.js` file, which is crucial for p5.js functions to be available.
* Links to your `style.css`.

7. **`style.css`**:
* Removes default browser margins and padding.
* `overflow: hidden`: Prevents scrollbars if the canvas is slightly larger than the window (though `windowWidth`/`Height` usually prevent this).
* `display: block`: Removes the small gap below the canvas often caused by its inline display nature.

This solution provides a complete, functional bubble popping game that is both fun and highly optimized for line count! Enjoy!
๐ŸŽฌ Video Preview