-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
78 lines (53 loc) · 1.58 KB
/
main.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
const virus = document.querySelector('.virus');
const boxes = document.querySelectorAll('.box')
let display_score = document.querySelector('#user-score')
const time_left = document.querySelector('#time-left');
const start_btn = document.querySelector('.btn-start');
const restart_btn = document.querySelector('.btn-restart')
const body = document.querySelector('body');
let score = 0;
let currentTimeLeft = 60;
let virusPosition = 0;
start_btn.addEventListener('click',start);
restart_btn.addEventListener('click', reset);
function reset(){
setTimeout(window.location.reload(),10)
}
function start(){
function getRandomPosition(){
boxes.forEach( box =>{
box.classList.remove('virus');
})
let randomPosition = boxes[Math.floor(Math.random()*9)]
randomPosition.classList.add('virus')
virusPosition = randomPosition.id;
}
boxes.forEach( box =>{
box.addEventListener('mouseup', ()=>{
console.log('virus location :', virusPosition);
if(virusPosition === box.id){
score+=1;
display_score.textContent = score;
}
})
})
function moveVirus(){
let timer = null;
timer = setInterval(getRandomPosition,800)
}
moveVirus();
function countDown(){
currentTimeLeft--;
time_left.textContent = currentTimeLeft;
if(currentTimeLeft == 0){
clearInterval(timerId);
alert(`Time over! You killed ${score} viruses.`)
reset();
}
}
let timerId = setInterval(countDown,1000)
}
//start();
//reset function
// start pause
//add animation on hit & miss