1a2
1a2
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dodge the Falling Obstacles</title>
<style>
body {
margin: 0;
overflow: hidden;
font-family: Arial, sans-serif;
}
canvas {
display: block;
background: #222;
}
.controls {
position: absolute;
top: 10px;
left: 50%;
transform: translateX(-50%);
z-index: 10;
}
button {
font-size: 16px;
margin: 5px;
padding: 10px 20px;
cursor: pointer;
border: none;
border-radius: 5px;
background: #00aaff;
color: white;
transition: background 0.3s;
}
button:hover {
background: #007acc;
}
</style>
</head>
<body>
<div class="controls">
<button id="respawnButton">Respawn</button>
<button id="pauseButton">Pause</button>
</div>
<script>
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
document.body.appendChild(canvas);
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
const player = {
x: canvas.width / 2,
y: canvas.height - 50,
width: 30,
height: 30,
color: '#00ff00',
speed: 10
};
function resetGame() {
player.x = canvas.width / 2;
player.y = canvas.height - 50;
obstacles.length = 0;
giantBlocks.length = 0;
frameCount = 0;
dodgedCount = 0;
gameOver = false;
}
function createObstacle() {
const x = Math.random() * (canvas.width - 30);
obstacles.push({ x, y: 0, width: 30, height: 30, speed: smallBlockBaseSpeed,
color: '#ff0000' });
}
function spawnGiantBlock() {
const size = canvas.width * 0.6; // 60% of screen width
const x = Math.random() * (canvas.width - size);
const y = -size; // Start off-screen
giantBlocks.push({ x, y, size, speed: giantBlockSpeed, color: '#ff7700' });
}
function updatePlayer() {
if (keys['ArrowLeft'] && player.x > 0) player.x -= player.speed;
if (keys['ArrowRight'] && player.x < canvas.width - player.width) player.x +=
player.speed;
}
function updateObstacles() {
obstacles.forEach(obstacle => {
obstacle.y += obstacle.speed;
obstacle.speed += smallBlockAcceleration; // Increase speed over time
// Check collision
if (
player.x < obstacle.x + obstacle.width &&
player.x + player.width > obstacle.x &&
player.y < obstacle.y + obstacle.height &&
player.y + player.height > obstacle.y
) {
gameOver = true;
}
});
function updateGiantBlocks() {
giantBlocks.forEach((block, index) => {
block.y += block.speed;
function drawPlayer() {
ctx.fillStyle = player.color;
ctx.fillRect(player.x, player.y, player.width, player.height);
}
function drawObstacles() {
obstacles.forEach(obstacle => {
ctx.fillStyle = obstacle.color;
ctx.fillRect(obstacle.x, obstacle.y, obstacle.width, obstacle.height);
});
}
function drawGiantBlocks() {
giantBlocks.forEach(block => {
ctx.fillStyle = block.color;
ctx.fillRect(block.x, block.y, block.size, block.size);
});
}
function drawGameOver() {
ctx.fillStyle = '#fff';
ctx.font = '48px Arial';
ctx.textAlign = 'center';
ctx.fillText('Game Over!', canvas.width / 2, canvas.height / 2);
}
function drawDodgedCount() {
ctx.fillStyle = '#fff';
ctx.font = '20px Arial';
ctx.fillText(`Dodged: ${dodgedCount}`, 10, 30);
}
function gameLoop() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
requestAnimationFrame(gameLoop);
}
// Event Listeners
window.addEventListener('keydown', e => (keys[e.key] = true));
window.addEventListener('keyup', e => (keys[e.key] = false));
document.getElementById('respawnButton').addEventListener('click', resetGame);
document.getElementById('pauseButton').addEventListener('click', () => {
isPaused = !isPaused;
const pauseButton = document.getElementById('pauseButton');
pauseButton.textContent = isPaused ? 'Resume' : 'Pause';
});
gameLoop();
</script>
</body>
</html>