-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
183 lines (130 loc) · 4.62 KB
/
script.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
const startNewGame = document.querySelector('#start-button');
const holes = document.querySelectorAll('.holes');
const gameBoard = document.querySelector('.game-board');
const pauseButton = document.querySelector('#pause-button');
const gamePlayMusic = new Audio('./Assets/gameMusic.mp3') ;
const smashMusic = new Audio('./Assets/smash.mp3')
const score = document.querySelector('#currentScore');
const scoreDiv = document.querySelector('.score-timer')
const timer = document.querySelector('#curentTime');
const popUpMessage = document.querySelector('.pop-up-message');
const scoreMessage = document.querySelector('#score-message');
startNewGame.addEventListener('click', () => {
startNewGameFunctionality();
});
pauseButton.addEventListener('click', pauseFunction);
// Adding start new game functionality
function startNewGameFunctionality() {
if (pauseButton.innerText === 'Resume') {
pauseButton.innerText = 'Pause';
clearInterval(randomMoleInterval);
randomMoleInterval = null;
holes.forEach((hole) => {
hole.innerHTML = '';
})
}
gamePlayMusic.volume=0.2;
gamePlayMusic.play();
score.innerText = '0';
timer.innerText = '60'
pauseButton.style.display = 'block';
scoreDiv.style.display = 'flex';
startNewGame.setAttribute('disabled', '');
startNewGame.classList.add('disabled-cursor');
hitingPosition();
callingRandomMoleInterval();
timerInterval();
}
let randomMoleInterval = null;
let lastHolePosition = null;
let randomNumber = null;
let timeInterval = null;
// Generating mole in random holes;
function randomMole() {
if (parseInt(timer.innerText) > 0) {
holes.forEach((hole) => {
hole.innerHTML = '';
})
randomNumber = Math.floor(Math.random() * 8);
if (lastHolePosition === null || lastHolePosition !== randomNumber) {
imageTag = document.createElement('img');
imageTag.src = './Assets/mole.png';
imageTag.classList.add('mole');
holes[randomNumber].appendChild(imageTag);
} else {
randomMole();
}
lastHolePosition = randomNumber;
} else {
clearInterval(randomMoleInterval)
clearInterval(timeInterval);
messagePopUp();
}
}
// Continusly call the randomMole Function
function callingRandomMoleInterval() {
randomMoleInterval = setInterval(randomMole, 800);
}
// Adding pause and resume functionality
function pauseFunction() {
if (pauseButton.innerText === 'Pause') {
pauseButton.innerText = 'Resume';
gamePlayMusic.pause();
clearInterval(randomMoleInterval);
randomMoleInterval = null;
clearInterval(timeInterval)
timeInterval = null;
startNewGame.removeAttribute('disabled', '');
startNewGame.classList.remove('disabled-cursor');
} else if (pauseButton.innerText === 'Resume') {
pauseButton.innerText = 'Pause';
randomMoleInterval = null;
gamePlayMusic.play();
randomMole();
startNewGame.setAttribute('disabled', '');
startNewGame.classList.add('disabled-cursor');
timerInterval();
callingRandomMoleInterval();
}
}
// Adding hit functionality
function hitingPosition() {
holes.forEach((hole) => {
hole.addEventListener('mousedown', () => {
if (parseInt(hole.id) === randomNumber && pauseButton.innerText === 'Pause') {
smashMusic.play();
hole.children[0].src = './Assets/mole-whacked.png'
score.innerText = parseInt(score.innerText) + 10;
}
})
})
}
// Adding timer function
function timerFunctionality() {
timer.innerText = parseInt(timer.innerText) - 1;
}
// timerinterval;
function timerInterval() {
timeInterval = setInterval(timerFunctionality, 1000);
}
// message pop up functionality
function messagePopUp() {
popUpMessage.style.display = 'flex';
gamePlayMusic.pause()
scoreMessage.innerText = score.innerText;
document.querySelector('.back-to-home').addEventListener('click', () => {
pauseButton.style.display = 'none';
scoreDiv.style.display = 'none'
let randomMoleInterval = null;
let lastHolePosition = null;
startNewGame.removeAttribute('disabled', '');
startNewGame.classList.remove('disabled-cursor');
let randomNumber = null;
let timeInterval = null;
popUpMessage.style.display = 'none';
holes.forEach((hole) => {
hole.innerHTML = '';
})
});
}
// custom hammer functionality