-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (58 loc) · 1.66 KB
/
app.js
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
const squares = document.querySelectorAll(".square");
const mole = document.querySelector(".mole");
const timeLeft = document.querySelector("#time-left");
const score = document.querySelector("#score");
let result = 0;
let hitPosition = [];
let currentTime = 60;
let timerId = null;
function randomSquare() {
squares.forEach((square) => {
square.classList.remove("mole");
});
let randomSquare = squares[Math.floor(Math.random() * 12)];
randomSquare.classList.add("mole");
hitPosition.push(randomSquare.id);
}
squares.forEach((square) => {
square.addEventListener("mousedown", () => {
if (hitPosition.includes(square.id)) {
result++;
score.textContent = result;
hitPosition = [];
}
});
});
function moveMole() {
timerId = setInterval(randomSquare, 500);
}
moveMole();
function countDown() {
currentTime--;
timeLeft.textContent = currentTime;
if (currentTime == 0) {
clearInterval(countDownTimerId);
clearInterval(timerId);
alert("GAME OVER! Your final score is " + result);
window.location = "./index.html";
}
}
let countDownTimerId = setInterval(countDown, 1000);
// Hammer
const cursor = document.querySelector("#hammer");
const whack = document.querySelector("#whack");
const soundPlayer = new Audio("./whack.mp3");
function followCursor(e) {
cursor.style.top = e.clientY + window.scrollY + "px";
cursor.style.left = e.clientX + "px";
}
function snap() {
cursor.classList.add("snap");
soundPlayer.play();
setTimeout(() => {
cursor.classList.remove("snap");
soundPlayer.currentTime = 0;
}, 100);
}
window.addEventListener("mousemove", followCursor);
window.addEventListener("click", snap);