-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgame.html
274 lines (274 loc) · 9.13 KB
/
game.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Click the Ball Game</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400&family=Merriweather:wght@700&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Roboto', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f9f9f9;
color: #333;
}
#title {
font-size: 36px;
font-weight: bold;
margin-top: 20px;
color: #5b8db8;
text-align: center;
font-family: 'Merriweather', serif;
}
#gameContainer {
position: relative;
width: 90%;
max-width: 1000px;
height: 600px;
background-color: #ffffff;
border: 2px solid #333;
margin-top: 20px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
border-radius: 10px;
}
.ball {
position: absolute;
border-radius: 50%;
cursor: pointer;
transition: transform 0.2s ease;
}
.ball:hover {
transform: scale(1.2);
}
#controls {
margin-top: 20px;
display: flex;
justify-content: space-around;
align-items: center;
width: 90%;
max-width: 1000px;
flex-wrap: wrap;
}
#startButton, #stopButton {
padding: 10px 20px;
font-size: 18px;
cursor: pointer;
background-color: #5b8db8;
color: #fff;
border: none;
border-radius: 5px;
transition: background-color 0.3s ease;
margin: 10px;
}
#startButton:hover, #stopButton:hover {
background-color: #3c6e91;
}
#sizeInput, #colorInput {
margin-left: 10px;
padding: 5px;
border: 1px solid #ced4da;
border-radius: 5px;
font-size: 16px;
margin-top: 10px;
}
#stats {
display: flex;
justify-content: space-around;
margin-top: 20px;
width: 90%;
max-width: 1000px;
font-size: 20px;
background-color: #eef3f6;
padding: 10px 0;
border-radius: 10px;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
flex-wrap: wrap;
}
#stats div {
text-align: center;
flex: 1;
margin: 5px 0;
}
#highScore {
margin-top: 20px;
font-size: 24px;
color: #5b8db8;
font-weight: bold;
}
#footer {
margin-top: 40px;
font-size: 16px;
color: #555;
text-align: center;
width: 90%;
}
#footer a {
color: #5b8db8;
text-decoration: none;
}
#footer a:hover {
color: #3c6e91;
text-decoration: underline;
}
@media (max-width: 768px) {
#title {
font-size: 28px;
}
#gameContainer {
height: 600px;
}
#controls {
flex-direction: column;
align-items: center;
}
#startButton, #stopButton {
width: 100%;
}
#sizeInput, #colorInput {
margin-left: 0;
margin-top: 10px;
width: 100%;
text-align: center;
}
#stats {
flex-direction: column;
}
#highScore {
font-size: 20px;
}
}
</style>
</head>
<body>
<div id="title">🎯 Click the Ball Game</div>
<div id="controls">
<div>
<label for="sizeInput">⚙️ Ball Size (10-100px):</label>
<input type="number" id="sizeInput" min="10" max="100" value="30">
</div>
<div>
<label for="colorInput">🎨 Ball Color:</label>
<input type="color" id="colorInput" value="#ff6347">
</div>
<button id="startButton">🚀 Start Game</button>
<button id="stopButton" style="display: none;">🛑 Stop Game</button>
</div>
<div id="stats">
<div>⏳ Time Left: <span id="timer">30</span> seconds</div>
<div>🏆 Score: <span id="score">0</span></div>
<div>🎯 Accuracy: <span id="accuracy">0</span>%</div>
</div>
<div id="gameContainer"></div>
<div id="highScore">🥇 High Score: <span id="highScoreValue">0</span></div>
<div id="footer">
Game repository: <a href="https://github.com/liaoyanqing666/liaoyanqing666.github.io" target="_blank">https://github.com/liaoyanqing666/liaoyanqing666.github.io</a><br>
Game author: <a href="https://siyuanli.tech" target="_blank">Siyuan Li</a>
</div>
<script>
const gameContainer = document.getElementById("gameContainer");
const scoreDisplay = document.getElementById("score");
const timerDisplay = document.getElementById("timer");
const accuracyDisplay = document.getElementById("accuracy");
const highScoreDisplay = document.getElementById("highScoreValue");
const startButton = document.getElementById("startButton");
const stopButton = document.getElementById("stopButton");
const sizeInput = document.getElementById("sizeInput");
const colorInput = document.getElementById("colorInput");
let score = 0;
let totalClicks = 0;
let timeLeft = 30;
let isGameStarted = false;
let gameInterval;
let timerInterval;
let ballSize = 30;
let ballColor = "#ff6347";
let highScore = 0;
function createBall() {
const ball = document.createElement("div");
ball.classList.add("ball");
const maxX = gameContainer.clientWidth - ballSize;
const maxY = gameContainer.clientHeight - ballSize;
const randomX = Math.floor(Math.random() * maxX);
const randomY = Math.floor(Math.random() * maxY);
ball.style.width = ballSize + "px";
ball.style.height = ballSize + "px";
ball.style.backgroundColor = ballColor;
ball.style.left = randomX + "px";
ball.style.top = randomY + "px";
ball.addEventListener("click", () => {
ball.remove();
score++;
totalClicks++;
updateScore();
updateAccuracy();
createBall();
});
gameContainer.appendChild(ball);
}
function updateScore() {
scoreDisplay.textContent = score;
}
function updateAccuracy() {
let accuracy = totalClicks === 0 ? 0 : Math.floor((score / totalClicks) * 100);
accuracyDisplay.textContent = accuracy;
}
function updateTimer() {
timeLeft--;
timerDisplay.textContent = timeLeft;
if (timeLeft <= 0) {
clearInterval(timerInterval);
clearInterval(gameInterval);
endGame();
}
}
startButton.addEventListener("click", () => {
if (!isGameStarted) {
isGameStarted = true;
score = 0;
totalClicks = 0;
timeLeft = 30;
ballSize = parseInt(sizeInput.value);
ballColor = colorInput.value;
updateScore();
updateAccuracy();
timerDisplay.textContent = timeLeft;
startButton.style.display = "none";
stopButton.style.display = "block";
createBall();
timerInterval = setInterval(updateTimer, 1000);
}
});
stopButton.addEventListener("click", () => {
if (isGameStarted) {
endGame();
}
});
gameContainer.addEventListener("click", (event) => {
if (isGameStarted && !event.target.classList.contains("ball")) {
totalClicks++;
updateAccuracy();
}
});
function endGame() {
gameContainer.innerHTML = "";
clearInterval(timerInterval);
clearInterval(gameInterval);
isGameStarted = false;
startButton.style.display = "block";
stopButton.style.display = "none";
if (score > highScore) {
highScore = score;
highScoreDisplay.textContent = highScore;
alert("🎉 New High Score! Your score is: " + score);
} else {
alert("Game over! Your score is: " + score);
}
}
</script>
</body>
</html>