Text
Text
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Falling Cubes Game</title>
<style>
canvas {
display: block;
margin: 0 auto;
background: #f0f0f0;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
const canvas = document.getElementById("gameCanvas");
const ctx = canvas.getContext("2d");
// Game variables
const paddle = {
x: canvas.width / 2 - 50,
y: canvas.height - 20,
width: 100,
height: 10,
speed: 5,
dx: 0,
};
// Paddle movement
document.addEventListener("keydown", (e) => {
if (e.key === "ArrowLeft") paddle.dx = -paddle.speed;
if (e.key === "ArrowRight") paddle.dx = paddle.speed;
});
document.addEventListener("keyup", (e) => {
if (e.key === "ArrowLeft" || e.key === "ArrowRight") paddle.dx = 0;
});
// Draw paddle
function drawPaddle() {
ctx.fillStyle = "#0095DD";
ctx.fillRect(paddle.x, paddle.y, paddle.width, paddle.height);
}
// Draw cubes
function drawCubes() {
ctx.fillStyle = "#FF5733";
for (const cube of cubes) {
ctx.fillRect(cube.x, cube.y, cubeSize, cubeSize);
}
}
movePaddle();
moveCubes();
// Game loop
function gameLoop() {
update();
draw();
if (!gameOver) requestAnimationFrame(gameLoop);
}
// Start game
gameLoop();
</script>
</body>
</html>