写出一款超级马里奥的网页游戏的代码要求有游戏开始和结束功能人物角色用数字代替
<!DOCTYPE html>
<html>
<head>
<title>超级马里奥</title>
<style>
#game-board {
width: 300px;
height: 200px;
border: 1px solid black;
}
.player {
width: 20px;
height: 20px;
background-color: red;
position: absolute;
}
#start-button, #end-button {
padding: 10px;
background-color: green;
color: white;
font-weight: bold;
cursor: pointer;
}
</style>
</head>
<body>
<div id="game-board">
<div id="player" class="player"></div>
</div>
<br>
<button id="start-button">开始游戏</button>
<button id="end-button">结束游戏</button>
<pre><code><script>
let player = document.getElementById("player");
let gameBoard = document.getElementById("game-board");
let startButton = document.getElementById("start-button");
let endButton = document.getElementById("end-button");
let gameInterval;
function startGame() {
let playerPosition = 0;
player.style.left = playerPosition + "px";
gameInterval = setInterval(function() {
playerPosition += 10;
player.style.left = playerPosition + "px";
if(playerPosition >= gameBoard.offsetWidth - player.offsetWidth) {
clearInterval(gameInterval);
alert("游戏结束");
}
}, 100);
}
function endGame() {
clearInterval(gameInterval);
player.style.left = "0px";
}
startButton.addEventListener("click", startGame);
endButton.addEventListener("click", endGame);
</script>
</code></pre>
</body>
</html>
原文地址: https://www.cveoy.top/t/topic/biw5 著作权归作者所有。请勿转载和采集!