<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Enhanced 2D Platformer Game</title> <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/phaser.min.js"></script> <style> body { margin: 0; overflow: hidden; } canvas { display: block; margin: 0 auto; } .hidden { display: none; } #gameOverText, #winText { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 48px; color: white; text-align: center; } #restartButton { position: absolute; top: 60%; left: 50%; transform: translate(-50%, -50%); font-size: 32px; color: white; background: rgba(0, 0, 0, 0.5); border: none; padding: 10px 20px; cursor: pointer; display: none; } </style> </head> <body> <div id="gameOverText" class="hidden">Game Over! Score: 0</div> <div id="winText" class="hidden">You Win! Final Score: 0</div> <button id="restartButton" class="hidden">Restart</button> <script> const config = { type: Phaser.AUTO, width: 800, height: 600, physics: { default: 'arcade', arcade: { gravity: { y: 300 }, debug: false } }, scene: { preload: preload, create: create, update: update } }; const game = new Phaser.Game(config); let player, platforms, cursors, collectibles, score = 0, scoreText, enemies, gameOver = false; function preload() { this.load.image('sky', 'https://labs.phaser.io/assets/skies/sky1.png'); this.load.image('ground', 'https://labs.phaser.io/assets/sprites/platform.png'); this.load.image('star', 'https://labs.phaser.io/assets/sprites/star.png'); this.load.image('enemy', 'https://labs.phaser.io/assets/sprites/baddie.png'); this.load.spritesheet('dude', 'https://labs.phaser.io/assets/sprites/dude.png', { frameWidth: 32, frameHeight: 48 }); } function create() { this.add.image(400, 300, 'sky'); platforms = this.physics.add.staticGroup(); platforms.create(400, 568, 'ground').setScale(2).refreshBody(); platforms.create(600, 400, 'ground'); platforms.create(50, 250, 'ground'); platforms.create(750, 220, 'ground'); player = this.physics.add.sprite(100, 450, 'dude'); player.setBounce(0.2); player.setCollideWorldBounds(true); this.physics.add.collider(player, platforms); cursors = this.input.keyboard.createCursorKeys(); collectibles = this.physics.add.group({ key: 'star', repeat: 11, setXY: { x: 12, y: 0, stepX: 70 } }); collectibles.children.iterate(function (child) { child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8)); child.setCollideWorldBounds(true); child.setGravityY(300); }); this.physics.add.collider(collectibles, platforms); this.physics.add.overlap(player, collectibles, collectStar, null, this); enemies = this.physics.add.group({ key: 'enemy', repeat: 1, setXY: { x: 400, y: 0, stepX: 200 } }); enemies.children.iterate(function (child) { child.setBounce(1); child.setVelocity(Phaser.Math.Between(-200, 200), 20); child.setCollideWorldBounds(true); child.setGravityY(300); }); this.physics.add.collider(enemies, platforms); this.physics.add.collider(player, enemies, hitEnemy, null, this); scoreText = this.add.text(16, 16, 'Score: 0', { fontSize: '32px', fill: '#fff' }); // Restart button event listener document.getElementById('restartButton').addEventListener('click', restartGame); } function update() { if (gameOver) return; if (cursors.left.isDown) { player.setVelocityX(-160); } else if (cursors.right.isDown) { player.setVelocityX(160); } else { player.setVelocityX(0); } if (cursors.up.isDown && player.body.touching.down) { player.setVelocityY(-330); } } function collectStar(player, star) { star.disableBody(true, true); score += 10; scoreText.setText('Score: ' + score); if (collectibles.countActive(true) === 0) { winGame(); } } function hitEnemy(player, enemy) { gameOver = true; this.physics.pause(); player.setTint(0xff0000); scoreText.setText('Game Over! Score: ' + score); showGameOverScreen(); } function winGame() { gameOver = true; this.physics.pause(); scoreText.setText('You Win! Final Score: ' + score); showWinScreen(); } function showGameOverScreen() { const gameOverText = document.getElementById('gameOverText'); gameOverText.innerText = 'Game Over! Score: ' + score; gameOverText.classList.remove('hidden'); showRestartButton(); } function showWinScreen() { const winText = document.getElementById('winText'); winText.innerText = 'You Win! Final Score: ' + score; winText.classList.remove('hidden'); showRestartButton(); } function showRestartButton() { const restartButton = document.getElementById('restartButton'); restartButton.style.display = 'block'; } function restartGame() { gameOver = false; score = 0; scoreText.setText('Score: 0'); document.getElementById('gameOverText').classList.add('hidden'); document.getElementById('winText').classList.add('hidden'); document.getElementById('restartButton').style.display = 'none'; // Reset player and collectibles player.setTint(0xffffff); player.setPosition(100, 450); collectibles.children.iterate(child => { child.enableBody(true, child.x, 0, true, true); }); enemies.children.iterate(child => { child.setPosition(400, 0); child.setVelocity(Phaser.Math.Between(-200, 200), 20); }); } </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>