⭐ 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

Build your own CANDY CRUSH using JavaScript, HTML and CSS

 

0:00

hi and welcome to my walkthrough in which I'm gonna show you how to build your very own game of candy crush this

0:05

game is gonna be super simple I say super simple as we're not going to be focusing too much on styling this is a

0:11

JavaScript walkthrough very much gonna be focusing on the logic of the game I am doing this so you can take the game

0:18

start up as you want truly make it your own this game has a lot of potential especially with the drag and drop

0:25

movements we're going to be doing so I'm excited to see what you guys come up with in this walkthrough I'm going to be

0:30

showing you how to create a board randomly generate candies all over the board switch out colors of candies on the

0:37

board drag and drop candies check for matches check for valid matches so check

0:43

that the row of candies doesn't go off into the next row move all the candies down if you get a match and generate new

0:50

candies by the end of this walkthrough you should have also got to grips with inbuilt JavaScript functions such as

0:57

every sum for each includes set attribute and many more I would love

1:03

to see your finished games so please do share them with me on youtube or Twitter my Twitter handle is Anja underscore

1:09

Kubo or Instagram whatever okay so as

1:17

always let's make sure our javascript file and style sheet are linked up properly using these script tag and link

1:22

tag respectively if you have put the two files in the root of your folder make sure that reflects and the path you put

1:28

in your tags if you are not sure what any of this means please do watch my video that I've linked as a card here I

1:35

am putting my script tag in the header as I want to use a Dom event listener on my JavaScript file this is a choice that

1:41

I'm making on my preference as opposed to putting the script tag in the footer now we need to go ahead and put a div

1:49

tag here and give it a class of grid this div tag is where all the magic happens so let's give it the respect and

1:56

deserves and style it up to look like an actual courage for this we flip over to

2:01

our style sheet or CSS file now to pick out our div element that we gave a class

2:07

name to we get the name we also put a dot in front of it this means class

2:13

I have pre decided that when my grid to be made up of 8 by 8 squares

2:18

I thought 70 pixels squares are nice so 70 x 8 that's 560 pixels let's give our

2:27

grid a height of 5 to 60 pixels and a width of 560 pixels as I've done this

2:33

many times I know that if we want to put divs inside the squared the divs will appear stacked over each other we used

2:39

to make them snake over each other row by row so we need to do this by using at display flags and then flexwrap

2:45

so that they break when they can't fit in the grid we have made and start a new row I'm also gonna give the grid a

2:53

background color just so we can see what's happening in our browser let's go ahead so right click on HTML file to see

3:01

this in our browser okay call a blue grid if you inspect the page you will

3:07

see that information has been passed to our browser now let's our CSS file what we want our future squares to look like

3:13

as we said I'm pre decided I want to be 70 by 70 pixels big so height 70 and

3:20

width 70 now let's create some squares go my grid using JavaScript you will see

3:27

here I have already put in my Dom event listener as part of my boiler plate to set up I will now be writing all my

3:33

JavaScript in this event listener for it to be picked up on my script tag which I placed in the header of my HTML as

3:40

mentioned some people liked with a script tag on the footer of the HTML and skip the Dom event listener the choice

3:46

is up to you this Dom event listener is essentially saying in laymen terms that

3:51

I want all of my HTML file to be read before loading this javascript file and is a more foolproof way of making sure

3:58

that these events happen in order now using an inbuilt method called a query

4:03

selector I am telling my JavaScript file to look at my HTML file and find the

4:08

element with the class name of grid indicated by this dot and save it as

4:14

constant grid we are doing this we can reuse this line of code over and over

4:19

again just by typing grid let's also tell our JavaScript file we want our width to be 8 from now on let's

4:25

store that as a and as it won't change so the first thing we're going to do is show you how

4:30

to create a board using javascript I will do this in a function I am choosing

4:36

to call create board now using a for loop I want to imagine our future grinch

4:42

we decided it was going to be 8 by 8 squares so let's use width x width to

4:47

tell our JavaScript that this is the syntax for a for loop what we are doing

4:56

here is looping over something 64 times incrementing by 1 each time from 0 now

5:04

every time it does loop over we want to create a div using an inbuilt method

5:10

called create element we are creating a div element I'm going to call this

5:18

square for our HTML file next we need to

5:24

put that square into a div with a class of grid we do so using another input method called append child we do so by

5:32

passing through the newly created square into a grid constant like this lastly each time we create a div and add

5:39

it into a grid I also want to store it in an array for us to work with so let's

5:44

create an empty array first called squares like this and now as well as put

5:50

a square and their grid I also want to push that square into my array that I called squares we do that using an

5:57

inbuilt method called push now that we have done all this let's see if it works by invoking the function like this so

6:04

save the page go over to our browser and yep it has works if we inspect the page

6:10

we can now see all our divs happily sitting in our garage ok let's flip back now I'm gonna start

6:17

off by going an array of all the candy colors I'm gonna work with so I'm going to call those candy colors as they do

6:23

not change I'm gonna put them in a constant so there we go red yellow

6:29

orange purple and green

6:36

now let's go back to our create board function instead of just creating the square after the square has been created

6:43

I also want to give it a candy color I also wanted to be random to get a random

6:48

number from our array we need three input methods we use math.random and

6:53

multiply it by the arrays like this now we want to make sure the number we get back as a full integer for this we're

7:00

going to use math.floor to round this number down to the nearest full number and there we go we now have a random

7:09

number from zero to five we are starting at zero as Index start count at zero

7:15

this is important so if you're not comfortable this yet please make sure a note to research into this later once we

7:23

have a random number we want to pass that through our candy colors array to

7:28

get a color and assignor to the square so let's get the square and using style

7:34

background color like this we want to assign it to that random color here we

7:40

are getting our candy colors array and passing through a number to it so say my

7:45

random number came out as 1 I'm gonna go into my array and get the item with index number one in this case is yellow

7:53

so my square will not be yellow okay let's go into our style sheet to get rid

7:59

of the blue background we gave our grid to see if this has worked remember what we have written is that now each time

8:05

you create a square we are assigning it a random color from our array okay let's

8:11

say well the files flip over to our browser hit refresh and great we have

8:17

our board let's refresh again just to make sure the colors are coming up randomly each time awesome now that we

8:26

have our board let's move on now each time I create a square I also want to make it draggable this is that we can

8:33

drag candies into different locations around our edge using the import method set attribute I add draggable like this

8:41

and make it true let's say this and see if this worked hit refresh and great

8:49

each square is now pluggable I'm going to do one more thing before we move on and that is given ID to each square this

8:57

is so that when we move a square or drag one you know exactly which one in our grid we are moving this is going to be

9:04

super useful for later so again using setattribute we write this and pass

9:10

through ideas of string and I as we are looping over with an I so now you shall

9:17

we loop over this we're gonna give it a number from 0 to 63 y 63 this is because

9:23

we have 64 squares in our grid of indexes from 0 to 63 ok cool we are now

9:29

done with creating the board the next thing we need to do is concentrate on the draggable feature now to drag the

9:36

candies I actually got taught this a while back and it invokes event listeners that listen at each stage of

9:42

dragging each stage of the dragon consists of five events this is cool

9:47

especially for our styling later as it allows us to add styling and timers each of the five stages I'm gonna do the bare

9:54

minimum for us to get to the game going and leave these go wild on the styling later on so here I'm writing out each of

10:01

the five event listener events the first one is drag start a scene in the green

10:07

string so for each item or as I'm gonna choose to call square + y square x'

10:14

array I'm adding an event listener that will listen out for an event the event

10:19

is green here and in bolt then if the event is triggered

10:24

I want a function to get invoked the five events we have are drag start drag

10:32

and drag over drag enter drag leave and

10:37

drop remember these are in built once we

10:44

have all the event listeners attached to each square on our grid we need to write functions for what happens with your

10:49

triggers so let's do that now all five of them I'm going to put a console log in each one just so I can show you

10:56

exactly what is happening

11:06

now it's also pass through the ID of these squares being listened to we do so using this ID this will pick up the

11:14

element it's being listened to and it's ID which we set using the set attribute method just so we are clear on what is

11:25

happening and wasn't gonna write the event happening in the console log like this okay I'm just gonna move drag end

11:31

and put it towards the bottom of our file just so I can show you what is happening in order

11:47

once we are happy with this let's click Save

11:58

now go to your browser and refresh now when we pick an element to drag you will

12:04

see exactly what was happening in our console log this is super useful

12:17

okay great now back to our javascript file let's focus on the drag start function as this is the first event I

12:24

want to say the color we were decided to pick up and drag let's store this as

12:30

quite literally color being dragged now in our drag start function this style

12:36

background color I'm gonna save this into a variable I'm doing this because I want to use it again let's see if that

12:43

has worked using the console.log

12:51

okay great we can now see that we are picking up the color of the square we are dragging

12:57

if we click another one that works too

13:03

now let's focus on the drag edge so and candy crush when you select a candy to

13:09

put into another location you actually first swap it out with another candy in that location so the Candy's essentially

13:16

swap place we can do this in JavaScript we just need to store another candy color so let's do that

13:23

in the drag drop function if I drop a

13:29

element in here I want to store the original color in that square so let's

13:35

do that let's store this color as color being replaced so again using this style

13:43

background color let's store that up here too so let color being replaced okay great

13:53

we now have the colors stored to replace them in the correct squares however we need to rely on the squares IDs that we

14:00

set so I'm going to call this square ID being dragged in our drag star function

14:06

and assign it this squares ID we need to make sure this is a number so using an

14:13

inbuilt method called pass int we are doing just that

14:20

so now as well as the colors let's store the IDS of the squares being selected

14:25

and drugs and the square we are dropping our candy in all right so square ID

14:31

being dragged and to our ID being replaced now in the drug drop is where

14:40

we assign the ID we need to make sure this is the number by using the mbot method passed int so parse the IDS

14:47

through that method and store it again okay now passing through the ID of the

14:53

scribing drugs into the squares array I want to add this squares color so just

14:58

to be clear this will give the original square being dragged this color after we

15:04

have draw in a new color in here let's say what we have done so far to

15:10

check if this is working so head over to our browser hit refresh you will see

15:15

that if I drag a square and let it go it goes back now if I drag a square and

15:21

drop it in another square it doesn't get dropped this is not what we expected right this is because like I said there

15:27

are five stages that we need to account for let's skip over the drug over and drag enter event for now I do this using

15:35

be prevent default so using an ephah event we pass through an event into the

15:41

function and using prevent default we prevent it from doing a c4 action if you

15:46

do want something to happen while we are dragging the squares ever each other or as it enters a new square you are free

15:51

to go back after this walk through and add some animations and stylings and whatever okay now let's flip back and

15:59

refresh and great now when we drag a Square and drop it into a new one we

16:05

change or the original square into a color being replaced we are one step in the right direction

16:11

however the square we are dropping into stays the same color let's fix that so

16:17

and the drag drop function we need to make the color of the square change into the color being dragged let's do that

16:25

like this okay great now in candy crush we can switch out your candies however

16:32

these two candies NewsView directly and a square above below to the left or to the right of our current candy we need

16:40

to tell our JavaScript this and define what is a valid move so what is a valid move let's define this and store it

16:47

let's first get an array of all of our moves for any square being dragged I'm going to call it valid moves

16:54

so Arthur solid move is any square being dragged ID minus one so say it's ID is

17:02

67 we are saying we are allowed to switch out with a square with the ID 66

17:07

now let's do the same with a square visually above our chosen square so exactly one width back in our array so

17:15

this I am square with the ID 59 as our width is defined as H

17:21

I'm just going to tie this up a bit giving them each their own space do the same for square 68 so our chosen ID plus

17:28

1 and square 75 so I chose an ID square

17:34

plus 8 this time this is the square that is visually appearing below our square on the garage okay now we have all our

17:42

Valley News live Allen move using an input JavaScript method called includes I am also going to pass through a value

17:53

here so what I'm saying is that if the number passed through so the square ID

18:00

being replaced is included in our value moves array this statement is true and

18:06

we store the billion of true for our value news variable now using an if

18:11

statement if the ID of my squares being replaced exists as we want to make sure

18:16

that this is only going to happen if my square is being dropped in another square and not if it's going off the

18:22

grid and so double and if this move is valid we execute the block of code we

18:29

need both these variables to be true or truthy in order to execute this code so

18:34

if they are true we clear the value of square being replaced ready for a fresh

18:40

start if however there is a square that the dried square can be dropped in but

18:46

it's not a valid move indicated by this bang here so in other words it's a

18:51

square that is too far we execute another block of code we can't go here

18:56

so we want to return the square being replaced with original spot by simply giving it back its color

19:09

you we then also need to do the same for the

19:16

original square so using the same method we get the square D being dragged pass

19:22

it through our squares array and using star background color assign it the color being dragged if none of these

19:29

statements are true so the square we are dragging has nowhere to go and is outside our grid we simply add the color

19:35

being drive back to its original space

19:42

okay great all three scenarios are now covered let's save and refresh our

19:47

browser great you will see the square switching other colors you will also see

19:56

are squares return back if we try and drop it outside of The Grudge everything seems to be working out for us so far in

20:03

candy crush if you get a matching row or column of three four or five three things happen the candies disappear all

20:11

the candies in the grid move down into free space and we gain points in the

20:16

actual game a few more things happen like animation wires but as I mentioned this is a JavaScript walkthrough so

20:22

styling and we leave that up to you at the end let's get going on the first part checking for matches I am going to

20:29

concentrate on finding a row of three fast as we will use this logic to find matches for the rest let's call our

20:37

function check row for three in our

20:43

function and we're going to fast aside how many squares we need to loop over exactly this is because if you think

20:49

about our as compiled of three squares if we check for three squares starting from index 63 in our grid this will

20:56

break the game as there's no squares after a square index 33 using maths we

21:02

figure out that the last square we could possibly loop over is 61 so that the loop would check 61 62 and 63 for a

21:09

match of three colors now let's define our row so if we used indexes to draw

21:15

our row it would look like this and I followed by an I plus 1 and an I

21:21

plus 2 each time we loop over this we are swapping out the I for a number from

21:27

0 to 61 now each time we loop over we also when a grab the color the first

21:32

square is an assign it to the decided color let's also define what a blank

21:38

spaces as we account for this when we are looping so it's blank if the squares background colors empty

21:45

so DV equals empty this statement is true now that we have these two things

21:51

to find let's get to writing our logic so you if statement let's get the row of food a

21:57

row using a nimble method called every if every index in our three array deeply

22:05

equals the decided color so the color of our first square we want to execute some

22:11

code

22:23

so if we find a match we want to take the row of three array and for every

22:28

index in it we want to give it an empty background color great now we also said

22:37

we want to add a score let's make this a score of three points each time we find a row of three let's scroll to the top

22:45

of our javascript file and add this as a global variable to our file we will also

22:50

set the score as zero to start that makes sense right great

22:55

now let's scroll back down and invoke this function we wrote to see if it works let's go ahead and save this

23:02

refresh the browser and amazing we can see here that one row has been deleted

23:10

now we want to actually invoke this function constantly while we are playing the game at the moment there's only get

23:16

some vote once we start up the browser meaning that if we get any matches from now on they will not be cleared we can

23:23

do this using a set interval and grabbing our window so with us here I'm

23:29

getting our window adding the M up method of set interval turret and saying that any code in this

23:34

function so in my case the Czech refer three function I want to invoke every 100 milliseconds let's say this and

23:42

check it in our browser now it's important to note that this will keep running indefinitely until you close

23:49

your tab I am gonna keep it like this but if you want to take your game to the next level and also you know for best

23:56

practice I will suggest adding a button to activate this set interval and also

24:01

maybe add some pause and stop functionality I do this son of the game so if you want to see how to do that

24:08

please do check out my Tetris game for example ok now that that works let's carry on okay now that we have that I'm

24:15

literally gonna copy/paste this to make our check column for three function with

24:21

a few changes of course so let's start by changing the name of the function change the pseudocode for good measure

24:27

change the name of the array here here and here

24:35

change when the loop stops so as these are columns the last square we can check

24:42

for a column of 3 is square 47 change

24:47

the array itself to be a column using width and width or multiply by 2 and

24:55

change the name of the function here when we invoke it we also now add it to

25:01

our set interval

25:07

okay that should do it let's head over to our browser okay fantastic

25:12

it looks like our game is working as we wanted to you can see here that the columns and rows are deleting if they

25:19

find a match now I'm trying to find an example here even though it's hard to come by but what do you think happens

25:27

when a row starts at the left or right hand edge or half of its body is in the

25:32

right hand column or edge it will appear to be broken up into but still valid as

25:38

we haven't told our JavaScript that this is not a valid move we need to fix this as this can only happen for rows let's

25:45

go back into our check row for three function back in our function let's define or what is a valid move again

25:53

let's get an array of all the indexes that are not valid what I am typing here is every index I do not want my row of

25:59

three to start at so I don't want to start it in like six otherwise a row would appear on the other side of my

26:05

grid same for index seven and so on and so on all of these are not valid for me

26:11

to start my checking for three are so once again let's get the array and use

26:18

the includes input javascript method and then we pass through I so the number we

26:23

are looping if this number is included in the array not valid we want to skip

26:29

it we do so like this using continue and that's it let's check this in our

26:36

browser let's save and hit refresh it's hard to tell but we have now fixed our

26:42

problem of rows of threes splitting up and still counting as valid rows remember we do not need to do this for

26:48

columns as we already have stopped our columns from going through the bottom by stopping the loop at square 47 the only

26:56

thing we do now are copy exactly what we have done for rows and columns of three but do the same but checking for four

27:03

this time in my code I'm literally gonna copy and paste the check row for three

27:09

and check for column of four function I mean you probably shouldn't really do this you should type each one out as

27:15

copying basically really dangerous especially when you know just probably places something really similar

27:21

but hey I feel comfortable with it so I'm just gonna go for her okay so this

27:27

is my piece of code I appreciate it's hard to tell once again I'm going to change the three

27:32

here to afford the pseudocode rename the function change the array name change

27:41

the loop to stop at 60 and add an item to the array to make it on row four I'm

27:49

also going to update the not valid index errors include indexes starting at five and so on as now each row needs to be

27:56

checked one square earlier for each row

28:03

change the array name here change the score to add for change the Iranian here

28:11

and change the functioning to check for a row of four and finally you change the

28:20

column of three copy version to column of four in the pseudocode check the function name change that make

28:29

the array an actual column of four change the name of the array here change

28:35

the school to add for change the name here being in the function to say check

28:43

for color for now we add these two new functions into the set interval I want

28:50

to check for four before checking three as we want to get rid of the big ones first

28:58

okay let's save that refresh the page I'm getting an error message I told you

29:06

copy and pasting was dangerous what's it saying it seems that why a Rea

29:12

name has not been defined in our check column up for four let's go fix that now

29:18

oh yes I forgot to change the a rare name we were still using the old one

29:23

here okay that's not fixed let's go ahead and save and there we have it I am

29:35

actually going to leave this here if you want to go ahead and add a check for five please do I feel we do not need to

29:41

go over this because it's simply a case of copy and pasting hopefully you get the idea now of what we need to do and

29:47

what needs to be changed in order to check for five let's now instead focus on how to get

29:54

the candies to move down if some have been cleared and also how to get new candies to generate we need to write a

30:00

function that will move all our candies down if a certain condition is met so let's do that before checking any of our

30:06

matches here in our code let's start by writing out what we want to happen now

30:13

I'm going to choose to call my function and move candies actually let's call it

30:18

move down I'm going to use a for loop here again to check all our squares up

30:24

to square 55 so essentially the first seven rows I'm going to do this as I

30:30

want to check the squares below each of the indexes for an empty square so in

30:35

this if statement I am getting at the index number plus the width and passing

30:41

it through our squares array once I am in that square I want to check that squares background color if that square

30:52

has an empty background color I want this statement to be true so what we are saying here if the square directly below

30:59

our square we are looping has an empty background color we want to execute the following code we want to essentially

31:06

give our squares background color to it so we are simply using the style background

31:12

color to do this I'm passing a color to the empty square now we want to remove

31:18

any color from a square making up here empty the final thing we need to do is

31:24

put it in our set interval so let's copy this scroll down into our set interval

31:30

and place it there I'm gonna put it before the others just so excuse first

31:36

okay now let's save and refresh our browser and great we can see that the

31:43

squares are all clearing as we get matches now we do one last thing and that's generating new candies to show up

31:50

in our first row now let us think about the logic here I think it's safe to say that if the first row contains an empty

31:56

square we want to fill it with a colored one and we keep doing this until the first row is not empty anymore so let's

32:04

do it let's define the first row fast I'm simply going to write an array putting an index at 0 to 7

32:12

now to check if something is in the first row we do this get the first row a

32:18

touch includes to it and pass through I now if I is in the first row row the

32:25

statement will come back as the boolean of true so if is first row comes back is

32:31

true and the square as a background color of Mt and this is true we need to

32:36

do something we use a fill it with a color using this line we wrote before we

32:45

get a random color this should be familiar to you it is literally the exact same long you read before

32:51

I know I call it a random color but its secondly a random number from my candy

32:57

colors array so a number from 0 to 5 once we get a random number from our

33:03

candy colors array we need to pass it through our array to get a string of a color name we then assign it to our

33:10

square and the first row

33:15

cool let's look at this in action let's refresh an amazing that has now worked

33:26

now let's get the score to display as we are collecting the school they're not really doing much with it at the moment

33:32

for this let's decide we're gonna make a div with an ID of score and call it a

33:37

score dislike we will need to add this

33:46

to our HTML file but for now let's just add it in our JavaScript first so let's

33:52

scroll down to every time we add a score using the constant I'm going to use an

33:57

inbuilt method of inner HTML we then assign the school to it I'm now going to

34:04

add this all for about checking the matches functions after every time we add to our school's variable once we

34:12

have done that let's flip over to our age shimmer in our HTML file I'm

34:19

actually going to add a deer with a class of scoreboard first this is so that we can start a board where we want

34:25

our school to show up inside I am placing two h3 tags one with the word

34:31

score and one with the ID of score this is what we sign a Const of score display

34:37

two in our JavaScript now let's save and head over to our browser okay great we

34:44

now have a fully functioning game of candy crush as mentioned this is a super

34:50

simple version of the game I have not added in checks for matches of five please feel free to do that if you want

34:56

also and the official candy crush game we have a lot more funky stuff going on I hope that by getting to where we are

35:03

so far you are now countable and other tools to go ahead and add all these funky features to your game hopefully

35:09

you can also create your own and hey the world's your oyster I am going to show you one more thing and that's how to add

35:16

images each candy color so if you understand this please do carry on let's

35:22

go ahead and do that now so let's flip back to our code

35:27

in this list of candy colors here I'm gonna want to replace them with images now I'm going to create a folder here to

35:34

drop some images I have pre-made if you want these images I'm going to share them all with you I'm gonna put a

35:40

link to my github in the description or you know you can feel free to also make your own I have made each image 70 by 70

35:49

pixels so that it will fit in each of my squares perfectly once I have made this

35:54

folder I'm simply gonna drag all my made images into this folder so here are my

36:00

images I am simply selecting them and dragging them into my project folder if I click

36:07

on this images folder now you will see all my image files now I'm gonna copy each relative path of each image like

36:14

this note I'm using the one URL I'm placing each part in brackets like this

36:20

you need to get the exact path name for this to work

36:33

now because we are not using the background color anymore we are using background image we need to find all the

36:40

instances of when I say background color in my file and replace them as M was a cool finder

36:47

in place function that helps me replace each one without missing any you can see here I have used the word 30 times I'm

36:55

gonna go ahead and click replace for okay great let's go ahead and see if

37:00

that has worked and amazing there are all my candies I obviously serve a whole

37:06

bunch of console looks of my code one thing I would suggest is definitely clearing this up before you publish your

37:12

code anywhere just for good practice now I'm just gonna show you something that I baked a little earlier this is my style

37:20

version on my candy crush like game we mentioned earlier in the walkthrough

37:26

that a button starting and stopping this game would be wise as we are essentially invoking the functions every 100

37:31

milliseconds so adding a button would be best practice if you want to take this game to that extra level so that's it

37:38

thank you very much for watching as always I would love to see your finished games please do tag me on YouTube

37:45

Twitter Instagram whatever I'd love to see what you've built how you taking the game made it your own taking it to the

37:51

next extra level so yeah enjoy and thanks again

No comments:

Post a Comment