Code 2, HTML
Code 2, HTML
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Fireball Dodge</title>
<style>
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f0f0f0;
}
canvas {
border: 1px solid #000;
}
</style>
</head>
<body>
<canvas id="gameCanvas" width="400" height="400"></canvas>
<script>
// JavaScript code for the game goes here
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
// Fireballs array
let fireballs = [];
// Game variables
let score = 0;
let gameSpeed = 2;
let gameOver = false;
// Game loop
function gameLoop() {
if (!gameOver) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
player.y += player.velocityY;
// Draw player
ctx.fillStyle = 'blue';
ctx.fillRect(player.x, player.y, player.width, player.height);
// Generate fireballs
if (Math.random() < 0.02) {
fireballs.push({
x: Math.random() * canvas.width,
y: 0,
radius: 10,
speed: Math.random() * 3 + 1 // random speed between 1 and
4
});
}
// Display score
ctx.fillStyle = 'black';
ctx.font = '24px Arial';
ctx.fillText('Score: ' + score, 10, 30);
// Request next frame
requestAnimationFrame(gameLoop);
} else {
// Game over
ctx.fillStyle = 'black';
ctx.font = '36px Arial';
ctx.fillText('Game Over!', canvas.width / 2 - 100, canvas.height /
2 - 50);
ctx.fillText('Score: ' + score, canvas.width / 2 - 80,
canvas.height / 2);
}
}