Create Your Own HTML Platformer Game: A Beginner's Guide
<!DOCTYPE html>
<html>
<head>
<title>Platformer Game</title>
<style>
/* CSS for the platformer game goes here */
#game-container {
width: 500px;
height: 500px;
margin: 0 auto;
background-color: #ccc;
position: relative;
}
#player {
width: 50px;
height: 50px;
position: absolute;
bottom: 0;
left: 0;
background-color: #000;
}
#platform {
width: 500px;
height: 10px;
background-color: #333;
position: absolute;
bottom: 0;
left: 0;
}
</style>
</head>
<body>
<div id='game-container'>
<div id='player'></div>
<div id='platform'></div>
</div>
<script>
// JavaScript for the platformer game goes here
var player = document.getElementById('player');
var platform = document.getElementById('platform');
<pre><code> window.addEventListener('keydown', movePlayer);
function movePlayer(e) {
if (e.keyCode == 37) {
player.style.left = player.offsetLeft - 5 + 'px';
} else if (e.keyCode == 39) {
player.style.left = player.offsetLeft + 5 + 'px';
}
if (player.offsetLeft + player.offsetWidth > platform.offsetLeft
&& player.offsetLeft < platform.offsetLeft + platform.offsetWidth
&& player.offsetTop + player.offsetHeight >= platform.offsetTop) {
player.style.top = platform.offsetTop - player.offsetHeight + 'px';
}
}
</script>
</body>
</code></pre>
</html>
原文地址: https://www.cveoy.top/t/topic/lnRN 著作权归作者所有。请勿转载和采集!