-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
136 lines (117 loc) · 4.01 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
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
/* sound */
const whackSound = new Audio('./sound/mixkit-metal-hammer-hit-833.wav');
whackSound.volume = 0.3;
const gameOverSound = new Audio('./sound/mixkit-funny-game-over-2878.wav');
gameOverSound.volume = 0.4;
const errorSound = new Audio('./sound/mixkit-game-show-buzz-in-3090.wav');
errorSound.volume = 0.4;
// sound toggle
const soundButon = document.getElementById('soundOff');
soundButon.addEventListener('click', () => {
let checkSound = soundButon.getAttribute('sound-off');
if(checkSound === 'false') {
soundButon.setAttribute('sound-off', true);
whackSound.volume = 0;
errorSound.volume = 0;
gameOverSound.volume = 0;
} else {
soundButon.setAttribute('sound-off', false);
whackSound.volume = 0.3;
errorSound.volume = 0.4;
gameOverSound.volume = 0.4;
}
});
const squares = document.querySelectorAll('.square');
const mole = document.getElementsByClassName('mole');
const score = document.getElementById('score');
const timeLeft = document.getElementById('time-left');
const gameBoard = document.querySelector('.grid');
const gameOver = document.querySelector('.game-over');
const resultGameOver = document.getElementById('result');
const saysMartin = document.getElementById('saysMartin');
const newGameButtonDesktop = document.getElementById('newGame-button-desktop');
const newGameButtonMobile = document.getElementById('newGame-button-mobile');
let lockboard = false;
function playWhackAMole() {
let result = 0;
let hitPosition;
var currentTime = 30;
var timerId = null;
let martinsLine;
if(lockboard === true) {
return;
}
score.innerText = result;
timeLeft.innerText = currentTime;
function selectRandom() {
squares.forEach(square => {
square.classList.remove('mole');
})
let randomSquare = squares[Math.floor(Math.random() * 9)];
randomSquare.classList.add('mole');
hitPosition = randomSquare.id;
}
squares.forEach(square => {
square.addEventListener('mousedown', () => {
if(square.id == hitPosition) {
whackSound.play();
result++;
score.innerText = result;
hitPosition = null;
}
})
})
function moveMole() {
timerId = setInterval(selectRandom, 550);
}
moveMole()
var countDownTimerId = setInterval(countDown, 1000);
function countDown() {
currentTime--;
timeLeft.innerText = currentTime;
lockboard = true;
if(currentTime == 0) {
clearInterval(countDownTimerId);
clearInterval(timerId);
gameBoard.setAttribute('hidden', '');
gameOver.removeAttribute('hidden');
resultGameOver.innerText = result;
martinSays(result);
saysMartin.innerText = martinsLine;
gameOverSound.play();
lockboard = false;
}
}
function martinSays(score) {
switch(true) {
case score === 0:
martinsLine = "Martin is not happy.\n Moles will make more hills!"
break;
case 0 < score && score < 6:
martinsLine = "Martin says, please,\n get more moles next time."
break;
case 6 <= score && score <= 9:
martinsLine = "Martin is happy\n and says thank you!"
break;
case score >= 10:
martinsLine = "Martin is excited!\n No more hills!"
break;
}
}
}
playWhackAMole();
newGameButtonDesktop.addEventListener('click', playNewGame);
function playNewGame() {
gameOver.setAttribute('hidden', '');
gameBoard.removeAttribute('hidden');
playWhackAMole();
}
newGameButtonMobile.addEventListener('click', playNewGame);
function playNewGame() {
if(lockboard === true) {
errorSound.play();
}
gameOver.setAttribute('hidden', '');
gameBoard.removeAttribute('hidden');
playWhackAMole();
}