Simple HTML Platformer Game: Build Your Own 2D Jumper
<!DOCTYPE html>
<html>
<head>
<title>Simple HTML Platformer Game</title>
<style type="text/css">
body {
margin: 0;
padding: 0;
}
<pre><code> #game-container {
width: 500px;
height: 400px;
position: relative;
border: 2px solid #000000;
}
#player {
width: 50px;
height: 50px;
position: absolute;
background-color: #00FF00;
left: 0;
top: 0;
}
#floor {
width: 500px;
height: 50px;
position: absolute;
background-color: #FF0000;
bottom: 0;
}
</style>
</code></pre>
</head>
<body>
<div id="game-container">
<div id="player"></div>
<div id="floor"></div>
</div>
<script type="text/javascript">
// Move the player left and right
document.onkeydown = checkKey;
function checkKey(e) {
e = e || window.event;
if (e.keyCode == '37') {
// left arrow
document.getElementById("player").style.left = document.getElementById("player").offsetLeft - 10 + 'px';
}
else if (e.keyCode == '39') {
// right arrow
document.getElementById("player").style.left = document.getElementById("player").offsetLeft + 10 + 'px';
}
}
</script>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lmmq 著作权归作者所有。请勿转载和采集!