⭐ If you would like to buy me a coffee, well thank you very much that is mega kind! : https://www.buymeacoffee.com/honeyvig Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Friday, November 7, 2025

Whac-a-mole in JavaScript (super simple!)

 

0:00

hey everyone and welcome to another

0:02

video this week i'm going to show you

0:04

how to build whack-a-mole in super

0:07

simple

0:08

javascript html and css

0:11

no styling no canvas because if you are

0:13

a fan of my channel you know that i

0:15

don't really like

0:16

either i don't really like to style

0:18

these video games because i really do

0:20

want you to be able to take the game and

0:22

make it your own

0:23

and really give it your own extra flair

0:25

and pizzazz

0:26

because i do want to see your game so

0:28

please do share them with me

0:30

on youtube instagram twitter whatever i

0:33

just really love to see

0:34

what you have built but before we get

0:37

started a little word from our

0:38

sponsors who have made this video happen

0:41

harperdb is a scalable database that's

0:43

improving app performance and

0:45

development with solutions like hybrid

0:47

cloud

0:48

with sql on json you no longer have to

0:51

pick a database based

0:52

on the structure of your data harper db

0:55

runs anywhere

0:56

and adjusts any type of data at scale

0:59

you can spin up a powerful free instance

1:01

through the harper db studio where you

1:03

can easily manage both self-hosted and

1:06

hardware db cloud

1:07

managed instances the studio includes

1:09

resources like

1:10

example code sdks and drivers

1:14

make sure to check it out using the link

1:16

in the description

1:17

it is a track link so they can manage

1:19

this ads performance so give it a click

1:21

and help me out in being their favorite

1:24

okay for those of you who don't know how

1:25

to play whack-a-mole this is a really

1:27

good game as we're going to be doing a

1:28

lot of timing events

1:30

so if you're currently learning

1:31

javascript i want to practice your

1:33

timing events this is definitely the

1:34

video for you

1:35

we're going to be building a countdown

1:37

timer that counts down to

1:39

60 seconds from 60 seconds and in that

1:42

60 seconds you have to hit as many moles

1:45

as possible okay which you're gonna add

1:47

to your score

1:48

so that is it in a nutshell this is what

1:50

we're going to build by the end of this

1:52

tutorial

1:53

i hope you're excited i know i am so

1:56

let's

1:57

do it okay so

2:00

first things first here is my file setup

2:03

in which i have some standard

2:06

html boilerplate making sure that

2:09

my stylesheet has the correct path

2:13

to my actual stylesheet which is here

2:16

and same for the script tag to pick up

2:19

my

2:19

javascript making sure that the

2:23

path the relative path is the same

2:27

so the first thing i'm actually going to

2:29

do for my game of whack-a-mole

2:31

is uh make a grid so above the script

2:35

tag make sure it's above the

2:36

script tag i'm just going to make a div

2:40

that has the class of grid

2:44

and in here well i'm just going to put

2:47

in

2:47

nine divs as i want my wacom ogre to be

2:51

three by three and then i'm gonna give

2:54

each of them

2:54

a class of square

2:59

i could technically add these in

3:01

javascript

3:02

but you know it's not that many so i'm

3:04

just gonna

3:06

put in the nine divs each with their own

3:09

id so one

3:13

two three

3:16

four this is so we can identify each one

3:19

but i want each one to look the same so

3:21

i'm going to style all of them as

3:23

squares and that's why i have chosen to

3:26

do it

3:27

this way

3:30

so there we go that is our grid

3:33

we of course need to style it so we can

3:36

see it

3:37

while we are here i'm also going to add

3:41

a h2 tag for the score

3:45

so i actually want my score to show up

3:47

here i'm going to hard

3:48

code it as zero and just give this idea

3:51

of score so we can pick it out later

3:53

with our javascript

3:54

i also want to have another h2

3:58

tag that is going to tell us how much

4:01

time we have left

4:03

so i want the game to count down from 60

4:05

seconds

4:06

and i'm just going to put time

4:10

left and you know what i'm also going to

4:12

give them a label so

4:14

quite simply i'm just going to make it

4:15

really basic please

4:17

style this better than the way i'm doing

4:19

it right now

4:21

i just want to give you the sort of um

4:23

skeleton

4:24

to make your own so your score

4:28

and time left okay this is looking good

4:33

let's style it up so we can see it in

4:35

our browser

4:37

so in here i'm just going to pick out

4:39

the bridge

4:42

and then the grid well i'm going to use

4:44

display

4:46

flex and i'll show you why in a bit

4:50

um actually let's get rid of it so i can

4:53

show you so i'm just going to get rid of

4:54

it

4:54

for now i'm just going to give this a

4:56

width so let's say each of our

4:57

squares is going to have let's make each

5:01

square

5:02

200 pixels uh

5:05

wide and high so which means this would

5:08

be 600 pixels and get this

5:10

height of 600 pixels

5:15

so that will look like a square and then

5:17

let's

5:19

um we could give it a border or we can

5:21

give each square a border so let's do

5:23

that

5:24

so every square with the class of every

5:27

div

5:28

sorry with the class of square we know

5:30

we're looking for divs with classes

5:32

because we put the dot

5:33

same for grid we're looking for a class

5:36

a div

5:36

or an element with the class of grid to

5:40

style

5:41

so let's make each square we decided

5:43

that each square we want to be 200

5:45

pixels

5:46

with our 200 pixels so height 200 pixels

5:49

width 200 pixels

5:50

and then i'm actually gonna give each

5:53

one

5:56

let's give each one a border

5:59

um i'm just gonna go border color and be

6:02

really simple

6:03

of black okay

6:06

so this might be a problem because i

6:08

don't know how thick the border is

6:10

in which case our grid might look a bit

6:13

strange but let's go ahead and see

6:16

so i'm going to copy the path and i'm

6:17

just going to put it

6:21

into our browser

6:28

so here is our grid

6:34

you can see all the squares inside it

6:37

however

6:38

they don't seem to have the border color

6:45

ah that's because we need to give it a

6:48

border style of solid

6:52

you can actually maybe just change this

6:54

it's too much for me

6:55

i can do border solid black one pixel

7:00

so that's another alternative it's just

7:01

like a shorthand

7:03

of what i wrote before and i'm also

7:05

assigning the border

7:06

width so that we can know how big the

7:09

border is going to be

7:10

so now if i refresh we now get

7:14

the nine divs stacks over each other

7:17

what we need is for them to snake over

7:19

each other

7:20

so that it can fit in and i could do so

7:23

by adding display

7:25

flex to my grid so now if i refresh it

7:30

see all the squares will try to fit into

7:33

my grid

7:35

so if i go on grid there's the grid and

7:37

they're all trying to fit in it

7:39

i want them to stack over each other

7:41

like a snake

7:43

so i can do that by adding

7:46

flex wrap wrap

7:50

and there we go now they don't fit

7:54

the third one isn't going here and

7:55

that's because

7:57

we need to make the grid fit the border

7:59

two

8:00

so what would that be one two three four

8:02

five six

8:03

so width six six oh six

8:07

and height six of six and there we go

8:11

the squares now fit perfectly into

8:15

my garage so this is looking good

8:19

if i want to see grid i'm sorry square

8:22

with id3 it's that one

8:24

four is there five is there six is there

8:26

seven eight nine it's literally snaking

8:28

around

8:29

perfect let's carry on

8:34

okay now i also want i'm gonna go ahead

8:37

and make the

8:38

mole for the moment i'm just going to

8:40

give it a background color of blue

8:42

but obviously we want to switch this out

8:44

later so we actually probably want like

8:45

a more image i'll show you how to do

8:47

that

8:48

but for now i'm just going to have the

8:50

class of mold to be blue

8:52

so for example if i add the class of

8:57

mole here and refresh

9:00

you will see that the third square has a

9:03

classic mole

9:04

so that square has turned blue we can

9:07

even see it when we inspect the elements

9:09

so that's great let's just pretend

9:11

that's our mod for now until we get an

9:13

image to put there

9:15

next up let's do some logic so let's

9:17

move on to our app.js

9:20

file okay

9:23

so actually i'm just going to remove the

9:26

mold class from here

9:28

because we're going to add it with

9:32

our javascript so the first thing i want

9:35

to do is actually select all the squares

9:37

so we can work with them in our

9:39

javascript file

9:40

so i'm going to do that by using the

9:42

document query selector so i'm not

9:44

making this

9:45

up document query selector

9:50

all so that's the javascript method that

9:52

will essentially help us

9:53

look for any thing with the class name

9:57

so dot of square

10:00

so we're going to search for all those

10:01

and we're going to store it as

10:04

squares

10:07

squares okay so we're finding all the

10:10

squares and we're storing them

10:12

under squares now even though we don't

10:15

have it because i removed it i'm

10:16

actually also going to store

10:18

the mold so i'm going to go document

10:21

query selector

10:23

not all because there's only going to be

10:25

one mole at any time and i'm going to

10:27

search

10:28

for the class of mole

10:31

so now we have our squares we can work

10:33

with them and then now we have our mole

10:35

if it's on the page that we can work

10:37

with

10:39

okay we have two more things to pick out

10:41

so we can do them later and that is

10:44

the element with the id of time left

10:47

so once again document i could use query

10:50

selector or i could use

10:52

uh get element by id but just so you

10:55

know in query selector if we're

10:56

searching for an

10:57

element with an id we need to use hash

11:00

and then the name so this is like an id

11:03

we're searching for an element with an

11:04

id

11:05

of time left and let's store this

11:09

as time left

11:12

and then finally one more thing and

11:14

that's the score

11:16

so this thing right here we need to get

11:19

this element so that we can manipulate

11:22

this with our javascript

11:24

so once again i would get document query

11:28

selector and then look for the

11:31

id of school and let's

11:34

save this as score

11:37

well maybe yeah let's just save a school

11:39

that's fine

11:40

great so let's start off with the result

11:44

as

11:44

zero even though we've hard coded it we

11:47

need to tell our javascript

11:48

the result that we start with is zero

11:50

and we'll do the time a little bit

11:53

later um let's actually focus on getting

11:57

the uh game to

12:01

work so i'm gonna write a function

12:04

called random

12:10

so we want to get a random square to put

12:12

our mole

12:15

now in here for each square so i'm going

12:18

to grab all the squares

12:20

and using the javascript method of for

12:23

each

12:24

so that is a javascript method i

12:27

am going to

12:30

get the class name

12:33

so i could put i here i could put

12:36

whatever we want i could put square

12:38

so we could actually use square maybe

12:42

that makes more sense so for each square

12:43

in our squares array

12:45

i essentially want to

12:48

get these square

12:52

i want to go class list so that is the

12:55

javascript property

12:56

and i'm going to remove the mole if he

12:59

exists on

13:00

any of the squares so we have a fresh

13:02

slate

13:03

to start with so that is what's

13:05

happening here i'm going getting each

13:07

square and for each of the squares i'm

13:08

removing the class of mole

13:10

so that's the first step that we want to

13:12

do in our function

13:13

before we start adding ammo randomly

13:17

so i'm going to write let random

13:19

position

13:21

and then i'm gonna use so once again i'm

13:23

gonna go into my squares

13:25

array and i am gonna pass

13:28

through so if i pass through a zero into

13:31

my squares array it would literally get

13:33

me that first square

13:34

if i pass a one well then i'm talking to

13:36

the second

13:37

square in my array i need to pass a

13:40

random number in here okay

13:41

i want to pass through a random number

13:43

from essentially

13:46

uh zero to eight because i want a random

13:50

index from zero to eight because there's

13:52

nine squares and we start counting from

13:54

zero

13:56

so i'm going to do this by passing

13:58

through the javascript method of

14:00

math random like this and then

14:03

multiplying it by 9

14:07

but i'm also going to use math floor

14:13

to round whatever that number is down

14:17

so for example if i console

14:20

so now if i console log

14:23

random position

14:28

and look in here

14:33

i need to actually call that function so

14:35

random

14:36

square

14:41

random position before initialization

14:44

okay

14:45

that's because i console load it up here

14:47

whoops

14:48

there we go and refresh

14:52

i'm going to get literally a random

14:55

square

14:56

each time each time with an id

15:00

from one to nine okay

15:04

there we go so that is it that's what is

15:07

happening here

15:08

if i actually console logged

15:11

just this

15:17

you will see that once again i am

15:19

getting a random number

15:21

from essentially zero to h

15:24

let's check that math rand

15:27

my trusty mdn

15:34

okay math random enters a function from

15:36

a floating point in the range from

15:37

zero to less than one okay so in this

15:40

case it's

15:41

we multiplied by nine so zero to less

15:43

than nine

15:44

so that is exactly what numbers are

15:46

getting back and we're rounding it

15:48

down so that's what we're doing see no

15:51

matter how many times i refresh this

15:53

we'll never get anything above eight

15:55

because we're rounding

15:56

down okay so eight's gonna be the

15:59

highest and that makes sense because

16:00

zero

16:00

one two three four five six seven eight

16:03

do we only go up to the eighth index so

16:06

hopefully that makes sense

16:08

uh i have a read of this from mdn if

16:11

you're not sure and have a play around

16:12

with it but that's exactly what we are

16:15

doing here

16:18

great so i'm just going to get rid of

16:19

this for now

16:23

okay now whatever random

16:26

position we should probably have random

16:28

square

16:29

i'm going to have random square because

16:32

essentially what we're getting back is a

16:33

random

16:34

square as we saw by the first console

16:36

log so for whatever random square we

16:38

have

16:39

i'm going to use class list add

16:43

mole class so once again we should now

16:47

get a blue square

16:48

in a random position so that's our mole

16:52

every time you refresh the page putting

16:55

itself in a random square

16:59

let's carry on so i'm happy with that

17:02

but i actually want to put this

17:04

on a timer so i want this to be on a

17:06

timer

17:08

um so what i'm actually going to do is

17:15

instead of just having the function like

17:17

this

17:18

i'm going to write this in another

17:19

function so we can attach it to

17:21

a button if we wish

17:24

move mole

17:28

and then i'm going to put let timer id

17:30

equal null

17:31

because we need to actually stop this

17:33

timer interval from moving

17:35

and i'm going to use the javascript

17:36

method of set interval

17:39

to move the mole so i'm just going to

17:41

put random

17:42

square to generate a random square and

17:45

let's say

17:46

every 500 milliseconds

17:49

so this is a so now i don't need this

17:50

here and

17:52

nothing will happen if i refresh the

17:54

page

17:55

however if we invoke the function or

17:58

call the function

18:00

which is why i want to do this in a

18:01

function because you now have

18:03

the option to attach this to a button if

18:05

you wish

18:07

right because if i do this now it will

18:09

just start on the page load

18:11

see you're getting i'm not doing

18:13

anything we are getting a

18:15

random square light up with a mole every

18:17

500 milliseconds

18:20

so that's some extra work for you if you

18:22

want it at the moment i'm just calling

18:24

the function on the page load but you

18:25

can attach it to

18:26

a button if you wish so that's working

18:31

um it is essentially

18:34

on a timer and we can use this timer

18:36

which is why i've done it like this

18:38

i could have just done that

18:41

but because it's on a timer we can

18:42

actually use this timer to stop

18:44

ammo again in our button if we wish so

18:47

that's

18:48

that's optional for you that's why i've

18:50

put it there

18:52

um cool so the next thing i want to do

18:55

is actually listen out for if we hit

18:58

the mode because we want to get a point

19:00

each time we hit the mall

19:02

so i'm going to do this

19:06

up here and now i'm going to grab the

19:09

squares

19:10

and for each square um

19:13

for each id in the square

19:19

i am going to get the id and use add of

19:23

again this could just be square so let's

19:25

just maybe put this with square

19:26

makes more sense for each square in our

19:29

squares array

19:30

i'm going to add an event listener

19:35

to listen out if we put

19:38

mouse down so you show me

19:42

click on the square and then

19:45

i'm just going to put a function here

19:52

so i'm passing through the event and

19:54

then a function of what's to happen if i

19:56

click the square

19:58

and if the square id

20:02

equals the square that is the random

20:06

square

20:07

so let's actually save that um i'm

20:10

actually going to put

20:12

so we want to save something i'm going

20:14

to put hit position

20:16

and then i'm going to get the random

20:18

squares

20:20

id so i'm literally getting the id of

20:22

the square

20:23

so i'm getting this thing right here and

20:25

that's how we can do it

20:27

so i'm getting the id of the random

20:29

square and i'm saving it to hit position

20:32

so perhaps i'm just going to put let hit

20:34

position and leave it as blank

20:37

if the square id equals

20:41

whatever the hit position is at that

20:43

point in time

20:49

then i want to get the result

20:52

and add one to the result

20:57

cool i also want to display it in our

21:00

score so

21:00

score let's do text content or in

21:04

html whatever you want equals the result

21:08

okay and then i also want

21:11

to make the hit position oops

21:14

so clear out hit position equals

21:18

null again okay cool

21:23

i can just spell that right hit position

21:26

so this is looking good

21:27

so amazing so that's what happens if we

21:30

hit

21:31

a mole let's see if that has worked

21:37

now we've got that working we are adding

21:40

a score each time we hit it the last

21:42

thing we need to do is actually get our

21:44

timer to stop working so let's

21:47

do that i'm going to do that

21:51

below here and then slow it down so we

21:54

can see it a bit because it's way too

21:56

fast

21:57

so i'm going to write a new function

22:01

called countdown

22:10

and then in here so in my function

22:14

uh well with each time we invoke this so

22:17

i'm gonna put this

22:18

um once again because i'm not attaching

22:22

it

22:22

to anything i could just put set a vowel

22:29

i'm just going to put this down here

22:32

and call this let's

22:36

count down timer id so we can

22:40

stop it if we want equal set

22:43

interval and then i'm going to put this

22:46

on a set interval so i'm literally going

22:47

to pass through the countdown function

22:50

and i want the countdown and function to

22:52

invoke every 1 1000 milliseconds it

22:54

would be like one second

22:55

two seconds three seconds and each time

22:58

i want something to happen and with that

23:00

i want to get

23:01

the current time so let's actually write

23:06

um

23:08

let current time equal

23:11

60.

23:15

so i want to get the current time and i

23:18

just want a minus one from it

23:20

and then i actually want to display that

23:22

i want to display that in the time

23:24

left so time left

23:27

text content current

23:31

time okay

23:34

so that's really it uh if however

23:38

current time

23:42

equals zero

23:46

so like we get to the end i want to

23:50

clear the timer so clear interval

23:54

countdown timer i d

23:58

okay so i want to clear this and

24:01

then maybe let's get an alert that says

24:07

game over

24:11

your final score is

24:15

and then the result

24:20

so that is looking good let's have a go

24:24

so to make this a little bit faster i'm

24:25

just going to change this to 10. let's

24:27

see how we're doing 10 seconds

24:29

so zero you can see the time is working

24:32

we're clicking we're clicking on the um

24:34

mole we're getting

24:35

points and then as soon as we get to

24:38

zero

24:39

game over your final score is nine you

24:41

can do with the space there

24:42

but that looks good awesome you can

24:46

see this is still moving around we can

24:48

actually stop that

24:49

thanks to this timer id

24:53

so i'm just going to move this out so

24:55

it's global

24:58

and we can use it here so we also want

25:00

to clear

25:01

interval timer i do

25:05

let's try that now once again

25:09

let's click click click click click

25:12

click click

25:13

click i've got score of five got a score

25:16

off

25:17

okay game of your function score is five

25:20

zero and it stops moving

25:23

so there we have it we have now

25:27

built our game of wekamo

25:30

um if you are interested in how to

25:33

get an image let me show you that very

25:35

quickly

25:38

so i'm just going to use uh this

25:40

whack-a-mole from my github feel free to

25:42

take him i've already pre-made them to

25:44

be 200

25:45

by 200 pixels so let's just download

25:48

that

25:48

i'm going to save image and let's save

25:51

them as

25:51

short mold that sounds good and

25:53

downloads

25:55

and then i'm simply just gonna

26:02

very basically drag it on for him in

26:06

here

26:07

so there he is and then in my style

26:10

sheet instead of having background color

26:12

blue

26:14

i'm gonna have background image

26:18

and then i'm gonna put uh url

26:21

and then the path to my mole so it's the

26:24

root so i just put

26:25

raw mole jpeg because it's a jpeg

26:30

and then i'm going to put background

26:36

size cover

26:40

so let's have a look at that

26:43

and there we go we have our little

26:47

mole moving around

26:50

so oops game over five squares three

26:54

let's fix that space

26:57

okay score is for the space there and

26:59

let's make a move faster so let's get a

27:01

random square every 500

27:03

milliseconds again and let's change the

27:06

current time to start with 60

27:09

seconds so once again boom boom this is

27:12

more realistic

27:13

you can of course add levels you can

27:15

make them go as fast as you want like

27:16

this is really fast

27:19

you will see the score adding and you'll

27:22

see the time left

27:23

so that is it i hope you've enjoyed this

27:26

tutorial

27:27

on how to make whack-a-mole without

27:29

canvas

27:30

and practice your timer skills

27:34

thanks very much for watching and i will

27:36

see you

27:37

soon

No comments:

Post a Comment