HTML实现五子棋计时和记分功能的代码
时间: 2024-05-02 17:17:18 浏览: 115
以下是一个简单的 HTML 代码,可以实现五子棋计时和记分功能:
```html
<!DOCTYPE html>
<html>
<head>
<title>五子棋计时和记分</title>
<style>
table {
border-collapse: collapse;
margin: 20px;
width: 600px;
}
td, th {
border: 1px solid black;
padding: 10px;
text-align: center;
}
th {
background-color: #eee;
}
.timer {
font-size: 24px;
margin: 20px;
}
.score {
font-size: 24px;
margin: 20px;
}
</style>
</head>
<body>
<h1>五子棋计时和记分</h1>
<table>
<tr>
<th>玩家</th>
<th>得分</th>
</tr>
<tr>
<td>玩家1</td>
<td id="player1_score">0</td>
</tr>
<tr>
<td>玩家2</td>
<td id="player2_score">0</td>
</tr>
</table>
<div class="timer">
<span id="timer">00:00</span>
<button onclick="startTimer()">开始计时</button>
<button onclick="stopTimer()">停止计时</button>
<button onclick="resetTimer()">重置计时</button>
</div>
<script>
var timerInterval;
var startTime;
var elapsedSeconds = 0;
function startTimer() {
startTime = new Date();
timerInterval = setInterval(updateTimer, 1000);
}
function updateTimer() {
var now = new Date();
elapsedSeconds = Math.floor((now - startTime) / 1000);
var minutes = Math.floor(elapsedSeconds / 60);
var seconds = elapsedSeconds % 60;
document.getElementById("timer").innerHTML = pad(minutes) + ":" + pad(seconds);
}
function stopTimer() {
clearInterval(timerInterval);
}
function resetTimer() {
stopTimer();
elapsedSeconds = 0;
document.getElementById("timer").innerHTML = "00:00";
}
function pad(number) {
if (number < 10) {
return "0" + number;
} else {
return number;
}
}
function addScore(player) {
var scoreElement = document.getElementById(player+"_score");
var currentScore = parseInt(scoreElement.innerHTML);
scoreElement.innerHTML = currentScore + 1;
}
</script>
<div class="score">
<button onclick="addScore('player1')">玩家1得分</button>
<button onclick="addScore('player2')">玩家2得分</button>
</div>
</body>
</html>
```
这个 HTML 页面包含一个计时器和两个玩家的得分表格。计时器可以开始、停止和重置。得分按钮可以增加相应玩家的得分。
阅读全文