gpt-4o-2024-08-06· OpenAI
WildChat-4.8M (AI2)
make a little bubble bobble like game in js
- User
make a little bubble bobble like game in js
- Assistant
make a little bubble bobble like game in js
Creating a simple Bubble Bobble-like game in JavaScript can be a fun project. For simplicity, we can use HTML5 Canvas for rendering graphics. The following code provides a foundational structure to create a basic game loop with a player character that can move left and right, and jump. For complete functionality, you would need to expand on this base by adding features like enemies, scoring, and more complex levels.
Here is a basic version to get you started:
<!DOCTYPE html>
<html>
<head>
<title>Bubble Bobble-like Game</title>
<style>
canvas { background-color: #000; display: block; margin: auto; }
</style>
</head>
<body>
<canvas id="gameCanvas" width="800" height="600"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Game settings
const GAME_WIDTH = canvas.width;
const GAME_HEIGHT = canvas.height;
// Player settings
const player = {
x: 100,
y: GAME_HEIGHT - 60,
width: 40,
height: 40,
color: 'cyan',
speed: 5,
dx: 0,
dy: 0,
gravity: 0.3,
jumpPower: -10,
onGround: false
};
// Keyboard input handling
const keys = {
right: false,
left: false,
up: false
};
window.addEventListener('keydown', event => {
if (event.code === 'ArrowRight') keys.right = true;
if (event.code === 'ArrowLeft') keys.left = true;
if (event.code === 'ArrowUp') {
if (player.onGround) {
player.dy = player.jumpPower;
player.onGround = false;
}
}
});
window.addEventListener('keyup', event => {
if (event.code === 'ArrowRight') keys.right = false;
if (event.code === 'ArrowLeft') keys.left = false;
});
// Update game objects
function update() {
// Player movement logic
player.dx = 0;
if (keys.right) player.dx = player.speed;
if (keys.left) player.dx = -player.speed;
player.x += player.dx;
player.dy += player.gravity;
player.y += player.dy;
// Collision detection with floor
if (player.y + player.height > GAME_HEIGHT) {
player.y = GAME_HEIGHT - player.height;
player.dy = 0;
player.onGround = true;
}
// Collision with walls
if (player.x < 0) player.x = 0;
if (player.x + player.width > GAME_WIDTH) player.x = GAME_WIDTH - player.width;
}
// Render game objects
function render() {
// Clear canvas
ctx.clearRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
// Draw player
ctx.fillStyle = player.color;
ctx.fillRect(player.x, player.y, player.width, player.height);
}
// Game loop
function gameLoop() {
update();
render();
requestAnimationFrame(gameLoop);
}
// Start the game
gameLoop();
</script>
</body>
</html>
This script provides a foundation that you can build upon to create a more robust and engaging Bubble Bobble-inspired game.