-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
81 lines (64 loc) · 1.95 KB
/
Copy pathscript.js
File metadata and controls
81 lines (64 loc) · 1.95 KB
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
let timeLeft = 30;
let score = 0;
let timerInterval;
let moveInterval;
let circle;
const timerDisplay = document.getElementById('timer');
const game = document.getElementById('game');
const startBtn = document.getElementById('startBtn');
const scoreDisplay = document.getElementById('score');
const finalMsg = document.getElementById('finalMessage');
function createCircle() {
if (circle && game.contains(circle)) {
game.removeChild(circle);
}
circle = document.createElement('div');
circle.style.width = '50px';
circle.style.height = '50px';
circle.style.background = 'red';
circle.style.borderRadius = '50%';
circle.style.position = 'absolute';
circle.style.cursor = 'pointer';
circle.style.left = Math.floor(Math.random() * (game.clientWidth - 50)) + 'px';
circle.style.top = Math.floor(Math.random() * (game.clientHeight - 50)) + 'px';
circle.onclick = () => {
score++;
scoreDisplay.textContent = 'Score: ' + score;
createCircle();
};
game.appendChild(circle);
}
function startGame() {
timeLeft = 30;
score = 0;
scoreDisplay.textContent = 'Score: 0';
timerDisplay.textContent = 'Time: 30';
finalMsg.style.display = 'none';
startBtn.disabled = true;
if (circle && game.contains(circle)) {
game.removeChild(circle);
}
createCircle();
clearInterval(timerInterval);
clearInterval(moveInterval);
timerInterval = setInterval(() => {
timeLeft--;
timerDisplay.textContent = 'Time: ' + timeLeft;
if (timeLeft <= 0) {
clearInterval(timerInterval);
clearInterval(moveInterval);
if (circle && game.contains(circle)) {
game.removeChild(circle);
}
finalMsg.textContent = 'Time is up! Your final score is ' + score;
finalMsg.style.display = 'block';
startBtn.disabled = false;
}
}, 1000);
moveInterval = setInterval(createCircle, 2000);
}
startBtn.onclick = () => {
finalMsg.style.display = 'none';
startBtn.disabled = true;
startGame();
};