File tree Expand file tree Collapse file tree 3 files changed +102
-0
lines changed
Expand file tree Collapse file tree 3 files changed +102
-0
lines changed Original file line number Diff line number Diff line change 1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < title > Whack A Mole!</ title >
6+ < link href ="https://fonts.googleapis.com/css?family=Amatic+SC:400,700 " rel ="stylesheet " type ="text/css ">
7+ < link rel ="stylesheet " href ="style.css ">
8+ </ head >
9+ < body >
10+
11+ < h1 > Whack-a-mole! < span class ="score "> 0</ span > </ h1 >
12+ < button onclick ="startGame() "> Start!</ button >
13+
14+ < div class ="game ">
15+ < div class ="hole hole1 ">
16+ < div class ="mole "> </ div >
17+ </ div >
18+ < div class ="hole hole2 ">
19+ < div class ="mole "> </ div >
20+ </ div >
21+ < div class ="hole hole3 ">
22+ < div class ="mole "> </ div >
23+ </ div >
24+ < div class ="hole hole4 ">
25+ < div class ="mole "> </ div >
26+ </ div >
27+ < div class ="hole hole5 ">
28+ < div class ="mole "> </ div >
29+ </ div >
30+ < div class ="hole hole6 ">
31+ < div class ="mole "> </ div >
32+ </ div >
33+ </ div >
34+
35+ < script src ="script.js " charset ="utf-8 "> </ script >
36+
37+ </ body >
38+ </ html >
Original file line number Diff line number Diff line change 1+ 'use strict' ;
2+
3+ const scoreBoard = document . querySelector ( '.score' ) ;
4+ const holes = document . querySelectorAll ( '.hole' ) ;
5+ const moles = document . querySelectorAll ( '.mole' ) ;
6+
7+ let lastHole ;
8+ let timesup = true ;
9+ let score = 0 ;
10+
11+ function randtime ( min , max ) {
12+ // randtime = rand * (max - min) + min
13+ return Math . round ( Math . random ( ) * ( max - min ) + min ) ;
14+ }
15+
16+
17+ function randomHole ( holes ) {
18+ const idx = Math . floor ( Math . random ( ) * holes . length ) ;
19+ const hole = holes [ idx ] ;
20+ if ( hole === lastHole ) {
21+ console . log ( 'same, retry !' ) ;
22+ randomHole ( holes ) ;
23+ }
24+ lastHole = hole ;
25+ return hole ;
26+ }
27+
28+ function peep ( ) {
29+ const time = randtime ( 200 , 1000 ) ;
30+ const hole = randomHole ( holes ) ;
31+ hole . classList . add ( 'up' ) ;
32+ setTimeout ( ( ) => {
33+ hole . classList . remove ( 'up' ) ;
34+ if ( ! timesup ) peep ( ) ; // eslint-disable-line curly
35+ } , time ) ;
36+ }
37+
38+ function startGame ( ) {
39+ score = 0 ;
40+ scoreBoard . textContent = 0 ;
41+ timesup = false ;
42+ peep ( ) ;
43+ setTimeout ( ( ) => {
44+ timesup = true ;
45+ } , 10000 ) ;
46+ }
47+
48+ function bonk ( event ) {
49+ if ( ! event . isTrusted ) return ; // eslint-disable-line curly
50+ score ++ ;
51+ this . classList . remove ( 'up' ) ;
52+ scoreBoard . textContent = score ;
53+ }
54+
55+ document . querySelector ( 'button' ) . addEventListener ( 'click' , startGame ) ;
56+
57+ moles . forEach ( mole => {
58+ mole . addEventListener ( 'click' , bonk ) ;
59+ } ) ;
Original file line number Diff line number Diff line change 6767.hole .up .mole {
6868 top : 0 ;
6969}
70+
71+ button {
72+ position : relative;
73+ left : 50% ;
74+ }
You can’t perform that action at this time.
0 commit comments