|
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>反应力挑战</title>
<style>
* {
box-sizing: border-box;
}
body {
margin: 0;
min-height: 100vh;
font-family: Arial, "Microsoft YaHei", sans-serif;
background: #101820;
color: #fff;
display: flex;
align-items: center;
justify-content: center;
}
.game {
width: min(92vw, 520px);
padding: 20px;
}
.top {
display: flex;
justify-content: space-between;
gap: 12px;
margin-bottom: 14px;
font-size: 18px;
font-weight: 700;
}
.board {
position: relative;
width: 100%;
aspect-ratio: 1 / 1;
background: #1f2a33;
border: 2px solid #3f5668;
border-radius: 12px;
overflow: hidden;
touch-action: manipulation;
}
.target {
position: absolute;
width: 58px;
height: 58px;
border: none;
border-radius: 50%;
background: #ffcc33;
box-shadow: 0 0 18px rgba(255, 204, 51, 0.75);
cursor: pointer;
transform: translate(-50%, -50%);
transition: left 0.12s ease, top 0.12s ease, transform 0.08s ease;
}
.target:active {
transform: translate(-50%, -50%) scale(0.88);
}
.panel {
margin-top: 14px;
display: flex;
justify-content: space-between;
align-items: center;
gap: 12px;
}
.message {
min-height: 24px;
color: #d8e3ea;
}
.start {
border: 0;
border-radius: 8px;
padding: 10px 16px;
background: #00a884;
color: white;
font-size: 16px;
font-weight: 700;
cursor: pointer;
}
.start:hover {
background: #00bd95;
}
@media (max-width: 420px) {
.top {
font-size: 16px;
}
.target {
width: 50px;
height: 50px;
}
}
</style>
</head>
<body>
<main class="game">
<div class="top">
<div>分数:<span id="score">0</span></div>
<div>剩余:<span id="time">30</span>s</div>
</div>
<section class="board" id="board" aria-label="游戏区域">
<button class="target" id="target" aria-label="点击得分"></button>
</section>
<div class="panel">
<div class="message" id="message">点击开始,测试你的反应速度。</div>
<button class="start" id="start">开始游戏</button>
</div>
</main>
<script>
const board = document.querySelector("#board");
const target = document.querySelector("#target");
const scoreEl = document.querySelector("#score");
const timeEl = document.querySelector("#time");
const messageEl = document.querySelector("#message");
const startBtn = document.querySelector("#start");
let score = 0;
let timeLeft = 30;
let timer = null;
let playing = false;
function random(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
function moveTarget() {
const boardRect = board.getBoundingClientRect();
const targetSize = target.offsetWidth;
const padding = targetSize / 2 + 8;
const x = random(padding, boardRect.width - padding);
const y = random(padding, boardRect.height - padding);
target.style.left = `${x}px`;
target.style.top = `${y}px`;
}
function startGame() {
score = 0;
timeLeft = 30;
playing = true;
scoreEl.textContent = score;
timeEl.textContent = timeLeft;
messageEl.textContent = "快点击黄色圆点!";
startBtn.textContent = "重新开始";
moveTarget();
clearInterval(timer);
timer = setInterval(() => {
timeLeft -= 1;
timeEl.textContent = timeLeft;
if (timeLeft <= 0) {
endGame();
}
}, 1000);
}
function endGame() {
playing = false;
clearInterval(timer);
messageEl.textContent = `游戏结束,你的最终分数是 ${score} 分。`;
}
target.addEventListener("click", () => {
if (!playing) return;
score += 1;
scoreEl.textContent = score;
moveTarget();
});
startBtn.addEventListener("click", startGame);
window.addEventListener("resize", () => {
if (playing) moveTarget();
});
moveTarget();
</script>
</body>
</html>
|