-
Notifications
You must be signed in to change notification settings - Fork 1
/
Red_square.html
106 lines (93 loc) · 3.73 KB
/
Red_square.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>点击红色方块</title>
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css" rel="stylesheet">
<style>
body {
text-align: center;
font-family: Arial, sans-serif;
}
#target {
width: 100px;
height: 100px;
background-color: red;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
cursor: pointer;
}
</style>
</head>
<body>
<h1>用最短的时间点击红色方块100次</h1>
<div id="target"></div>
<p id="score">得分: 0</p>
<p id="time">时间: 0秒>
<script>
let score = 0;
let timeElapsed = 0;
let timerInterval;
let gameStartTime;
function generateRandomPosition() {
const target = document.getElementById("target");
const maxX = window.innerWidth - target.clientWidth;
const maxY = window.innerHeight - target.clientHeight;
const randomX = Math.random() * maxX;
const randomY = Math.random() * maxY;
target.style.left = randomX + "px";
target.style.top = randomY + "px";
}
function increaseScore() {
score++;
document.getElementById("score").textContent = `得分: ${score}`;
if (score === 100) {
clearInterval(timerInterval); // 停止计时器
const endTime = new Date();
const timeDifference = (endTime - gameStartTime) / 1000;
const modal = document.createElement("div");
modal.className = "fixed inset-0 flex items-center justify-center z-50";
modal.innerHTML = `
<div class="bg-white rounded-lg p-8 shadow-lg">
<p class="text-lg font-semibold mb-4">恭喜!您成功点击了100次红色方块!</p>
<p class="mb-4">用时:${timeDifference.toFixed(2)}秒</p>
<button id="resetButton" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-full focus:outline-none focus:shadow-outline-blue active:bg-blue-800">
确认
</button>
</div>
`;
document.body.appendChild(modal);
const resetButton = document.getElementById("resetButton");
resetButton.addEventListener("click", () => {
score = 0;
timeElapsed = 0;
document.getElementById("score").textContent = `得分: ${score}`;
document.getElementById("time").textContent = `时间: ${timeElapsed}秒`;
generateRandomPosition();
timerInterval = setInterval(updateTime, 1000);
gameStartTime = new Date();
modal.remove();
});
}
}
function updateTime() {
timeElapsed++;
document.getElementById("time").textContent = `时间: ${timeElapsed}秒`;
}
const target = document.getElementById("target");
target.addEventListener("click", () => {
if (score === 0) {
gameStartTime = new Date(); // 记录游戏开始时间
}
increaseScore();
generateRandomPosition();
});
generateRandomPosition();
// 开始计时器
timerInterval = setInterval(updateTime, 1000);
</script>
</body>
</html>