Create Your Own Platformer Game with HTML: Simple Tutorial
<!DOCTYPE html>
<html>
<head>
<title>Platformer Game</title>
<style>
body {
background-color: #f3f3f3;
margin: 0;
}
#game {
position: relative;
width: 500px;
height: 500px;
margin: 0 auto;
background-image: url('background.png');
}
#player {
position: absolute;
width: 50px;
height: 50px;
left: 100px;
top: 100px;
background-image: url('player.png');
}
</style>
</head>
<body>
<div id='game'></div>
<div id='player'></div>
<script>
// Player movement
document.onkeydown = function(e) {
if (e.keyCode == 37) {
// Move left
document.getElementById('player').style.left = (document.getElementById('player').offsetLeft - 10) + 'px';
}
if (e.keyCode == 39) {
// Move right
document.getElementById('player').style.left = (document.getElementById('player').offsetLeft + 10) + 'px';
}
if (e.keyCode == 38) {
// Jump
document.getElementById('player').style.top = (document.getElementById('player').offsetTop - 10) + 'px';
}
if (e.keyCode == 40) {
// Duck
document.getElementById('player').style.top = (document.getElementById('player').offsetTop + 10) + 'px';
}
};
</script>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/lnRe 著作权归作者所有。请勿转载和采集!