-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhack.js
90 lines (81 loc) · 2.49 KB
/
whack.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
const images = Array.from(document.querySelectorAll('img'));
const moles = Array.from(document.querySelectorAll('.mole'));
const play = document.querySelector('.play');
const scoreBoard = document.querySelector('.scoreboard');
const scoreContainer = document.querySelector('.score-container');
const Empty = document.querySelector('.empty');
const Welcome = document.querySelector('.welcome');
const gameContainer = document.querySelector('.game-container');
const Score = document.querySelector('.score');
const Timer = document.querySelector('.timer p');
let lastMole;
let isTimeOver = false;
let scoreValue = 0;
images.forEach(item => item.draggable = false);
moles.forEach(item => item.addEventListener('click', addPoint));
play.addEventListener('click', gameStart);
function gameStart() {
Empty.style.transform = 'translate(100%, -100%)';
scoreBoard.style.transform = 'translate(0%, 0%)';
Welcome.style.transform = 'translate(-100%, 0)';
gameContainer.style.transform = 'translate(0%, -100%)';
scoreContainer.classList.add('custom')
setTimeout(() => {
Timer.innerText = 30;
Score.innerText = 0;
scoreValue = 0;
}, 500)
setTimeout(() => {
scoreContainer.classList.remove('custom');
startGame();
}, 1500);
}
function addPoint(e) {
scoreValue++;
Score.innerText = scoreValue;
e.target.classList.remove('rise');
}
function randomTime(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
function randomHole(param) {
const idx = Math.floor(Math.random() * param.length);
const mole = param[idx];
if (mole == lastMole) {
return randomHole(moles);
}
lastMole = mole;
return mole;
}
function look() {
const time = randomTime(600, 1000)
const mole = randomHole(moles);
mole.classList.add('rise');
setTimeout(() => {
mole.classList.remove('rise');
if (!isTimeOver) {
look();
}
}, time);
}
function startGame() {
let countDownTimer;
let remainingTime = 30;
isTimeOver = false;
countDownTimer = setInterval(() => {
if (isTimeOver) {
clearInterval(countDownTimer);
}
else {
remainingTime--;
Timer.innerText = remainingTime;
}
}, 1000)
setTimeout(() => {
isTimeOver = true;
Welcome.style.transform = 'translate(0%, 0%)';
gameContainer.style.transform = 'translate(100%, -100%)';
Timer.innerText = '';
}, 30000);
look();
}