make a tiny bubble bobble game in js code
- User
make a tiny bubble bobble game in js code
- Assistant
- User
make it so it wokrs in my online editor
- Assistant
make a tiny bubble bobble game in js code
make it so it wokrs in my online editor
Creating a simplified version of Bubble Bobble using JavaScript is an excellent way to understand basic game development concepts such as rendering, animation, and user interaction. Below is a basic implementation using the HTML5 Canvas API. This example will cover rendering a player character that can move left and right and shoot bubbles.
First, structure your HTML to include a canvas element:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tiny Bubble Bobble</title>
<style>
canvas {
background-color: #000;
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script src="bubbleBobble.js"></script>
</body>
</html>
Then, create a bubbleBobble.js file to handle the game logic:
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const player = {
x: 100,
y: 500,
width: 50,
height: 50,
dx: 5
};
const bubbles = [];
const bubbleSpeed = 2;
function drawPlayer() {
ctx.fillStyle = "green";
ctx.fillRect(player.x, player.y, player.width, player.height);
}
function drawBubbles() {
ctx.fillStyle = "cyan";
bubbles.forEach(bubble => {
ctx.beginPath();
ctx.arc(bubble.x, bubble.y, bubble.radius, 0, Math.PI * 2);
ctx.fill();
});
}
function updateBubbles() {
bubbles.forEach(bubble => {
bubble.y -= bubbleSpeed;
});
// Remove bubbles that have gone off the screen
while(bubbles.length && bubbles[0].y + bubbles[0].radius < 0) {
bubbles.shift();
}
}
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function update() {
clearCanvas();
drawPlayer();
drawBubbles();
updateBubbles();
}
function movePlayer(dir) {
if (dir === 'left' && player.x > 0) {
player.x -= player.dx;
} else if (dir === 'right' && player.x < canvas.width - player.width) {
player.x += player.dx;
}
}
function shootBubble() {
const bubble = {
x: player.x + player.width / 2,
y: player.y,
radius: 10
};
bubbles.push(bubble);
}
document.addEventListener('keydown', (event) => {
if (event.key === 'ArrowLeft') {
movePlayer('left');
} else if (event.key === 'ArrowRight') {
movePlayer('right');
} else if (event.key === ' ') { // Space bar to shoot
shootBubble();
}
});
function gameLoop() {
update();
requestAnimationFrame(gameLoop);
}
gameLoop();
This code creates a simple player character that can move left and right and shoot bubbles straight up by pressing the space bar. The game is rendered and updated using the requestAnimationFrame loop for smooth animations.
To expand this game, you could consider adding enemies, scoring, and more skillful interactions. Implementing a full Bubble Bobble game would require additional game mechanics, graphics, and sound.
To ensure the game runs smoothly in an online editor or when you host it on any web server, you can follow these steps to make sure all parts of the code are correctly set up. Here's a concise way to verify everything runs well:
Create an HTML file, for instance, index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tiny Bubble Bobble</title>
<style>
canvas {
background-color: #000;
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
const player = {
x: 100,
y: 500,
width: 50,
height: 50,
dx: 5
};
const bubbles = [];
const bubbleSpeed = 2;
function drawPlayer() {
ctx.fillStyle = "green";
ctx.fillRect(player.x, player.y, player.width, player.height);
}
function drawBubbles() {
ctx.fillStyle = "cyan";
bubbles.forEach(bubble => {
ctx.beginPath();
ctx.arc(bubble.x, bubble.y, bubble.radius, 0, Math.PI * 2);
ctx.fill();
});
}
function updateBubbles() {
bubbles.forEach(bubble => {
bubble.y -= bubbleSpeed;
});
// Remove bubbles that have gone off the screen
while(bubbles.length && bubbles[0].y + bubbles[0].radius < 0) {
bubbles.shift();
}
}
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
}
function update() {
clearCanvas();
drawPlayer();
drawBubbles();
updateBubbles();
}
function movePlayer(dir) {
if (dir === 'left' && player.x > 0) {
player.x -= player.dx;
} else if (dir === 'right' && player.x < canvas.width - player.width) {
player.x += player.dx;
}
}
function shootBubble() {
const bubble = {
x: player.x + player.width / 2,
y: player.y,
radius: 10
};
bubbles.push(bubble);
}
document.addEventListener('keydown', (event) => {
if (event.key === 'ArrowLeft') {
movePlayer('left');
} else if (event.key === 'ArrowRight') {
movePlayer('right');
} else if (event.key === ' ') { // Space bar to shoot
shootBubble();
}
});
function gameLoop() {
update();
requestAnimationFrame(gameLoop);
}
gameLoop();
</script>
</body>
</html>
Canvas Element:
<canvas> element is used to render the game graphics.Player Handling:
ArrowLeft, ArrowRight).Bubble Shooting:
).Animation and Rendering:
gameLoop function ensures continuous updating and rendering of the game state using requestAnimationFrame.Code Structure:
<script> tag inside the HTML document to ensure it runs directly without external file dependencies—ideal for quick hosting or online editors.By following this setup, you should be able to run and test the game seamlessly in an online editor like CodePen, JSFiddle, or directly hosting on a web server.
Not yet included in a published release.