<!DOCTYPE html> <html> <head> <title>Shooter Game</title> <style> #game-canvas { border: 1px solid #000; } </style> </head> <body> <canvas id="game-canvas" width="800" height="400"></canvas> <script> // Get the canvas element const canvas = document.getElementById("game-canvas"); const context = canvas.getContext("2d"); // Game constants const PLAYER_WIDTH = 50; const PLAYER_HEIGHT = 50; const BULLET_WIDTH = 10; const BULLET_HEIGHT = 10; const ENEMY_WIDTH = 30; const ENEMY_HEIGHT = 30; const ENEMY_SPEED = 2; const BULLET_SPEED = 5; // Game objects let player = { x: 0, y: canvas.height / 2 - PLAYER_HEIGHT / 2, width: PLAYER_WIDTH, height: PLAYER_HEIGHT, color: "#00f" }; let bullets = []; let enemies = []; let score = 0; // Handle key press events document.addEventListener("keydown", function (event) { if (event.key === " ") { event.preventDefault(); bullets.push({ x: player.x + player.width, y: player.y + player.height / 2 - BULLET_HEIGHT / 2, width: BULLET_WIDTH, height: BULLET_HEIGHT, color: "#f00" }); } }); // Update game logic function update() { // Update player position if (player.y > 0 && player.y < canvas.height - player.height) { if (keys[38]) { // Up arrow player.y -= 3; } if (keys[40]) { // Down arrow player.y += 3; } } // Update bullets for (let i = 0; i < bullets.length; i++) { bullets[i].x += BULLET_SPEED; // Remove bullet if it goes off-screen if (bullets[i].x > canvas.width) { bullets.splice(i, 1); i--; } } // Update enemies for (let i = 0; i < enemies.length; i++) { enemies[i].x -= ENEMY_SPEED; // Check for collision with player if (collide(player, enemies[i])) { gameOver(); return; } // Check for collision with bullets for (let j = 0; j < bullets.length; j++) { if (collide(bullets[j], enemies[i])) { bullets.splice(j, 1); j--; enemies.splice(i, 1); i--; score++; break; } } // Remove enemy if it goes off-screen if (enemies[i] && enemies[i].x + enemies[i].width < 0) { enemies.splice(i, 1); i--; } } // Add new enemies if (Math.random() < 0.01) { enemies.push({ x: canvas.width, y: Math.random() * canvas.height, width: ENEMY_WIDTH, height: ENEMY_HEIGHT, color: "#" + ((1 << 24) * Math.random() | 0).toString(16) }); } } // Render game state function render() { // Clear canvas context.clearRect(0, 0, canvas.width, canvas.height); // Draw player context.fillStyle = player.color; context.fillRect(player.x, player.y, player.width, player.height); // Draw bullets context.fillStyle = "#f00"; for (let i = 0; i < bullets.length; i++) { context.fillRect(bullets[i].x, bullets[i].y, bullets[i].width, bullets[i].height); } // Draw enemies for (let i = 0; i < enemies.length; i++) { context.fillStyle = enemies[i].color; context.fillRect(enemies[i].x, enemies[i].y, enemies[i].width, enemies[i].height); } // Draw score context.fillStyle = "#000"; context.font = "20px Arial"; context.fillText("Score: " + score, 10, 25); } // Game loop function loop() { update(); render(); requestAnimationFrame(loop); } // Game over function gameOver() { alert("Game Over! Your score was " + score); score = 0; enemies = []; player.y = canvas.height / 2 - PLAYER_HEIGHT / 2; } // Check if two objects collide function collide(obj1, obj2) { return obj1.x < obj2.x + obj2.width && obj1.x + obj1.width > obj2.x && obj1.y < obj2.y + obj2.height && obj1.y + obj1.height > obj2.y; } // Track pressed keys let keys = {}; document.addEventListener("keydown", function (event) { keys[event.keyCode] = true; }); document.addEventListener("keyup", function (event) { delete keys[event.keyCode]; }); // Start the game loop loop(); </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>