1
1
<!DOCTYPE html>
2
2
< html lang ="en ">
3
+
3
4
< head >
4
5
< meta charset ="UTF-8 ">
5
6
< title > Whack A Mole!</ title >
6
7
< link href ='https://fonts.googleapis.com/css?family=Amatic+SC:400,700 ' rel ='stylesheet ' type ='text/css '>
7
8
< link rel ="stylesheet " href ="style.css ">
8
9
</ head >
10
+
9
11
< body >
10
12
11
- < h1 > Whack-a-mole! < span class ="score "> 0</ span > </ h1 >
13
+ < h1 > Whack-a-mole!</ h1 >
14
+ < h2 > Score:
15
+ < span class ="score "> 0</ span >
16
+ </ h2 >
12
17
< button onClick ="startGame() "> Start!</ button >
13
18
14
19
< div class ="game ">
@@ -31,57 +36,61 @@ <h1>Whack-a-mole! <span class="score">0</span></h1>
31
36
< div class ="mole "> </ div >
32
37
</ div >
33
38
</ div >
39
+ < audio src ="clap.wav "> </ audio >
40
+ < script >
41
+ const holes = document . querySelectorAll ( '.hole' ) ;
42
+ const scoreBoard = document . querySelector ( '.score' ) ;
43
+ const moles = document . querySelectorAll ( '.mole' ) ;
44
+ const clapSound = document . querySelector ( 'audio' ) ;
45
+ let lastHole ;
46
+ let timeUp = false ;
47
+ let score = 0 ;
34
48
35
- < script >
36
- const holes = document . querySelectorAll ( '.hole' ) ;
37
- const scoreBoard = document . querySelector ( '.score' ) ;
38
- const moles = document . querySelectorAll ( '.mole' ) ;
39
- let lastHole ;
40
- let timeUp = false ;
41
- let score = 0 ;
42
-
43
- function randomTime ( min , max ) {
44
- return Math . round ( Math . random ( ) * ( max - min ) + min ) ;
45
- }
49
+ function randomTime ( min , max ) {
50
+ return Math . round ( Math . random ( ) * ( max - min ) + min ) ;
51
+ }
46
52
47
- function randomHole ( holes ) {
48
- const idx = Math . floor ( Math . random ( ) * holes . length ) ;
49
- const hole = holes [ idx ] ;
50
- if ( hole === lastHole ) {
51
- console . log ( 'Ah nah thats the same one bud' ) ;
52
- return randomHole ( holes ) ;
53
+ function randomHole ( holes ) {
54
+ const idx = Math . floor ( Math . random ( ) * holes . length ) ;
55
+ const hole = holes [ idx ] ;
56
+ if ( hole === lastHole ) {
57
+ console . log ( 'Ah nah thats the same one bud' ) ;
58
+ return randomHole ( holes ) ;
59
+ }
60
+ lastHole = hole ;
61
+ return hole ;
53
62
}
54
- lastHole = hole ;
55
- return hole ;
56
- }
57
63
58
- function peep ( ) {
59
- const time = randomTime ( 200 , 1000 ) ;
60
- const hole = randomHole ( holes ) ;
61
- hole . classList . add ( 'up' ) ;
62
- setTimeout ( ( ) => {
63
- hole . classList . remove ( 'up' ) ;
64
- if ( ! timeUp ) peep ( ) ;
65
- } , time ) ;
66
- }
64
+ function peep ( ) {
65
+ const time = randomTime ( 250 , 1000 ) ;
66
+ const hole = randomHole ( holes ) ;
67
+ hole . classList . add ( 'up' ) ;
68
+ setTimeout ( ( ) => {
69
+ hole . classList . remove ( 'up' ) ;
70
+ if ( ! timeUp ) peep ( ) ;
71
+ } , time ) ;
72
+ }
67
73
68
- function startGame ( ) {
69
- scoreBoard . textContent = 0 ;
70
- timeUp = false ;
71
- score = 0 ;
72
- peep ( ) ;
73
- setTimeout ( ( ) => timeUp = true , 10000 )
74
- }
74
+ function startGame ( ) {
75
+ scoreBoard . textContent = 0 ;
76
+ timeUp = false ;
77
+ score = 0 ;
78
+ peep ( ) ;
79
+ setTimeout ( ( ) => timeUp = true , 10000 )
80
+ }
75
81
76
- function bonk ( e ) {
77
- if ( ! e . isTrusted ) return ; // cheater!
78
- score ++ ;
79
- this . classList . remove ( 'up' ) ;
80
- scoreBoard . textContent = score ;
81
- }
82
+ function bonk ( e ) {
83
+ if ( ! e . isTrusted ) return ; // cheater!
84
+ score ++ ;
85
+ this . classList . remove ( 'up' ) ;
86
+ scoreBoard . textContent = score ;
87
+ clapSound . currentTime = 0 ;
88
+ clapSound . play ( ) ;
89
+ }
82
90
83
- moles . forEach ( mole => mole . addEventListener ( 'click' , bonk ) ) ;
91
+ moles . forEach ( mole => mole . addEventListener ( 'click' , bonk ) ) ;
84
92
85
- </ script >
93
+ </ script >
86
94
</ body >
95
+
87
96
</ html >
0 commit comments