-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
85 lines (70 loc) · 2.05 KB
/
game.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
74
75
76
77
78
79
80
81
82
83
84
85
let score = 0;
let gameActive = false;
let pointTimeout = [];
let gameInterval;
let clem = 0;
function getIntervalTime() {
const baseInterval = 1000;
const minimumInterval = 500;
const intervalReduction = 30;
console.log(
Math.max(minimumInterval, baseInterval - score * intervalReduction)
);
return Math.max(minimumInterval, baseInterval - score * intervalReduction);
}
function startGame() {
if (gameActive) return;
gameActive = true;
score = 0;
updateScore();
gameInterval = setInterval(placeRedDot, getIntervalTime());
}
function placeRedDot() {
clem++;
const redDot = document.createElement("div");
redDot.id = `target-${clem}`;
redDot.className = "red-dot";
const maxX = window.innerWidth - 200;
const maxY = window.innerHeight - 200;
const randomX = Math.floor(Math.random() * maxX);
const randomY = Math.floor(Math.random() * maxY);
redDot.style.left = randomX + "px";
redDot.style.top = randomY + "px";
document
.getElementsByTagName("body")[0]
.appendChild(redDot)
.addEventListener("click", dotClicked);
pointTimeout = [
...pointTimeout,
[
setTimeout(() => {
endGame();
}, 2000),
setTimeout(() => {
document.getElementById(redDot.id).style.filter = "blur(5px)";
}, 1000),
],
];
}
function dotClicked(e) {
if (!gameActive) return;
const clickedDotIndex = e.srcElement.id.split("target-")[1] - 1;
clearTimeout(pointTimeout[clickedDotIndex][0]);
clearTimeout(pointTimeout[clickedDotIndex][1]);
score++;
updateScore();
clearInterval(gameInterval);
gameInterval = setInterval(placeRedDot, getIntervalTime());
document.getElementById(e.srcElement.id).remove();
}
function updateScore() {
const scoreDisplay = document.getElementById("score");
scoreDisplay.textContent = "Score: " + score;
}
function endGame() {
gameActive = false;
pointTimeout.forEach((timeout) => clearTimeout(timeout[0]));
document.querySelectorAll(".red-dot").forEach((e) => e.remove());
clearInterval(gameInterval);
alert("Game Over! Your score was: " + score);
}