-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
55 lines (49 loc) · 1.55 KB
/
script.js
File metadata and controls
55 lines (49 loc) · 1.55 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
const box = document.getElementById("box");
const scoreText = document.getElementById("score");
const timerText = document.getElementById("timer");
const gameOverText = document.getElementById("gameover");
const startBtn = document.getElementById("startBtn");
const clickSound = document.getElementById("clickSound");
const bgMusic = document.getElementById("bgMusic");
let score = 0;
let timeLeft = 30;
let gameRunning = false;
let timer;
function moveBox() {
const x = Math.random() * (window.innerWidth - 60);
const y = Math.random() * (window.innerHeight - 60);
box.style.left = x + "px";
box.style.top = y + "px";
}
box.addEventListener("click", () => {
if (!gameRunning) return;
score++;
scoreText.textContent = "Score: " + score;
clickSound.currentTime = 0;
clickSound.play();
moveBox();
});
function updateTimer() {
timeLeft--;
timerText.textContent = "Time: " + timeLeft;
if (timeLeft <= 0) {
clearInterval(timer);
gameRunning = false;
box.style.display = "none";
gameOverText.style.display = "block";
bgMusic.pause();
}
}
startBtn.addEventListener("click", () => {
score = 0;
timeLeft = 30;
scoreText.textContent = "Score: 0";
timerText.textContent = "Time: 30";
gameOverText.style.display = "none";
startBtn.style.display = "none";
box.style.display = "block";
bgMusic.play();
gameRunning = true;
moveBox();
timer = setInterval(updateTimer, 1000);
});