-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
79 lines (69 loc) 路 2.04 KB
/
Copy pathscript.js
File metadata and controls
79 lines (69 loc) 路 2.04 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
var timer = 60;
var score = 0;
var hitrn = 0;
var highScore = localStorage.getItem("highScore") || 0;
document.querySelector("#scoreval").textContent = score;
document.querySelector("#restartBtn").style.display = "none";
// SOUND ELEMENTS
const hitSound = document.getElementById("hitSound");
const gameOverSound = document.getElementById("gameOverSound");
function makeBubble() {
var clutter = "";
for (var i = 1; i <= 168; i++) {
var rn = Math.floor(Math.random() * 10);
clutter += `<div class="bubble">${rn}</div>`;
}
document.querySelector("#pbtm").innerHTML = clutter;
}
function runTimer() {
var timerInterval = setInterval(function () {
if (timer > 0) {
timer--;
document.querySelector("#Timerval").textContent = timer;
} else {
clearInterval(timerInterval);
gameOverSound.play();
document.querySelector("#pbtm").innerHTML = `<h1>Game Over</h1>`;
document.querySelector("#restartBtn").style.display = "block";
// Save High Score
if (score > highScore) {
localStorage.setItem("highScore", score);
alert("馃帀 New High Score: " + score);
} else {
alert("馃挴 High Score: " + highScore);
}
}
}, 1000);
}
function getnewHit() {
hitrn = Math.floor(Math.random() * 10);
document.querySelector("#hitval").textContent = hitrn;
}
function increaseScore() {
score += 10;
document.querySelector("#scoreval").textContent = score;
}
document.querySelector("#pbtm").addEventListener("click", function (details) {
var clickednum = Number(details.target.textContent);
if (clickednum === hitrn) {
hitSound.play();
increaseScore();
makeBubble();
getnewHit();
}
});
// Restart Button Logic
document.querySelector("#restartBtn").addEventListener("click", function () {
timer = 60;
score = 0;
document.querySelector("#scoreval").textContent = score;
document.querySelector("#Timerval").textContent = timer;
this.style.display = "none";
makeBubble();
getnewHit();
runTimer();
});
// INITIAL CALLS
runTimer();
makeBubble();
getnewHit();