我创建了一个游戏,它将在屏幕上显示得分最高的获胜者。一号和二号玩家有两个按钮。用户将按照设定的分数玩游戏。例如,如果游戏设置为 5,用户将同时点击两个按钮,直到按钮一或按钮二达到 5 的获胜分数。标题可以显示“祝贺玩家 1 以 5 比 4 获胜”。玩家应该不能一直点击一个按钮来达到 5 的获胜分数。但是,我如何跟踪玩家在游戏中的轮次?下面是代码:
score.html file:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>score</title>
<link rel="stylesheet" href="score.css">
</head>
<body>
<h1> <span id="p1Display" > <span id="player1Win"></span> 0</span> to <span id="p2Display">0</span></h1>
<p>Play to: <span>5</span> </p>
<input type="number">
<button id="p1">Player one</button>
<button id="p2">Player two</button>
<button id="reset">Reset</button>
<script src="score.js"></script>
</body>
</html>
score.js file:
p1Button.addEventListener("click", function () {
if (!gameOver) {
if(turnTacker == 0) {
turnTacker = 1;
p1Score++;
}else {
turnTacker = 0;
p2Score++;
}
if (p1Score === winningscore) {
p1Display.classList.add("winner");
gameOver = true; //stop adding to score
}
p1Display.textContent = p1Score;
if (gameOver ) {
p1Display.textContent = `congratulations Player 1 win score of ${p1Score}`;
}
}
});
p2Button.addEventListener("click", function () {
if (!gameOver) {
if(turnTacker == 0) {
turnTacker = 1;
p2Score++;
}else {
turnTacker = 0;
p1Score++;
}
if (p2Score === winningscore) {
p2Display.classList.add("winner");
gameOver = true; //stop adding to score
}
p2Display.textContent = p2Score;
if (gameOver ) {
p2Display.textContent = `congratulations Player 2 win score of ${p2Score}`;
}
}
});
MM们
相关分类