File tree Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Expand file tree Collapse file tree 1 file changed +55
-0
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,61 @@ <h1>Whack-a-mole! <span class="score">0</span></h1>
39
39
const holes = document . querySelectorAll ( '.hole' ) ;
40
40
const scoreBoard = document . querySelector ( '.score' ) ;
41
41
const moles = document . querySelectorAll ( '.mole' ) ;
42
+ let lastHole ;
43
+ let timeUp = false ;
44
+ let score = 0 ;
45
+
46
+ function randomTime ( min , max ) {
47
+ return Math . round ( Math . random ( ) * ( max - min + min ) ) ;
48
+ }
49
+
50
+ function randomHole ( holes ) {
51
+ const index = Math . floor ( Math . random ( ) * holes . length ) ;
52
+ const hole = holes [ index ] ;
53
+
54
+ if ( hole === lastHole ) {
55
+ return randomHole ( holes ) ;
56
+ }
57
+
58
+ lastHole = hole ;
59
+
60
+ return hole ;
61
+ }
62
+
63
+ function peep ( ) {
64
+ const time = randomTime ( 200 , 1000 ) ;
65
+ const hole = randomHole ( holes ) ;
66
+
67
+ hole . classList . add ( 'up' ) ;
68
+ setTimeout ( ( ) => {
69
+ hole . classList . remove ( 'up' ) ;
70
+
71
+ if ( ! timeUp ) {
72
+ peep ( ) ;
73
+ }
74
+ } , time ) ;
75
+ }
76
+
77
+ function startGame ( ) {
78
+ scoreBoard . textContent = 0 ;
79
+ timeUp = false ;
80
+ score = 0 ;
81
+ peep ( ) ;
82
+
83
+ setTimeout ( ( ) => ( timeUp = true ) , 10000 ) ;
84
+ }
85
+
86
+ function bonk ( event ) {
87
+ if ( ! event . isTrusted ) {
88
+ return ;
89
+ }
90
+
91
+ score += 100 ;
92
+ this . classList . remove ( 'up' ) ;
93
+ scoreBoard . textContent = score ;
94
+ }
95
+
96
+ moles . forEach ( mole => mole . addEventListener ( 'click' , bonk ) ) ;
42
97
</ script >
43
98
</ body >
44
99
</ html >
You can’t perform that action at this time.
0 commit comments