-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWhack-A-Mole.html
116 lines (100 loc) · 2.38 KB
/
Whack-A-Mole.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<style>
button {
position: relative;
align-items: center;
display: inline-flex;
justify-content: center;
text-align: center;
}
div {
border: 2px solid black;
min-height: 100px;
}
.body {
display: flex;
flex-flow: row wrap;
}
.divs{
flex-basis: calc(100% / 4 - 8px);
margin: 2px;
}
.center {
max-width: 50%;
margin: auto;
}
.txtcenter {
text-align: center;
}
.mole {
background-color: rgb(183, 142, 221);
}
</style>
<header class="center">
<button>Start</button>
<h1>Your Score:</h1>
<h1 id="score">0</h1>
<h1>Time left:</h1>
<h1 id="time-left">60</h1>
</header>
<body>
<main class="body center">
<div class="divs"></div>
<div class="divs"></div>
<div class="divs"></div>
<div class="divs"></div>
<div class="divs"></div>
<div class="divs"></div>
<div class="divs"></div>
<div class="divs"></div>
<script>
const square = document.querySelectorAll(".divs")
const mole = document.querySelectorAll(".mole")
const timeLeft = document.querySelector("#time-left")
const score = document.querySelector("#score")
let result = 0
let hitPosition
let currentTime = 60
let timerId = null
function randomSquare(){
square.forEach(square => {
square.classList.remove("mole")
})
let randomSquare = square[Math.floor(Math.random() * 9)]
randomSquare.classList.add("mole")
hitPosition = randomSquare.id
}
square.forEach(square => {
square.addEventListener("click", () => {
if (square.id === hitPosition) {
result++
score.textContent = result
hitPosition = null
}
})
})
function moveMole(){
timerId = setInterval(randomSquare, 1000)
}
moveMole ()
function countDown (){
currentTime--
timeLeft.textContent = currentTime
if (currentTime == 0) {
clearInterval(countDownTimerId)
clearInterval(timerId)
alert("Game Over! Your final score is" + result)
}
}
let countDownTimerId = setInterval(countDown, 1000)
</script>
</main>
</body>
</html>