<!DOCTYPE html>
<html>
	<head>
		<title>Platformer Game</title>
		<style>
			/*Styles for the game*/
			body {
				background-color: black;
				margin: 0;
				padding: 0;
			}
<pre><code>		#game-container {
			margin: 0 auto;
			width: 800px;
			height: 600px;
			background-color: white;
			position: relative;
		}
		
		#player {
			width: 20px;
			height: 20px;
			position: absolute;
			background-color: blue;
			top: 0;
			left: 0;
		}
		
		#platforms {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
		}
		
		.platform {
			background-color: gray;
			width: 50px;
			height: 10px;
			position: absolute;
		}
	&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
	&lt;div id='game-container'&gt;
		&lt;div id='player'&gt;&lt;/div&gt;
		&lt;div id='platforms'&gt;
			&lt;div class='platform' style='top: 50px; left: 50px;'&gt;&lt;/div&gt;
			&lt;div class='platform' style='top: 100px; left: 200px;'&gt;&lt;/div&gt;
			&lt;div class='platform' style='top: 250px; left: 250px;'&gt;&lt;/div&gt;
			&lt;div class='platform' style='top: 350px; left: 400px;'&gt;&lt;/div&gt;
		&lt;/div&gt;
	&lt;/div&gt;
	&lt;script&gt;
		//Script to control the player
		let player = document.querySelector('#player');
		
		//Add event listeners for key presses
		document.addEventListener('keydown', function(e) {
			//Move left
			if (e.keyCode === 37) {
				player.style.left = (player.offsetLeft - 10) + 'px';
			}
			//Move right
			else if (e.keyCode === 39) {
				player.style.left = (player.offsetLeft + 10) + 'px';
			}
		});
	&lt;/script&gt;
&lt;/body&gt;
</code></pre>
</html>
HTML Platformer Game Code: Simple Example

原文地址: https://www.cveoy.top/t/topic/lnR1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录