<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> body { margin: 0; overflow: hidden; } canvas { display: block; } #score { position: absolute; top: 10px; left: 10px; color: white; font-size: 20px; font-family: Arial, sans-serif; } #timer { position: absolute; top: 10px; right: 10px; color: white; font-size: 20px; font-family: Arial, sans-serif; } </style> <title>Balloon Pop Game</title> </head> <body> <canvas id="gameCanvas"></canvas> <div id="score">Balloons Popped: 0 | Balloons Missed: 0</div> <div id="timer">Time: 02:00</div> <script> const canvas = document.getElementById('gameCanvas'); const ctx = canvas.getContext('2d'); const balloonColors = ['#ff0000', '#00ff00', '#0000ff']; // Add more colors as needed const balloonRadius = 25; const balloonSpeed = 2; let score = 0; let missedBalloons = 0; let timer = 120; // 2 minutes in seconds function generateBalloon() { const x = Math.random() * (canvas.width - 2 * balloonRadius) + balloonRadius; const color = balloonColors[Math.floor(Math.random() * balloonColors.length)]; return { x, y: canvas.height, color }; } let balloons = []; function update() { // Update timer timer--; const minutes = Math.floor(timer / 60); const seconds = timer % 60; document.getElementById('timer').innerText = `Time: ${String(minutes).padStart(2, '0')}:${String(seconds).padStart(2, '0')}`; // Update balloons balloons = balloons.filter(balloon => balloon.y > 0); balloons.forEach(balloon => { balloon.y -= balloonSpeed; if (balloon.y < 0) { missedBalloons++; } }); // Draw everything ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.fillStyle = 'black'; ctx.fillRect(0, 0, canvas.width, canvas.height); balloons.forEach(balloon => { ctx.beginPath(); ctx.arc(balloon.x, balloon.y, balloonRadius, 0, Math.PI * 2); ctx.fillStyle = balloon.color; ctx.fill(); ctx.closePath(); }); document.getElementById('score').innerText = `Balloons Popped: ${score} | Balloons Missed: ${missedBalloons}`; // Check for collisions balloons.forEach((balloon, index) => { const distance = Math.sqrt((balloon.x - canvas.width / 2)**2 + (balloon.y - canvas.height / 2)**2); if (distance < balloonRadius) { balloons.splice(index, 1); score += 2; } }); if (timer <= 0) { endGame(); } else { requestAnimationFrame(update); } } function endGame() { alert(`Game Over!\nFinal Score: ${score}`); location.reload(); // Reload the page for simplicity } canvas.addEventListener('click', (event) => { const mouseX = event.clientX - canvas.getBoundingClientRect().left; const mouseY = event.clientY - canvas.getBoundingClientRect().top; balloons.forEach((balloon, index) => { const distance = Math.sqrt((mouseX - balloon.x)**2 + (mouseY - balloon.y)**2); if (distance < balloonRadius) { balloons.splice(index, 1); score += 2; } }); }); function startGame() { balloons = []; score = 0; missedBalloons = 0; timer = 120; for (let i = 0; i < 10; i++) { // Adjust the number of initial balloons as needed balloons.push(generateBalloon()); } update(); } // Initial setup canvas.width = window.innerWidth; canvas.height = window.innerHeight; // Start the game when the page loads startGame(); </script> </body> </html>
Write, Run & Share HTML code online using OneCompiler's HTML online Code editor for free. It's one of the robust, feature-rich online Code editor for HTML language, running on the latest version HTML5. Getting started with the OneCompiler's HTML compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as HTML
. You can also specify the stylesheet information in styles.css
tab and scripts information in scripts.js
tab and start coding.
HTML(Hyper Text Markup language) is the standard markup language for Web pages, was created by Berners-Lee in the year 1991. Almost every web page over internet might be using HTML.
<!DOCTYPE html>
<html>
and ends with </html>
<h1>
to <h6>
where <h1>
is the highest important heading and <h6>
is the least important sub-heading.<p>..</p>
tag.<a>
tag.
<a href="https://onecompiler.com/html">HTML online compiler</a>
<img>
tag, where src
attribute consists of image name.<button>..</button>
tag<ul>
for unordered/bullet list and <ol>
for ordered/number list, and the list items are defined in <li>
.<a href="https://onecompiler.com/html">HTML online compiler</a>
CSS(cascading style sheets) describes how HTML elements will look on the web page like color, font-style, font-size, background color etc.
Below is a sample style sheet which displays heading in green and in Candara font with padding space of 25px.
body{
padding: 25px;
}
.title {
color: #228B22;
font-family: Candara;
}
<table>
tag.<tr>
tag<th>
tag<td>
tag<caption>
tag<script>
is the tag used to write scripts in HTML<script src="script.js"></script>