Java Script
Java Script
canvas.width = 800;
canvas.height = 600;
// Bubble object
class Bubble {
constructor(x, y, color) {
this.x = x;
this.y = y;
this.radius = 20;
this.color = color;
this.dx = 0;
this.dy = 0;
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2);
ctx.fillStyle = this.color;
ctx.fill();
ctx.closePath();
}
update() {
this.x += this.dx;
this.y += this.dy;
}
checkCollision(bubbles) {
for (let i = 0; i < bubbles.length; i++) {
const b = bubbles[i];
const distance = Math.sqrt((this.x - b.x) ** 2 + (this.y - b.y) ** 2);
if (distance < this.radius + b.radius) {
return b;
}
}
return null;
}
}
// Game variables
let shooterBubble = new Bubble(canvas.width / 2, canvas.height - 30, "red");
let bubbles = [];
let isShooting = false;
// Game loop
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
requestAnimationFrame(gameLoop);
}