⭐ 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

Learn JavaScript by building Mario! (Super simple!)

 

0:00

Okay, so today we're going to be building a game of Mario in pure JavaScript, HTML, and CSS. No fancy

0:07

frameworks, no Canva, no, we're doing this to actually practice our JavaScript

0:13

skills. So, this is the finished game. We've got a little game of Mario. We have Mario himself. He can jump. He can

0:20

kill the evil little mushrooms. And he can bounce into surprise blocks, which will either give him a mushroom in which

0:25

he grows bigger for a certain amount of time, or get coins. So, it's a surprise each time. We of course also have pipes

0:33

to let him go to next level. So, yes, we're going to have two levels in here. But don't worry, this game is all about

0:40

practicing your fundamentals and gaining knowledge so you can take this game, make it your own, add as many levels as

0:47

you want because you will have the knowledge to do so. So, once again, as a reminder, this is to practice your

0:54

JavaScript. Really master your JavaScript. It's going to assume you have knowledge of objects. It's going to assume you have knowledge of JavaScript

1:00

methods. If you don't, however, don't worry. Please try to code along anyway. You always have my 12-hour free coding

1:06

course which you could go back to. But we do go into these in a lot more detail. Okay? Once again, this assumes a

1:13

certain level of JavaScript. But if you're feeling brave, try it anyway. Who knows? You might be a coding prodigy.

1:20

And if you're interested in deploying this game onto the internet or deploying anything you want really, I'm going to

1:25

show you where to go for that. This is actually outside of the scope of this tutorial, but if you finish this project

1:31

and want to deploy it, you could do so with Savala for free. So, make sure to go to the link below and bookmark it.

1:37

Simply sign up to the cloud platform and deploy straight from GitHub, GitLab, or Git Bucket in minutes. So, in other

1:42

words, save your code to GitHub like so and deploy with Savala onto the

1:48

internet. Easy. Whether you're launching your first side project like this one or scaling a serious application with

1:53

sensitive data in the future, Savala, which is actually built on Google Cloud Platform and backed by Kinsta, offers

2:00

added benefits such as security all-in-one intuitive dashboard. Okay, so

2:06

what are we waiting for? Let's get to it. Let's build out Mario. Okay, so let's do it. Let's build a super simple

2:13

game of Mario using just HTML, CSS, and JavaScript. No fancy things like Canva.

2:20

We're really getting to understand the fundamentals of JavaScript, the language. So, let's do it. So, let's go

2:26

ahead and create a new project. I'm going to choose to make this just an empty project. Okay. So, we're really

2:32

starting from scratch. I'm going to call this Mariojs. And this lives in a directory called WebStorm projects that

2:38

lives on the Ana Kuba username on my computer. So, let's go ahead and create that. Okay. So, there we go. At the

2:46

moment, it's just like this. And I'm going to create a new file. This is going to be an HTML file. I'm going to

2:53

call it index. We don't need to put the HTML extension because, you know, we selected HTML file, but if you're not,

3:00

please go ahead and do that. So, this is what it looks like. I'm just going to call this Mario JS just like so. Here's

3:07

the boilerplate code if you don't have yours in your IDE already. Okay. So,

3:12

that's what it looks like. Next, we're going to create an app.js file. This is going to be for our JavaScript. So, once

3:20

again, I'm just going to do JavaScript file. Call it app. And you can put the extension or not. So, this will now know

3:26

to treat this as a JavaScript file. Wonderful.

3:31

And next, we're going to have to create a CSS file. So, this is going to be for

3:37

our stylesheet. So, we can just select stylesheet. to select CSS and I'm going to call this

3:45

styles. So once again, styles.css. So now our code editor knows to treat

3:50

this as a CSS file because of that CSS extension. Great. Now let's link them

3:56

up. So back in here, I'm just going to use the link element just like so. It's

4:02

a self-closing tag and I'm going to do re stylesheet and then href and then I'm

4:08

going to link the styles.css CSS file is in the same directory. Okay, so on the same level. So I'm just going to type in

4:15

style CSS to get that file name because it lives in the same place as the index html.

4:21

Great. And for the JavaScript, well, I'm just going to put a script tag at the very bottom of my body. And instead of

4:28

index js, we need app.js, right? Because we need to go to the app.js file. But we

4:34

do it after all of the HTML has been read first, which is why this needs to be at the bottom in between my two body

4:42

tags. Okay, so great. We're set up. We've linked up all the files. We linked

4:47

up the JavaScript file and the styles CSS file. So, we are now ready to

4:53

continue. So, in order to make our super simple Mario game, first off, I'm just

4:59

going to use an H1 tag. And I'm just going to put simple Mario game just like

5:08

so. And let's go ahead and put a mushroom there. So wonderful. That's the

5:13

H1 tag. And then I'm just going to use a div. And this is going to have all our game stuff in it. So I'm going to use

5:20

the class a class name of game container.

5:26

Okay. so that we can essentially style this whole div as some kind of game container essentially. And for now I'm

5:33

just going to put in another div and this is going to be the user interface

5:41

which is going to hold the score, the levels and the lives. So let's give this the class name of UI. And then in here

5:50

I'm going to just put divs. Uh let's put score and then let's use a span to put

5:57

the actual score which I'm going to hardcode a zero for now. We're going to pick out this whole element, the whole

6:03

span element. Okay. And I'm going to give this the ID of score. So we can

6:09

pick that whole thing out and essentially change this zero to be a one or a two or a three or a four. So that's

6:16

what I'm going to do. You can keep this as a div or you can put this as a paragraph element. It's really up to you

6:23

and how you want to style this. So, we've got the score, the levels, and the lives. So, score, levels,

6:32

lives. I will say one thing, it's better to have a P element just in terms of it

6:37

being more semantically correct than using a div because essentially it is a bunch of text. So, a P element would fit

6:44

much more level. I'm going to hardcode this as one as you don't really start from level zero. and lives. I'm going to

6:51

also have quote this to three just like so. Okay, great. I'm just going to move this

6:57

over as well for now. In fact, let's just get rid of it. So, wonderful. The

7:02

next thing I'm going to do is just lower this once more. Making sure that it is in the game container. I'm going to add

7:09

a div. And this time, this div is going to have another div inside. And let's

7:14

give this the id of game area.

7:21

Just like that. And this the ID of Mario. So this is going to be our little

7:26

Mario. Next, I'm just going to add another div. And this is going to be the controls. So class name controls just

7:35

like so. And in here, we're going to have a P element. And this is going to

7:41

say controls just like that. Another P

7:46

element that's going to say arrow key. So this is essentially the instructions on how to move on our board. Move left,

7:54

right with the arrow keys. And one more P element and this is going to say down

7:59

arrow and down arrow is to enter the pipe. Okay. When on top of it. Great. So

8:09

that is our instructions essentially on how to control Mario. And finally, we're

8:14

going to have one more. Okay. So, let's close off the game container. And

8:20

outside of the game container, I'm going to have another div. This is going to have the class name of game over. So, we

8:28

know that if it's a game over, well, let's have the game over appear quite

8:34

large. This H2 element is just going to have the words game over. And let's pick it

8:40

out. I'm going to give it an ID of game over

8:45

title just like so. And then let's use the P element to have score. And once

8:52

again, let's pick it out. So, span zero. Let's give this the ID of

8:59

final score. Just like that. And then I'm just going to add a button. And we're going

9:07

to pick this out later, but let's just have play

9:12

again on it. And I'm going to give this the ID of

9:18

restart button. Just like that. Great. Okay. So,

9:24

that is our index. HTML file. We are really kind of done here. Let's move

9:30

over to the styles next because at the moment if we look at this. So I'm just

9:35

going to get this up and then we copy the path. Copy the absolute path and

9:43

paste it like so. This is what our project looks like right now just with the HTML and no styling. So let's style

9:49

it up. So let's go back here. I'm just going to minimize this and let's goes to

9:55

our styles.css CSS file and let's start picking stuff out by classes and ids.

10:01

First off, however, I'm just going to grab the whole body. So, the all whole body holding everything in. Let's just

10:08

get rid of any default margin and padding just in case. And then background. Well,

10:17

I'm going to give this a linear gradient actually. So linear gradient, this is a CSS function to bottom as I want the

10:25

gradient to go from top to bottom. And then I've already picked out two colors.

10:30

Okay, so I want to go from this color to this color. And you get a nice preview of them there. So that is good. We just

10:37

need a comma there as well. So all three have a comma in between them. The font

10:42

family that we're going to use is courier new. And as a backup, I'm just going to

10:48

have monospace. Great. And finally, I'm going to use

10:54

display flex to initiate flexbox so that we can

10:59

say that everything inside is going to have a flex selection of column. So any

11:04

elements inside the body are going to essentially stack up like a column. And then I'm going to also use align items

11:11

center to make sure that they're centered. Let's also set the height to

11:16

be 100 viewport height in order for all of this to essentially work. Okay, so

11:24

now it should all look like that. As you can see, it's centered. It's in a column and it looks pretty good so far. Let's

11:31

continue. Now let's pick out the class name of game container. So to pick out a

11:37

class name, you know what to do. It's dot game container. And I'm going to go

11:43

with background white. Or if you want to keep everything consistent, let's use

11:48

hex. So the hex color code for this would be fff as a shortand of fff six

11:54

times. So you could just put three fs if you want. And then I'm going to give this a border of three pixels. So three

12:02

pixel wide border that is solid, not dotted or anything like that. And black. So again, that's the short hand for

12:08

writing black, which in full is three zeros. But that is the short hand. So it will be the same. Next, let's look at

12:15

the border radius. So the border radius I'm going to just round off to be slightly curved to be 10 pixels. Now

12:23

let's pad it out. So I'm just going to give a padding of 10 pixels as well. And a box shadow. Okay. So a box shadow of

12:31

zero on the x axis, four pixels on the y axis, x pixels spread. And then let's

12:38

also make it black. So at the moment it looks like this. It's very dark.

12:44

However, we can make it transparent. I'm just going to inspect that. Okay. So there we are. You can also see all the

12:51

elements here. So at the moment we're starting the game container. If I remove the padding, it will look like that. But we added the padding. And I could just

12:58

go ahead and remove things if I want. But I think that looks good for now.

13:04

Great. I'm happy with this. Let's continue.

13:09

Next. Let's style the element with the class name of UI. So that is this one

13:15

here. And all I'm going to do is just use display flex once more. I'm going to use justify

13:23

content space between. So that just means that if we look here

13:29

now and look at the UI, you will see all that lovely spacing in between. Uh if

13:35

you get rid of it, it looks like that. But if you keep it there, it's spaced out nicely. And that's thanks to display

13:41

flex. You can't use this without display flex. Okay, great. Let's continue. Now,

13:48

I'm also maybe just going to give it a bit of a margin, but only at the bottom. So, you can do so like this. Let's go

13:54

with 10 pixels. And then also, I'm going to change the font size of this to be 18

13:59

pixels. And the font weight

14:07

just to make it a little bit thicker is bold. Okay. I'm also going to apply a

14:13

min width to it which is going to be 800 pixels. Meaning that even if you kind of move it

14:19

across, let's just refresh this. It will stop at 800. It won't

14:25

move this across. The minimum width will always be 800 pixels. Great. And that is

14:31

again of the UI element right there. Let's continue.

14:38

Now it's time to pick out the game area. So this time we're picking out an

14:43

element by its ID. So to do that I'm going to use hash. And that is how we

14:48

pick out the game area. Okay. So just like that. Now, the reason I've chosen

14:55

to use an ID for this is that we're actually going to be picking this out in JavaScript, too. So, it kind of needs to

15:02

be unique. Same for this div right here. Whereas, this is more to do with styling

15:07

and how it looks. Okay, wonderful. So, the game area itself, well, I'm going to

15:14

go ahead and give it a width of 800 pixels, a height of 400 pixels, a

15:22

background. Once again, I'm going to make it a linear gradient to bottom, and

15:29

then I've already picked out all the colors. We're going to have a sky, so I'm going

15:37

to go with this. And then we're gonna have

15:43

the green for the floor. But if you look at

15:50

it at the moment, I'm just going to refresh. It looks like this. If we want a hard kind of stop and a chop, well,

15:58

what we'd have to do is just add some other things in here. So, it's blue up

16:04

to 80%, but then at the same time, 80% I want to turn it to green. So, that

16:10

should fix it. Great. I love that. That's a little hack for you. Okay. Now,

16:17

the border of this again, I'm going to put two pixels solid. And this time,

16:24

let's go with that right there. So, a little bit off

16:29

black. And now we get a nice border to our game area. So, this is looking good.

16:35

and any overflow if there ever is any. I want it to be hidden. Okay, now let's

16:41

look at the players. Well, Mario, we're going to pick him out by his ID in JavaScript, but also I'm just going to

16:46

style him up to have a width of 20 pixels, a height of 20 pixels, a

16:53

background image. So, I actually made this game a long time ago using a

16:59

framework, which I don't want to use today. So, I've already uploaded all the images we need. Please go ahead and take

17:05

mine. You can take them from the code that I share later. So that is my little Mario which is in fact 20 x 20 pixels

17:11

wide. So this image is that uh and then I think that should be fine. But just in

17:19

case I'm going to give a z index of 100 to make sure that it's in front of everything. And for that we need to put

17:26

a position of absolute. Great.

17:31

So, let's see if our Mario shows up. And there he is at the moment. Wonderful.

17:38

Now, we also need something for when Mario is big. We haven't added this in

17:43

our HTML. This is something that we will do in JavaScript. So, if an element has

17:48

the ID of Mario and the class name of big on the same element, well, then

17:54

let's make him big, right? Okay, let's maybe make him 40

18:00

by 40 pixels so it's kind of obvious. And the background image that we're

18:05

going to use is actually the same. So let's go ahead and get this URL right

18:12

here. Great. Now let's get anything with a class name of platform. Okay, because

18:20

there could be a few. And once again, we're going to have a background image

18:25

for this. It's going to be a URL and it's one that I have previously uploaded

18:30

of a platform. So, here it is. Please go ahead and take mine once again. I'm

18:37

going to put width and height 20 pixels actually. So, all the elements are kind

18:43

of just going to be the same to be honest. So, there's the platform. I kind

18:48

of want to differentiate if the platform is on the ground. And I want to use a different background image. So I'm just

18:54

going to copy all this and just delete that. And the background image for this

19:01

will be here. It's kind of hard for us to obviously

19:06

imagine this because we haven't built this so far, but just know that there will be platforms of different types. So

19:15

in fact, maybe it's best to just copy all of this. And I'm going to delete that class name. And delete that image.

19:22

Okay. So that is kind of our template. And when the platform is also blue, so it has the class name, the element,

19:29

sorry, has the class name of platform and the class name of blue. Well, then we're going to have a different URL. So

19:36

the blue platform is essentially this image right here. Now let's also

19:44

create a enemy. So once again, I'm just going to make the enemy 20 by 20. And

19:50

the URL for the enemy is going to be

19:55

this. We're also going to have an enemy that is purple for other levels. And so the

20:04

URLs of this is going to be a little bit different. Here we go. And we're also

20:09

going to have a coin because, you know, Mario uses the coin. So once again, let's just copy this. Everything's 20 by

20:17

20 pixels, but the image for this is of a cute little coin. I can't wait to show

20:22

you. And for this one, I'm actually going to animate it as well. So animation

20:30

coin spin is something that we're going to write. It's going to last for one

20:35

second. It's going to be linear and it's going to be infinite.

20:42

Okay. So now to define the animation, we're going to need key frames

20:47

and then we're going to get the animation name. So I chose to call this coin spin. And at 0% start of the

20:55

animation because you know an animation runs from 0 to 100, we're going to transform

21:02

just rotate on the Y axis by 0 degrees. And by the

21:09

time it's reached a full animation, so the full thing, I'm going to run

21:16

transform rotate by y-axis 360°. So it's going to

21:21

literally rotate on the y- axis by 360°. Great. And that should have an L there,

21:27

too. Okay, we are nearly done. Once again,

21:32

I'm just going to copy this because we need the surprise

21:39

block which again is 20 by 20 and the URL for this is going to be this right

21:46

here. We're going to have a surprise block. However, if it's hit, so if it also has

21:52

the class name of hit and on this occasion, it's going to be

21:59

this right here. If you are curious what these look like, you can of course just paste them into the browser like so. So,

22:05

here it is. Here is my little block that I made ages ago in 2021. In fact, there

22:12

we go. That's what it looks like. Cool. We also need the pipe. So, the pipe,

22:19

this is going to be again made of the pipe top actually. So, let's

22:29

have all the pipe styles. Then we're going to have the pipe top,

22:36

right? The pipe bottom, and the pipe bottom,

22:43

right? Okay. So, now I could just quickly add all those in. So, the pipe

22:51

top, the pipe top, right,

22:58

the pipe top bottom.

23:04

and the pipe bottom. Right, we should also

23:10

maybe encapsulate all of these in one pipe element too. So for this, I'm just

23:16

going to say that the width of the whole pipe is going to be 40 pixels and the height of it is going to be

23:24

I guess 40 pixels too, right? Because if we have these two stacked up, that

23:29

should be good. We can always come back to this if we need. So now all the pipe

23:35

elements we're going to encapsulate in one whole pipe element that we will give the class name of pipe to. Great. We

23:43

will be coming back to this file once we actually start working with it. I just want to get all the images here in one

23:48

place. Next, I'm just going to grab the controls. So simply the controls that we

23:55

have here. So this div with the class name of controls and I'm just going to

24:01

give it a margin top 15 pixels to space it out. I'm going to align the text in

24:08

the center. And let's just change the font size of this to be 14 pixels like so. So that

24:16

will now affect the controls here. And they have I think I'm kind of happy with

24:22

this. Now next let's also pick out this whole element the game over element by

24:30

the class name. So dot game overelement.

24:35

Uh and I'm going to give this the position of absolute. So now if we save that it will go from here to here.

24:42

Right? It's absolute meaning it kind of overrides everything and appears on top of everything. And if I want to position

24:48

it 50% from the top, all I would do is go top 50%.

24:54

Just like that. And it will appear 50% from there as the whole element. Right?

25:02

So it's here at the moment. I'm also going to make it have a border of three

25:09

pixels solid. Let's go with white.

25:15

Okay. So there we go. Let's maybe go ahead and

25:22

give it a background. We can go with something transparent if

25:28

we want. So black is this. And by adding the A, we're giving it a 0.9

25:35

opacity. So that's what it looks like. Let's also change the text color to be

25:41

white. So, hash FFF. There we go. Let's pad it out a little

25:49

bit. So, padding 30 pixels. Border

25:54

radius 10 pixels. Text align center.

26:02

And let's just make sure it's always always in the front. So, I'm going to go Z index a th00and. Really kind of go

26:08

overboard to make sure that it does. Okay. So now with that it will look like

26:14

this. And you can change the opacity if you want. If you go 0.5

26:21

it will appear a little bit more opaque. So it's really up to you where you want to take this project. Okay. Great. We

26:30

can also style the button. So perhaps let's do that. I'm just going to grab

26:36

the button. So this button right here with the ID restart button. So

26:43

restart button. Let's give it

26:49

a background color of hash FF6

26:55

B6B. Let's make the color of the font

27:01

white. I'm going to go with border none.

27:09

Border radius

27:15

5 pixels. Font size 16 pixels.

27:23

Margin 10 pixels. Just move that up.

27:31

So that looks much better. I'm happy with that. We can also add an

27:38

on hover effect. So, if we want to change what this looks like if we hover over the button, we can change the

27:45

background color if we wish maybe to

27:50

FF52 52. So, kind of similar just more.

27:57

There we go. Red. I love it. Okay, so that's what it looks like at the moment.

28:04

Now, because we will be displaying this game over popup in JavaScript, you can

28:10

pick things out by the class name. However, it's best to keep this an ID and keep it unique. So, I'm actually

28:17

going to go ahead and change this right now to an ID. Okay, so giving this whole

28:24

div the ID of game over, meaning that we need to change it here as well to an ID.

28:30

So just change it to a hash. Visually that won't change but for us moving on

28:35

it makes more sense when we start working with JavaScript. I love it. Let's do it. Okay. So we will be coming

28:43

back to this page as there's a few more things I will be adding. However, for now let's move on to the JavaScript.

28:50

Okay. Great. Okay. So first off I'm just going to start with the game constants.

28:56

This is going to be things like gravity, jump force, move speed, and enemy speed. So, const gravity

29:03

0.5 const jump force

29:10

equals -12. We can always change these later if you want to increase the gravity, increase the jump force, or

29:16

increase the move speed. Let's make this 2.5. And then we're going to have enemy

29:25

speed as well and make this a one. So those

29:30

are our constants. Okay, that is something that we use throughout the game and it won't change. Now let's

29:37

write some game state variables. So let game state this will change. This will

29:44

continues to be updated. So, the game state is going to be an object that's going to hold a bunch of information

29:50

such as our score, which at the moment is going to be zero, the level, which at the moment we're going to start off with

29:57

one, and the lies, which we're going to start off with being three. Next, we're

30:02

going to put game running as true because the game is running. The keys,

30:09

which we will come back to, this is essentially the keys that you're going to be pressing on your keyboard. So once

30:15

again the game state is just one object that looks like this and essentially is our entire game state for the Mario

30:22

game. Now let's define the player object. So let player and once again

30:29

this is going to be like I said an object and first of all let's actually

30:35

have the element and we're going to assign something to the element value. We're going to go into our document. So

30:42

into our document here kind of essentially and we're going to use a JavaScript method called get element by

30:49

ID and we're going to look for the element in here with the ID of Mario. So

30:56

we're going to grab that whole element by its ID Mario. Okay, making sure to spell it

31:02

exactly the same. So now that whole div has been assigned to element which lives in the player object. Great. Let's also

31:11

here assign it what we want the x value of the player to be on the game board.

31:16

We want it to be 50. The y we want to be 300. So x is this way. Y is that way.

31:23

We're also going to define the width here which we know is 20 pixels. But for the JavaScript it doesn't know that. So

31:30

we're going to go height and width 20. And then we're also going to have something called velocity. So the

31:37

traveling velocity of x the x axis which we're going to start off being zero and

31:43

also velocity y which is also going to start off being zero. We'll start off with being

31:49

grounded as false because the player is going to drop from the sky big. He's

31:56

also going to not be big. So let's start off with false. And there's something called the big timer because if you've

32:02

ever played Mario, you should know that the bigness of Mario does run out. Okay,

32:08

great. So that is everything assigned to the player object. Great. Let's move on

32:15

to the game objects arrays. So let game objects and this is again

32:23

going to be an object and in it it's going to hold the game's platforms. It's

32:29

going to be an empty array for now. The enemies which is going to be an empty array for now. All the coins in the

32:37

game. So here they go. Obviously empty for now. The surprise

32:43

blocks again empty for now.

32:50

and the pipes empty for now, but essentially that's what we're going to be working with in

32:56

this game. Let's make sure to spell surprise correctly.

33:01

Great. Okay, we're only going to have two levels in this game. Uh you can

33:08

after this I should show you how to make two levels. You can actually go ahead and add as many as you want. So const

33:14

levels equals uh and it's going to be array and the first object is going to be our first

33:21

level. So in fact maybe let's go ahead and write that level

33:27

one and level one consists of platforms. So

33:33

exactly like we saw up here, we're now going to actually build this out. So this is going to be an array and my

33:40

platforms are going to exist on we're just going to define where. So on

33:45

the x axis zero, yaxis 360. We're positioning the platforms. Essentially,

33:51

we're going to say this platform has a width of 400 and a height of 40.

33:59

And the type of platform this is is going to be a ground platform. Okay, so this makes

34:05

sense why here we wrote platform ground.

34:15

And next we're going to have the same thing. So once again, I'm actually just going to copy this and we're going to

34:21

essentially just change these values here. So let's have one.

34:29

Let's have a total of five. Three, four, five. X is going to here be 500. Yaxis

34:36

is going to be 360. The width of this is going to be 300. And the height of this

34:43

is going to be 40 again. And let's make this a ground platform. The next

34:48

platform, we're going to have a floating one. So, this is going to be 200. This,

34:53

let's put it 280. We can always change these after we've done it. This is kind of already been planned out by me. Uh,

35:00

and the floating one. Visually, it looks the same as the ground one, but for us, it's best to kind of differentiate

35:08

these. But however, it does mean that I'm just going to copy this and put floating here. Even though it

35:16

looks exactly the same, we're going to have different class names for it so we can differentiate them in JavaScript.

35:23

Okay, great. 300, Y 240, width

35:30

60, height 20. Again, this is going to be a floating

35:37

platform. X 600, Y 280,

35:43

width is going to be 80, height is going to be 20, and this again is going to be

35:49

a floating platform. So once again, in level one, I've just mapped out exactly where on that level every platform is

35:55

going to be. Next, let's map out the, you guessed it, the enemies. So let's

36:01

copy that and let's put our enemies in here. Each enemy is going to be an object. It is going to have an xaxis

36:07

once more. So 250 yaxis is going to be 344

36:13

and then the type of this enemy is just going to be the normal enemy. In fact,

36:18

I'm just going to define it as brown. So in fact maybe here also where the

36:25

enemies just to make our lives a little bit more clear. Enemy

36:31

brown will show up as this and enemy purple will show up as that. So that's

36:36

one enemy on level one. Let's just have two. I'm just going to change this to be

36:43

550 this time and keep the y axis the same. Next, you guessed it. Let's look

36:48

at coins. Uh, and what kind of coins are we going to have

36:55

for this? Because there's just one type of coin really. Let's just have an x axis and a y- axis. So that's where my

37:03

first coin is going to appear. And then let's have maybe three total. So

37:10

one, two, three. Let's change this to be 320 and this to be 620.

37:17

Uh, just going to move them around like that. What's next? The surprise blocks. So, in

37:26

fact, maybe let's just copy those two things. And there we go. The surprise

37:33

blocks. I'm just going to have one here. It's going to show up on x-axis 350,

37:38

yaxis 220. Uh, the type of this. So, I'm just going to define what's going to pop up inside. It's going to be a mushroom.

37:47

And the pipe is going to show up at 750

37:53

300. Great. We haven't added the image of mushroom to our style CSS file, but

38:00

we will do that later. Okay. So that is one level. Let's do another one. So

38:05

whenever the object finishes, we're going to put a comma and essentially define level

38:12

two. So once again, it's an object. And in fact, we could just go ahead and copy all this because that is exactly what

38:18

we're going to be filling out. Okay, so let's do it once again. Let's go X,

38:28

Y, width,

38:34

height, and type.

38:40

Okay, so all the types on level two of the platform are going to be blue. And

38:46

let's go ahead and have nine, two, three, four, five, 6, 7, 8, 9.

38:58

Uh, I have already pre-planned this for you. So if you don't like how this

39:03

looks, you can always change the position of everything that you see here. So maybe it was easier for me to

39:09

copy here. I am just copying from a pre-made sheet, but I have all these already stored because this took a lot

39:16

of time to plan out. I'm essentially just saving you a bunch of time by giving you a kind of already drawn out

39:22

level. But like I said, if you want to add more levels, please go ahead and do

39:27

so. Okay. In fact, let's just have eight

39:34

here. Okay, I'm going to go 360

39:42

280 260

39:48

240 280 200

39:54

200 40 40

40:01

40 60 Height 40

40:10

20 20 20 20.

40:18

Great. Now for the enemies. So once more, we're going to have X Y

40:26

type and then let's have three enemies on this level. So I'm going to have one

40:32

at 350 3 44 type purple this time. Xaxis

40:38

is going to be 650 344. Once again, purple and X here. 570

40:49

264 purple.

40:54

Okay. So, there we go. Next, let's also do the coins. So, in fact, maybe I'll

41:01

just copy and paste all these coins here to save us time. Those are the coin

41:06

areas that I picked out. And this time, we're going to have two surprise blocks.

41:12

One is going to have a coin and one is going to have a mushroom and one pipe only. So here is the location of that

41:19

pipe. Great. So those are two levels. Let's move on. I think it's time to

41:25

actually show everything we've done so far. So I'm going to initialize.

41:31

Okay, great. So function init

41:36

game. That is my function. I'm going to write

41:41

another function called load level. And I'm going to look into the game state.

41:47

Get the level. Okay. So, if we go into game state right here, game state.le

41:56

because there's an object. So, we go into the object by adding a dot to get any of these properties. Games state.le

42:02

will give us the value one. Okay, the integer value of one. And then I'm also

42:10

going to write another function to game loop. Cool. So let's go ahead and write

42:15

the function load level next.

42:21

So function load level. And then whatever we pass into here, I'm

42:26

just going to go ahead and call it level index.

42:32

So in this case, we're passing through a one. This is just a little helper that shows

42:38

up which we can disable. Now, first off, I just want to check that if the level index is larger than

42:47

or equal to the levels length. Now, there is a bit of an issue

42:53

here because imagine we are level two, right? Well, we only have two levels in

42:59

our levels array. So technically if we pass through a two that does equal the

43:04

length of the levels array which is two which would mean it would be a game over. That is not what we want. So

43:11

actually what I'm going to do is just add a minus1

43:16

to the level index. So it starts counting at zero. However, let's go ahead and do

43:22

that all the way here in the load level function. So that level index is actually being passed through as a zero

43:28

now. So if it goes to one, which is essentially level two, it is still lower

43:34

than the level's length. So we will be good to play. And if it of course goes over, then it will show a game over.

43:41

Okay. So hopefully that makes sense. Now if we are below that and so essentially

43:50

level index does not go over the level's length, we are happy to play the game. I'm just going to start off by clearing

43:58

the existing objects.

44:06

I'm going to write another function called clear level which we have not

44:11

written yet. So I'm just going to comment that out for now. And now let's

44:18

actually get the level. So con level, let's define it. We're going to go into our levels array

44:27

and we're going to pass through the level index. So if it's a zero, we'll get back the first item and assign it to

44:34

level. Next, I'm also going to define the game area. And for this, we're going

44:40

to go into our document into our whole document here and get the element with

44:47

the ID of game area. So let's use get element

44:54

by ID and pass through the game area. Easy.

45:00

Okay. So now I'm going to also reset the player. So wherever the player is, our

45:06

little Mario, I'm going to get the player and get its xaxis because as we know player is an object that we wrote

45:13

above and I'm going to assign it to 50. Let's also get y and assign it 300.

45:21

Player velocity X is going to be zero.

45:31

Player velocity Y is going to be zero.

45:38

Player big is going to be false. We're starting off as small. player big timer

45:47

will also start off as being zero. Great.

45:53

Let's also clear any class names that might be assigned to the player. So, for

45:58

example, if he's currently big class name, it's going to clear it to be

46:04

an empty string. And we're going to update element position. This is a function

46:11

that we are yet to write. And we'll just pass through the player

46:16

uh just a few things actually. So the player element

46:22

the player xaxis and the player yaxis. So again this is a

46:29

function that we are yet to write. So I'm just going to comment that out for

46:34

now. Let's move on. So outside of this function, I'm going

46:41

write another function called, you guessed it, update element position in which you pass through the element, the

46:49

x-axis and the y axis. And I'm just going to grab the element style

46:56

left and just use x plus px as we are working

47:02

in pixels. So we're getting that element essentially our Mario and using style to manipulate the style position left

47:12

and we're passing through whatever that is. So in this case it's 50 50 pixels. We're also going to have to add a

47:19

position of absolute to Mario. So let's find Mario here. That's already been

47:24

done for us because otherwise we would not be able to add left, right, top and bottom as we are doing now. We've just

47:31

added left. And then also let's do top. Okay. And for this it's white.

47:40

Great. So we've now written that. Let's uncomment it out here. I'm going to keep

47:47

that like that. Okay. So let's give it a go. We are going to have to call this function of

47:54

course. So let's start game.

48:01

And I'm just going to call that and let's see what happens. Okay, so wonderful. Game loop is not defined.

48:08

That is fine. Let's just go ahead and comment out game loop for now. What

48:14

we've essentially done is assign an x and a yaxis to Mario. Okay, it is

48:20

absolute. So, it's taking in the whole page into account. However, we just want this in the game area. So, let's go back

48:30

to styles. Let's find our game area. And I'm going to add a position of relative

48:40

to the game area, meaning that Mario will now appear inside that game area

48:47

50 pixels and 300 pixels from the top. Great. So, we've essentially kind of

48:54

started our game by placing Mario exactly where he is. Wonderful. I love

49:01

it. Let's continue. So, let's go back to this file right here. Okay. Now, let's

49:08

continue by going back into this function. So, the load level function.

49:13

And what I'm going to do is actually create the platforms for this layer. So create platforms

49:22

just like so. And I'm going to get the level. And from

49:27

the level I'm going to get the platforms. And for each platform

49:37

that lives inside of that array, I'm going to

49:44

essentially create an element out that data.

49:50

So maybe let's call it this platform data instead as it is just a bunch of

49:55

data and let's define an actual platform. So remember the data, it had

50:00

the X and the Y and the width and the height. We're actually going to use that. We're going to write a function called create element and we're going to

50:06

create a div. And that div is also going to have the class name of platform. And

50:14

we're going to go into that platform data once more to get the type. Okay. So

50:23

whether it was ground, whether it was floating, that is going to be assigned right here. Okay. And the other thing

50:31

that we're going to have to pass through is this whole object. And we're going to assign a left side. So that once more

50:38

we're going to get that platform data and get X from it and add just the px

50:46

string for pixels. And let's do the same for the top width and height. So top

50:57

width and height. So platform data Y platform

51:02

data width and platform data height.

51:09

Next, let's grab the game area that we picked out previously and use the method

51:15

of append child to actually add an element inside of it. And we're going to grab that whole platform and just whack

51:23

it in. Okay, for each platform. And finally, we're going to save that to

51:29

the game objects. Platforms.

51:37

We're going to push that into the array. We're going to push in this whole object and it's just going to be the element

51:45

which is going to have the value of the platform itself so we can keep track of it. And we're going to also just spill

51:53

out any other platform data that we have. And let's also add an ID to this.

51:59

I'm just going to put the string of platform in front of it and add the index number. So it be like platform

52:05

one, platform two, platform three starting from platform zero of course.

52:10

Okay. So let's define that function create element. So once again just do it outside. I'm just going to do it here.

52:18

Function create element. And of course we pass through the type the class name

52:27

and the styles as an object. So with the element that we pass

52:34

through, we're going to use document and create element.

52:39

This we did not write. This already exists from the document API. We're going to create a div. Okay, so we're

52:46

creating a div in the document and assigning it to the const element. So now let's grab that element

52:53

and let's assign that class name which we have passed through into this

52:58

function. And then I'm just going to use object assign

53:04

and assign it the elements style

53:12

with the styles we passed through and then we're just going to return the element itself. Okay, so that is our

53:18

function create element which we wrote. Okay, this is not the one from the

53:23

document API. This is the one from the document API. You can see we're getting the document object and getting it from

53:30

there. Great. So now if we look in here, we have created all those platforms.

53:37

However, they're all bunched up here. Why is that? It's because we need to add position absolute to them just like we

53:45

have with Mario. So, back in here where we have the platforms,

53:51

we're just going to add position absolute.

53:57

Okay, for here for this platform, for the floating platform, and the platform

54:03

blue. Okay, so now, tada. There we have it. We have just positioned all of them

54:10

on our game board. This is looking great.

54:15

Let's go back to creating our game. I think next let's write our show game

54:23

over function. So I'm just going to uncomment that for now. And once again at the bottom here. So outside that

54:29

function let's write function show game over

54:35

just like so. And this is going to be easy. We're just going to grab the game state. We have

54:43

the game running property on that object. We're going to set it to false.

54:51

We're going to get the document get element

54:57

by ID. Uh so let's search for the game over

55:04

title. Let's change the text content

55:10

of it to be well based on if we won or not. So this is going to be a variable

55:15

if we won. I'm going to put congratulations,

55:25

you won. Otherwise, I'm just going to put game over. Okay, so great. Of course, this is

55:33

a variable we're going to have to define. Some other things that we're going to do is I'm going to do get

55:39

element by ID. This time, let's get the final

55:46

score. And once again, get the text

55:51

content. Uh, and we're just going to get the game

55:56

state score. Finally also get the

56:02

game over style display

56:10

block so that we can actually see it. Okay. And the variable one we're going

56:15

to actually pass that through into the show game over which means that here

56:21

shame show game over is true. We'll actually see that element. So let's hide it as a default.

56:29

So, back in here, let's find game over.

56:34

And I'm just going to put display none for now. Okay. So, as a default, it

56:42

should be hidden. And if we lose, we will display it. Great. The other thing

56:47

we need to do is write the function clear level. So, once again, let's go ahead and define define that here.

56:54

Function clear level. All I'm going to do is once again get the game area, use

57:03

document, get element by ID. Make sure to spell that

57:09

correctly. And let's look for game area.

57:16

And now let's remove all the objects apart from Mario. So objects values

57:24

game objects flat

57:30

for each and then let's get each object

57:36

and if object element

57:44

plus object element parent node

57:51

exists well we just remove it. So, this is kind of like a hack way to keep removing, removing, removing until

57:57

nothing is there anymore. Okay, great. So, in fact, I don't really

58:05

think we need this. We can just do it this way. Let's check that out.

58:10

Okay, so we're going to do that. And then finally, let's clear the array so we can start fresh. So, game objects,

58:20

platforms, let's reset it to an empty array.

58:26

Enemies, let's reset it to an empty array.

58:31

Coins, let's reset it to an empty array. Ser

58:37

rise blocks, let's reset it to an empty array.

58:43

and pipes also. We're clearing everything.

58:49

Okay, great. While we are also here, I'm just going

58:55

to write the input handling. So, essentially how the Mario moves left and

59:02

right and jumps and so on. So, for that, I'm going to get the whole document and listen out to the entire document with

59:08

event listener. and we're going to listen up anytime we press the key down

59:14

on our keyboards. Okay, pass through an event so we know

59:20

which key. So I'm going to also get the game state keys and I'm going to pass

59:27

through the E key code that was pressed.

59:33

Now I'm also going to listen out to any time the key is relieved. And then I'm

59:40

going to put that as false. So we're not pressing down anymore.

59:47

Great. However, if we press key down and we do

59:53

press the space button. So if E key code equals space, we know this is a jump. We

59:58

also want to prevent the default from happening. Okay? Because we're jumping. we don't want to continue moving. Okay,

60:04

so for example, if we're going left and right, we would move left to right continuously unless we press a space and then we prevent that default from

60:10

happening. So that is our input handling. We also need to handle the

60:16

game loop because we want the game to keep looping. So function game loop

60:25

if however there is no game state

60:31

game running. So the game is not running anymore. It is false. We just want to return out of this. Otherwise, let's

60:38

update and request

60:44

animation frame

60:49

game loop. We of course need to write the update

60:54

function as well. The update function is going to be a long one, okay? Because it's essentially

61:01

going to handle all of our game logic. So update game logic

61:08

function update. So there we go.

61:14

Now let's also uncomment the game loop that we put up here

61:20

on the game initialization. Great. So this is what our game looks

61:25

like at the moment. Can't see much, but we've done a lot of

61:30

logic behind the hood. Let's go back to our load level function. Okay, so we're

61:38

clearing the level. We're resetting the player. We're creating platforms. There's a bunch more stuff we need to

61:43

create. We need to create enemies. We need to create coins. And we need to create supplies box. Create pipes as

61:49

well. And the pipe parts. So let's do that now. Okay. So just after we create

61:56

the platforms. So that function finishes here. Still inside the

62:03

load level function, I'm going to

62:08

create enemies. So, I'm going to grab the level enemies

62:15

associated to that level. And for each enemy,

62:22

let's call this enemy data because it is the XY access data and so on. Just going

62:27

to use index so we know which one we are working with.

62:33

And what I'm going to do is define our enemy. And for that we're going to use

62:38

the create element function that we wrote. So we're going to want to make

62:43

sure that this is a div. And then let's pass through the class name of enemy.

62:48

You will notice I'm using back text as we are going to use some code in here. We're going to get the enemy

62:55

data type. So whether it's brown or purple will be passed through into here.

63:01

We'll also then pass through an object that has the left

63:07

and the top pixels. So let's get enemy

63:15

data which is an integer. Okay, stored under X. And we're just going to get

63:23

the string pixels and add it like so. And for the top, we're just going to use

63:30

enemy data Y. Okay. So, now that we've created the enemy, we're going to append

63:36

the enemy in the game area like we did here. So, append

63:42

child pass through that enemy. We're also going to get the game objects.

63:48

get the enemies array. We're going to push in this new

63:54

enemy or each enemy that we create. So, as the element, let's just pass through

64:01

the enemy we created. As the X, we're going to pass through enemy X. That's

64:08

pretty easy. As the Y, we'll pass through enemy data Y.

64:14

The width is going to be 20 pixels. The width is going to be 20. The height

64:21

is going to be 20. We're also going to pass through a direction that we want the enemy to move. I'm going to go minus

64:27

one. A speed which we set above as enemy speed.

64:35

And let's give it ID. So the ID is just going to be the string of enemy dash. And we're going to add

64:44

the index. So starting from zero, we're also going to make the enemy alive true

64:49

to start off with. Great. So let's use the same logic to now create a coin. So

64:56

after this has finished, so just here we're going to create

65:01

coin or coins multiple if there are multiple coins. So once again for the level we

65:09

are working with let's get the coins array and for each coin that exists we're going to get the coin data. So the

65:17

x the y and the so on let's get its index and then once again let's use the create

65:26

element function that we wrote to pass through a div.

65:32

And then I'm just going to pass you the type of coin.

65:38

And as left I'm going to just put coin data X

65:43

and append the string pixels. And then I'm going to also put top

65:52

with coin data Y. Okay, great. And once again, let's get

65:58

the game area. Use append child

66:07

pass through the coin itself. Let's get the game objects coins

66:15

push. So we're pushing into that array this object which is going to

66:20

essentially represent our one coin. So let's get the element pass through the

66:27

coin itself as the x. We're going to just get the coin data

66:34

X. The Y, we're going to get the coin data

66:39

Y. Width is going to be 20. Height is going

66:45

to be 20. Collected, we're going to start off with being false and change it to be true.

66:51

The ID, well, we're going to do coin and then the index.

66:57

Great. So just the surprise box and the pipes next. So same logic once more.

67:06

You'll really get the hang of this soon. Create surprise

67:13

[Music] blocks. Let's get the level and the surprise

67:20

blocks array. And for each block data, let's

67:27

get the index as well. So starting from zero, we're going to use the create

67:33

element function we wrote. So this is great function. It's getting a lot of use. And then let's just pass through

67:40

the string of surprise block just like so.

67:48

And once again, let's pass through the styles. So left is block data X plus pixels,

68:02

top is block data Y plus pixels

68:10

and then game area append child

68:16

the block itself. So it shows up in the game area and then game objects. Let's just update this game objects

68:25

surprise blocks

68:30

push this whole object to represent one supplies block. We'll get the element

68:38

which is the block itself that we defined. The X is the block data.

68:47

X. The Y is the block data Y.

68:54

The width is 20. The height is 20.

69:01

The type is the block data.

69:07

Type. Okay. So, that's going to say coin or mushroom. It's not going to look like a

69:13

coin or mushroom. It just look like a surprise block, however. And let's start off with it being here as false.

69:21

And then let's just give an ID. So I'm just going to go with block plus the

69:26

index. So let's just look at this at the moment. Let's see if we made any

69:32

mistakes. So enemy here. Okay. So this is looking good. So now if

69:38

I refresh this, tada. We get our enemies. We get our coins. We get our surprise block. However, they're just

69:45

stuck in here. Why? Because we need to add position absolute to all of them.

69:51

So, in fact, let's go back up here.

69:57

I'm going to go with enemy here. Enemy here. Coin.

70:03

Surprise block. Surprise block. Hit pipe. Pipe top. Pipe

70:10

top right. Pipe dot bottom. Pipe bottom right. And great. So now if we refresh

70:18

this, they will appear in their correct positions. I love it. This is looking so

70:24

so good. Now let's do the pipes. So once again,

70:30

right after this, but still in the same load level function, let's create pipes.

70:39

Once again, level. So let's get the pipes associated with that level. And for each

70:46

pipe data, so the X, the Y, and so on, let's also grab the index.

70:53

I'm going to use the create element

70:59

function and just pass through that I want to create a div and I want to give

71:04

it the type of pipe and these are the styles I want to add. So left is going to have pipe data X plus pixels and top

71:15

is going to have pipe data Y. So that's for the whole pipe itself.

71:21

However, we need to also create the pipe parts. So

71:27

still inside this function, let's define pipe top left equals and create element

71:36

once more. This time just going to pass through that. I want to create a div and pipe

71:42

top. It's going to be the type. And then let's do pipe top

71:51

right. Pipe bottom left

71:58

and pipe bottom right. Okay. So pipe top

72:08

right pipe bottom is the

72:16

style name and pipe bottom. Right.

72:23

Great. So now let's append this all to the pipe. So, let's grab the pipe itself

72:29

that we created above. And I'm going to use append

72:34

child to put in the pipe top left.

72:41

We can just use a pend in which we can just pass them all through. Pipe top

72:47

right, pipe bottom left, and pipe

72:53

bottom right. And once that pipe is created, we get the game area and we use

73:00

append child. This is just one to append that whole pipe with all of its four

73:06

children. Okay. So it will look like that.

73:14

So there's our pipe top, pipe top right, pipe bottom, pipe bottom right, all in

73:20

one space. all as children. We're going to have to space them out a little bit.

73:26

But for now, let's also get the game object pipes and push

73:36

the element

73:41

pipe. X is going to be pipe data. X Y is going

73:49

to be pipe data Y width is going to be

73:56

40. Height is going to be 40 and the ID

74:03

is going to be pipe plus the index value. Great. We have

74:11

finally finished our load level function. I love it. Let's get to

74:16

spacing these out a little bit more. Okay, so back in here, we're going to

74:23

have to assign a top and left value for inside the pipe itself. So everything

74:30

lives in the pipe. But with position absolute, we can actually position the pipe top, pipe top right, pipe bottom,

74:37

or pipe bottom right inside the pipe itself. So that's why I'm going to say top and left zero for pipe top, top zero

74:45

and right zero for pipe top right. Then we're going to have pipe bottom as

74:52

bottom zero left zero and also bottom right as bottom zero right zero. Okay.

74:59

And that simply means that it will position out the pipes so they look more like this. Just going to get this image

75:07

to see what that looks like. Okay, so it is cutting off a bit of the image. Pipe

75:13

top, right? I'm just going to increase that top to top

75:19

320. I think it should be a bit lower. So, let's go ahead and do that. So, here,

75:27

where do we add that? Well, it's going to be in the levels. So, where is the

75:33

pipes? Let's change that to 320. And it will probably be the same for the second

75:38

level, too. So, I'm just going to change that to 320 as well. Great. And that

75:44

just means that now it will show up in the right place. I love it. Same for Mario. We can also change where he shows

75:53

up. So, maybe let's go with Y 320.

75:59

And same for here. When we reset the player, it

76:04

should be 322. Okay, so that's where he will be. We probably need to go even lower than

76:10

that. So, player Y 340. And once again, at the top, player Y 340 as well. So,

76:18

tada. Now he's on the floor. So, if you never need to position anything, that's how you do it. Great. I love it. Let's

76:26

continue. Okay. So, now let's write some game logic. So, let's go all the way

76:32

down here and once again work on the game logic function. So, first off,

76:38

we're going to handle the inputs. We're going to support simultaneous movement and jumping. So, we're going to use the

76:45

game state keys for this that have been saved. if and then we're going to look

76:50

into the game state object that we have saved above and look at the keys

76:56

property and if the keys property contains arrow left so us moving left or

77:06

key A because we're going to do the WD movements as well so you can move with

77:13

your arrow keys or WD so either one of

77:18

those we are moving left. We're going to get the player and

77:24

velocity x equals

77:30

minus the move speed. Okay, because we are going left. Else

77:38

if and I'm simply just going to copy this. I'm going to go arrow right. And then

77:46

the key D is for moving right. Then we're going to get the player velocity,

77:52

but just move at the move speed. Okay, so we've done left and right. Else

77:58

there's just friction. Okay, so we're going to apply friction and friction is going to be 0.8. Okay, but it's going to

78:07

multiply. So it's going to get harder and harder to move. Just like that. Friction. Wonderful. So, we've just used

78:14

the multiply operator here, too. Next, let's handle the jumping. So, this

78:20

handles left and right. And now we're going to

78:28

handle jumping. So, once again, still in this function,

78:34

handle jumping. Okay. And what we're going to do is say

78:41

if and once again game state keys equals

78:46

the space bar. So if spacebar is in there and we have to check if the player

78:52

is grounded because we don't want to jump mid jump. So we're checking if the player is grounded. So if both those

78:58

things are true, we're going to get the player velocity

79:03

y this time. and we're just going to assign the jump force constant that we

79:11

assigned at the top of our file. Okay, so the player obviously will not be

79:18

grounded anymore. So we're going to also assign false because he's in the air.

79:23

And I'm also going to apply gravity. So apply gravity

79:28

if player is not grounded. So he's in the air. player

79:38

velocity y is going to increment. I'm going to

79:44

increase it with gravity. So that's the increment operator right there. We're just adding

79:50

gravity to it. Okay? And that's going to happen over and over again. Okay? So it will appear that gravity is impacting

79:58

the jump. So in the game loop, that is where the update function is being

80:03

called. And then in initializing the game. So all the way here the game loop

80:10

is being called and down here

80:15

the initialize game function is being called. Great.

80:23

So now let's just go console logs out so we can see what's going on under the hood. And here just going to console log

80:31

out the game keys so you can see which game keys are being pressed and refresh.

80:36

So as you can see if you press on the arrow left it will say left and if you let go okay at the moment it's true

80:43

because I'm pressing down on it. If you let go it will say false. Okay. And do it for the other side too. Okay. So it's

80:51

just collecting every single arrow press you do. If you click the space okay it's true. As soon as you let go, it'll be

80:57

false. So, we're just collecting all the arrows there. Great. And that is constant. Wonderful. Let's continue. So,

81:06

now we actually need to make Mario appear as if he's moving. And to do

81:11

that, well, we're going to have to use the update element position function that we wrote. That will update the

81:17

player element position and size. And we're going to just pass through the player element. Okay. and then the

81:24

player xaxis and the player yaxis too. So make sure again that is in the update

81:31

function after we've written all these if statements. Now if I refresh

81:40

uh he's still not moving. Why is that? Let's have a look. Uh because we're just

81:46

passing through the player element, the player x and the player yaxis. we've actually updated the velocity which then

81:53

needs to translate to the player position. So whatever the player x

81:59

position was, we add the player

82:05

velocity x and we do the same for the y ais. So

82:13

whatever y was, we've add the velocity y to it. And this will essentially update

82:20

player position. Okay. So now let's have a look.

82:27

Let's refresh. And Mario seems to be dropping somewhere down there. Whoa, there he goes. Let's

82:35

inspect where he went. So here is our game container. Here is our game area.

82:43

There is Mario. Okay, he's dropping from the top. He's dropping and dropping and

82:48

dropping. We need to essentially ground him somewhere. So add a platform

82:54

collision. So let's maybe go back and work on that first.

83:00

So let's say platform collision.

83:06

Uh and then let's do it for every platform. So let platform

83:12

of game object platforms. So we're saying for each

83:18

platform in the game object platforms. Okay. So this is just an loop that we

83:24

are doing if check collision. So this is a function

83:30

that we are going to write. I'm going to pass through the player. I'm going to pass through the platform. Okay. So

83:37

we're looping. We're looping. We're checking if every single platform that exists against our one player. And if

83:43

that is true, we do something. So let's write the check collision function.

83:49

Let's do so outside of this game. So here perhaps function check collision.

83:56

We'll pass through rectangle one and rectangle two because it can literally be any rectangle. We'll pass through

84:06

element one and element two because it could literally be anything. And we're

84:11

just going to check the x coordinate of the elements and the width as well as

84:18

the ycoordinate and the height to make sure that areas of the two elements

84:24

overlap. So we're going to return element 1 x and if it is smaller than element 2

84:33

x plus element 2 width and that is true plus element 1

84:42

x plus element one width is larger than

84:48

element 2 x. So if that statement is true and this statement is true

84:57

and let's do the yaxis now as well. So let's check element one. Y is smaller

85:04

than element 2 Y plus element 2 height

85:12

plus element one Y plus element one height is

85:20

bigger than element 2 Y. So again we are

85:25

checking if all of these statements are true we know there is a platform

85:30

collision that has occurred. Okay. So if platform collision has occurred well

85:38

what do we want to do? Check first if the player is falling. So player

85:43

velocity y is larger than zero. This means that indeed

85:51

they are falling.

85:57

So we want to stop them from falling essentially. So player Y, we're going to have to override with platform

86:05

Y so that the player stops on the platform. But then we're also going to minus the player height because

86:12

otherwise the player and the platform will appear on top of each other. No, we want the player to essentially appear

86:19

above it. That's why I'm also subtracting the player height. Now,

86:25

we're going to get the player velocity Y and just reset it

86:32

to zero. Okay. And then we're going to add player grounded else true. Okay. So that the player will

86:40

stop. So now this should fix our problem of the player

86:46

falling through. And there we go. So once again, if I

86:52

refresh, Mario doesn't fall through the ground now because he is on a platform.

86:58

Okay. And now if I do that, he can also move around. So left, right, jump. This

87:07

is looking wonderful. I'm so happy with this. We have now made Mario move on our

87:14

board. Great. However, look, if we go off a platform, he doesn't appear to be

87:21

falling. This is because we need to actually reset before the platform

87:26

collision. Let's just reset player grounded to be false so that he

87:32

continues falling. As a reset, okay, as a reset to everything. So, by default,

87:37

he should always be falling. Okay, wonderful. So now if we do that, he will

87:43

fall back down. Wonderful. Let's move on. So now we've done the platform

87:49

collision. We're going to use the same logic to do a pipe collision. So

87:56

pipe collision. Platform collision. Yeah, it's going to be exactly the same.

88:03

So we're going to use a loop for let pipe of game object

88:13

pipes if and then we're going to check collision going to pass through the

88:19

player and each pipe in the loop. We're going to check each pipe. And if we're

88:25

going to check the player velocity

88:30

y is larger than zero, this just means we're falling down into the pipe. Okay,

88:36

so we've pressed down. We want to fall into the pipe. So falling. Well, then once again we want to get player Y and

88:43

then just assign pipe Y to it minus the player height so that the player appears

88:50

on the right position and not exactly where the pipe is. So once again we get player

88:56

velocity Y and just clear it to be zero again.

89:01

And we're also going to ground the player. Okay. So make him stop on top of

89:07

the pipe. Wonderful. So that's stopping on top of the pipe. We still have some logic to do

89:13

for him to essentially go to the next level. First, however, let's do enemy.

89:19

So enemy collisions uh but also enemy movement. So enemy

89:24

movement and collisions. We'll do that in one. So once again, let's make a loop

89:30

for let enemy of game objects enemies

89:38

if no enemy is alive. So we're checking if any

89:43

enemies so we're looping again. If the enemy is not alive then you continue

89:49

otherwise we get the enemy xaxis

89:55

and we use the and operator. So addition operator

90:00

to get the enemy speed and multiply by the enemy

90:06

direction because it's going to change direction. So this will allow us to make

90:11

a move either minus or plus the speed. Okay. Now we need to actually change

90:17

that direction. So reverse direction at platform

90:25

edges or boundaries. Make sure to spell boundaries

90:31

correctly. Boundaries.

90:37

So for let platform

90:43

of game objects platforms so we're checking the array of platforms

90:50

and if enemy so we're still working with one enemy remember this is still in the loop x plus enemy width is larger than

91:00

or equal to x. So if that is true, all four of these statements are going to

91:06

have to be true to recognize that there is a collision with the platform, an enemy with a platform. So enemy X is

91:15

smaller than platform X plus platform

91:21

width. So if that's true, and now we're just going to do the same for the Y-axis. Enemy Y plus enemy

91:31

height is larger than or equal to platform Y plus enemy Y plus enemy

91:40

height is smaller than or equal to platform

91:46

Y. Okay. So if all of these things are true, we do have a collision.

91:54

So we are on the platform. So I'm just going to say platform on platform true

91:59

and I'm just going to create a variable here for on platform just we can store it outside of this loop on platform and

92:07

start off with it always being false before checking this loop. Great. And

92:12

then we just break out of this. Okay. So now we can write if we are not on the

92:19

platform or enemy

92:25

x is smaller than equals zero. So at the boundary edge of our game board or enemy

92:32

x is larger than or equal to 800. So at the right hand border of our game board,

92:39

we know a collision has happened. So let's make enemy direction switch. And a nice way to

92:47

switch from essentially one to minus one or minus one to one is

92:52

multiply by minus one. Great.

92:59

Okay. And now of course we update the element position. So we did it for

93:05

Mario. Now we do it for the enemies. So let's pass through the enemy

93:10

element. Let's pass through the enemy xaxis. And let's pass through the enemy

93:17

yaxis too. So now if we refresh, uh it seems the

93:23

little enemies are on the platform. They're kind of overlapping. So I'm just

93:28

going to jiggle this a little bit and put minus5 here on the y ais. + five

93:36

just to emit that little bit where they are overlapping. And great. Okay. So,

93:43

this is looking wonderful. Now, we need to do the Let's check the Yep. And they

93:49

bounce back. They changed direction. Let's do the enemy player collision now.

93:54

So, let's go ahead and do it. So, once again, still in this function, we're

94:00

going to do a player enemy collision. So,

94:07

Check player enemy collision.

94:14

So we're going to use the same function that we've been doing. Check collision.

94:19

We're going to pass through the player. We're going to pass through the enemy.

94:27

And if player velocity

94:33

y is larger than zero and player y is smaller than enemy

94:42

y. Okay. So basically the player is falling because velocity y is higher

94:48

than zero and the player is also so this also has to be true. The player is lower

94:54

than an enemy on the game board. Right? Because player Y of the player and the Y

95:00

of the enemy is what we are checking. Then essentially we are jumping on the enemy. So jump on enemy.

95:07

Uh and if that happens then the enemy alive is going to switch to false. We're

95:13

going to remove that enemy. So element remove. This is a JavaScript method to remove the element from the board. Then

95:20

we're going to do player velocity.

95:25

Y is going to be the jump force

95:32

multiplied by 0.7. Okay, so the jump force is going to slow down. And then the game state score,

95:42

let's just add 100 points. Okay, so 100 points for jumping on the enemy.

95:48

Great. else we're hit by the enemy. So hit by enemy.

95:55

So then that case we just want to essentially well let's check first if

96:02

the player is big right because if he's big he just gets a little bit smaller. So player big is going to go to false.

96:10

We're still to write this function but you know we have the big property on the player so it's easy to know what to do.

96:16

Player big timer. Let's reset that to zero.

96:22

Player element class list.

96:27

Remove the class list of big from Mario so he just appears normal. And then

96:33

let's check change the player width back to 20 and the height back to

96:40

20 because if he's big those should be 40 as well.

96:45

Okay. and then else. So if he's not big, we

96:52

lose life. But we are yet to write that function. So I'm just going to comma

96:58

that out for now. Great. Let's change how I spell enemy

97:04

here too. Okay, great. So again still in the

97:10

function of the update function

97:16

as you can see here we are still in it. We also need to

97:21

work on coin collection. So this is another loop for let coin of game

97:28

objects coins if

97:35

and we're going to check the coin because we're checking all the coins in the coin array. So if the coin is not

97:41

collected because we're going to mark it as collected if it is and there is a collision between the player and the

97:47

coin. So check collision player coin.

97:54

Well then you want to change collected to true, right? Because it is collected

98:00

now. And I'm going to remove it. So coin element remove game state score plus 50. So you

98:09

only get 50 for a coin. And that is it. Now let's work on the surprise

98:17

blocks. So when we interact with surprise blocks. Okay. Uh it seems we

98:22

made a little typo here. So let's change that. So once again another loop for let

98:28

block of game objects. So prize

98:36

blocks. If the current block that we are looping

98:42

over because there may be many blocks in the surprise block array. So if the current block we are looping over is not

98:49

hit. This means not and we check collision. So we is a collision between

98:56

the player and the block that we are currently looping over for and so these

99:01

three things need to be true. the player velocity

99:08

y is smaller than zero. Block hit equals true. We hit the block

99:16

and then we're going to change its class list so it changes visually. Block element class list add hit. So it looks

99:25

like it's been hit. Okay. So for now, let's see what hit looks like. Uh,

99:31

essentially we're just changing the image here. So that is good. Let's go ahead and try it. So I'm just going to

99:38

refresh here for now. Make sure player is spelled correctly there.

99:44

So let's just go ahead and refresh for now. So here we go. I'm jumping.

99:49

I'm killing the little enemies. I collide with a coin. I get the coin.

99:57

We need to update the score, too. And now let's check for a collision. So annoyingly that block is under there.

100:04

But amazing. So when we collide with the block, it changes to a hit block instead

100:10

rather than one with a question mark. So that is looking good. I love it. We can

100:16

of course move that to be here as well. That is something that I will do later on when we are refining the game. But

100:22

for now, this all looks good. Let's just check. We can land on the pipe as well.

100:28

And we can I love it. Okay, so let's continue. Now, of course, if we hit the

100:35

block, we're not quite yet done because if we hit the block, so if block is hit,

100:40

we just say true. We can also check the block type because our block object has a type assigned to it if you remember.

100:47

And it could be that has the type of mushroom assigned to it. And if it's mushroom, well, you know what to do. We

100:54

need to make the player big. So player big is going to be true. Player big

100:59

timer we're going to set to 600. So 10 seconds at 60fps.

101:07

The player element class list. We're also going to add

101:14

so add big to make our player big. So

101:19

for 10 seconds our player will be big. Uh let's also change the width of him to

101:25

be 40. Actually, maybe let's make it 30. Maybe double is a bit too much. Again, you can rejiggle with this as much as

101:32

you like. So, I'm changing the width and height the game state score.

101:40

Uh I'm also going to add 100 for getting a mushroom.

101:45

Then else if the block type was the other block type

101:50

it could be equals. So db equals the string of

101:56

coin. Well, I'm just going to add

102:03

a score of 50. Okay, great. We should also have

102:13

some kind of visual to show that a coin has been hit. We can work on that later,

102:19

but for now, let's check if he does in fact become big. So, once again, I'm

102:24

just going to refresh. And I think this is a mushroom. Ah,

102:32

great. So, amazing. We've multiplied Mario. However, why is that?

102:38

It's just looking quite funny that there is four. Let's have a look why. So,

102:44

Mario class big styles. Let's search for big. Uh, okay.

102:52

30. So, it's up to you however you want to do it. I obviously changed my mind and went

102:58

from 40 to 30. Uh, let's have a look. Okay, so there we

103:05

go. However, the Mario is not image is not covering the element like I want it

103:12

to. Just going to zoom in for you here a little bit. So, Mario big on Mario

103:19

itself. I'm just going to do background size cover no repeat.

103:25

So, now let's check it out. Tada. There we go. Mar is big. We fixed

103:33

that problem. Great. Let's continue.

103:38

We have one more interaction to do and that is for pipe interaction

103:46

to next level. Okay. So for once again let's

103:52

check for any pipes because there could be more than one pipe. Pipe of game objects

103:59

pipes. This means that when you create future levels, you don't have to worry about anything. You can add as many

104:05

pipes as you want. They all take you to the next level. If player

104:10

grounded. So if he is grounded on top of the pipe

104:16

and player X plus player

104:25

width is larger than pipe

104:31

X and player X is smaller than pipe X plus

104:40

pipe width and

104:48

we want to make sure that he's standing on top of the pipe, the whole pipe. I'm

104:53

going to use math abs to get the absolute number. Okay,

105:01

like a rounded number and I'm going to get player y + player height minus pipe

105:09

y. So, this will help us check if he's standing on top of the pipe is smaller

105:17

than five. And we also want to be pressing down, right?

105:23

So, game state keys and the current key that's being pressed is arrow down.

105:29

Okay, if all of that is true, we go to the next level and call it. Of course,

105:35

we haven't written that yet, so I'm just going to comma that out. However, this should be like that. Okay. So, if all of

105:42

that is true, then we go to the next level. I love it.

105:48

However, I'm just going to go up here because

105:54

check for enemy interaction. So, enemy movement and collision. Actually, this

106:00

should have inside of it. Check enemy player collision.

106:06

not the coin collection or the surprise blocks, however, or the pipe interaction. So, I'm just going to take

106:13

those three functions. Close this up now

106:18

and paste those three above the update element position still inside the update

106:24

function. Okay, so just like that. Please refer to my code that I have

106:29

added if you are not sure. Okay, now we're just going to do two more things before we go out of this

106:36

function of updating fall death.

106:42

If player Y is suddenly larger than 400,

106:50

then we lose life. So once again, that is a function that we are yet to write. And finally, let's

106:59

just update the UI with the score and everything. things. We're going to use document get element

107:04

by id and we're going to get the score so we

107:09

can update the score visually. You can use text content to do this

107:16

game state score. Okay. So whatever the game state score is is going to be assigned to the element of score in our

107:22

UI. Let's do the same for the level and the live. So just change this to level

107:29

and this too. lives and then this level. So this is

107:35

something that lives inside our game state object and lives. Great. And we

107:40

are finally done with the update function. Let's write our next level and lose life functions. Next. Okay. So down

107:48

here function lose life. Well the game state does store lives. So we're just

107:54

going to minus that from it. And if game

108:00

state lives, so for example, you have three lives. Uh this operator will remove one. So it

108:07

will come to two lives. And if game state lives suddenly is smaller than or

108:13

equal to zero, well then there's no lives, right? So we show game over. And because

108:20

we didn't win, we pass through the one variable as false. go back to the game

108:26

of a function if you need to see the one variable to refresh your memories.

108:32

Else we reset the player position. So player X is going to go back to being 50.

108:39

Player Y is going to go back to being 300. Of course, we need to make sure that this is aligned with what we

108:45

originally set. Player velocity

108:50

X is going to go back to being zero. Player velocity Y is going to go back to being zero.

108:57

Player big is go back to being false. Player big timer can go back to

109:06

being zero. Player element

109:14

class list remove any class of big.

109:21

Make sure that is spelled correctly. And let's set the play width back to being 20. And so the same with the

109:30

height. Wonderful. So that is our lose life function. We can uncomment where we

109:36

have used it. And we're also going to write a function for next level. So this

109:41

is going to take us to the next level. And all I'm going to do is get the game state level. So if it's currently one,

109:47

we'll change it to two using the uh addition operator. And then if game

109:52

level level is larger than levels

109:57

length. Well, I think for now we'll just show the game over. Okay. But you can

110:04

loop this if you want. You can add more levels. Game over. I'm just going to add

110:10

a game over for now. However, uh sorry, this should be course game

110:15

state because level lives in the game state object. else we go to the next level. So we use the

110:22

load level method that we wrote or function. We get the game state level

110:28

and minus one from it. Okay, great. Make sure that

110:33

says next level like so. Okay, so now let's uncomment those out. Uncomment

110:39

lose life here. Uncomment next level. And

110:45

uncomment lose life there. This is looking good. Let's have a go. Okay, so

110:51

now if we kill, we get a score of 100. That is great. Let's just check that

110:57

works again. So once again, score of 100. And if we get a mushroom

111:05

block and turn big again, that has been updated. We are still on level one. We've got three lives. So let's try

111:13

go to the next level. So, if we land on the pipe and go down, press down. Tada. We are right here. This is looking good.

111:21

However, I'm starting a bit higher. That is something I'm going to have to adjust. Let me just check first.

111:30

Okay. So, here something strange is happening. We will have to figure this out. And ta,

111:38

congratulations. You've won. So, that's our score. We can play again. So to me

111:44

this is looking wonderful. We of course probably need to attach some functionality to the play again button.

111:53

So maybe let's write a function called restart game.

112:01

Okay. Function restart game. And then all I'm going to do is set the game

112:06

state to be whatever it was as well as so once again what I am going to do. So

112:16

we can just scroll all the way back here just to make sure that it is the same. We'll get the game state. So this is the

112:23

reset essentially. We'll scroll all the way down here

112:29

and just paste it like that to make sure it's exactly the same. Well, then also, so just like here,

112:38

reset all of these things. So, I'm just going to copy that

112:46

and paste it here like so. And now let's grab the restart button.

112:56

So document get element by id

113:03

restart restart button const restart restart button

113:11

and then we'll grab the restart button add event listener click and then call

113:18

the function restart game. Okay, just going to move that to be down here.

113:25

Wonderful. I'm happy with that. So the X and Y axis after you lose a life is 50

113:31

and 300. I just want to make sure that is the same for here. It's actually 340

113:36

here. So let's change that down here.

113:42

340. And let's try play again. So

113:49

great. This is really really fun. Of course, you can mess around with how fast and how slow all these uh enemies

113:58

are. So, some of the images are a bit bigger. I'm just going to do cover just

114:03

like we did with Mario. So, where is Mario? Cover background repeat on some

114:11

of these two. So, on the enemies. There we go. And I believe also

114:19

on the pipes. So, pipe top,

114:26

pipe right, pipe bottom, pipe bottom, right? Uh, we didn't quite

114:33

finish writing this. So, restart game. Yes, reset everything. Let's also get

114:38

rid of the game over element. So, still in the function document. Get element

114:46

by it game over

114:54

style display

115:00

none. Make sure of course to do game over exactly how it's written

115:08

here. Okay. and then initialize

115:14

game again. One thing we can do is actually just simplify this. So remove that and

115:22

do that just so it's in the same format.

115:27

And great. So now if I get killed

115:33

game over play again. Wonderful. So yes, this is looking good. Amazing.

115:42

This is super super fun. So now I'm just going to go to the next level and once

115:48

again we probably need to put cover fit on some of these images and just

115:55

adjust a little bit where Mario is landing. But otherwise this is looking

116:01

good. Okay. So back in here I'm just going to add background size cover.

116:07

Let's just do it to all of these just to be sure. So it was platform. We need to

116:14

do platform background repeat. Let's do it also for the floating

116:20

and platform blue. And finally, let's just move the where

116:27

this little question mark is displaying. So that is easy. We just go to the game

116:32

board. So level one, the coin. Well, there's three. There's the surprise

116:39

block. So, I just want to move the surprise block higher and maybe more to the left. So, let's go ahead and do

116:47

that. I'm going to go xaxis

116:52

320 and then just add maybe 120.

116:59

Okay, maybe that's a bit too high. Let's go with 180.

117:06

Okay. So, that is looking better. That's how you would move yours around as well. So, if we go here, ta, he is bigger.

117:17

And these should be for coins. Just going to change this to background

117:23

size instead. Same for the platform ground and put

117:29

background repeat. Same for platform

117:34

blue. So

117:39

background size repeat. Same for here

117:46

as well. Okay. And that looks much better. I'm happier with that. Okay. We

117:52

have one last thing to do to really take this game to the next level, and that is spawn either a mushroom or a coin. if we

118:00

interact with the surprise block. So I'm going to do that. Let's say here.

118:06

And the function we're going to write. So function it's going to be called spawn item on box.

118:13

There we go. And of course we're going to pass through the block and the type.

118:20

So spawn. First we're going to create a new div to

118:26

represent the item. So, it's either going to be a mushroom or a coin. So, we're going to use document create

118:32

element div. And that div, we're going to assign to the const item. Next, we're

118:37

going to grab that item. Of course, at the moment, it's not on our board or anything like that. We're going to add

118:43

class list. So, class list add and the type. So, if you remember, the type will

118:49

either be a mushroom or a coin. Scroll back to the top if you want to

118:54

familiarize yourself with this again. Next, we have to position that item. So, item style on the left, we're going to

119:03

assign the block x value. So, wherever the surprise block is and just add

119:10

pixels because we want it to be exactly where the surprise block is. So, that is horizontally. And then we also need to

119:18

do this on the y axis. So item style

119:23

top just like so. And this time we get the Y-axis. So

119:29

block Y. However, we want it to be slightly above the block. So exactly 20.

119:36

The block is 20 height. So it will show up above the block. And then we add

119:44

pixels just like so. And then of course we need to add that element to the game

119:50

area so it's visible on the screen. So let's get the game area use the append child method and pass through the item.

119:59

Great. Now we're going to create an object to keep track of the items properties and physics.

120:05

So const item object equals and then of course we have to

120:12

make that an object. So the X is simply going to be where the surprise block con is. So we're going to

120:18

use block X. The Y of the object or the mushroom or the coin that's about to sprout on the Y axis is going to be Y

120:26

minus 20 because we said we want it to be above the block. The width of it is well going to be 20 because we're

120:32

keeping everything consistent. The height is going to be 20 as well. The element is going to be well the item

120:38

itself. So this that we created

120:44

after all of this has been applied and the velocity velocity

120:52

y so how it is falling so the vertical speed we're going to start off with being zero now game area is not defined

120:59

we must have picked it out in the scope of another function so I'm going to pick it out here uh let's do at the top so

121:06

document element by ID game area so we can pick out the game area of our game

121:11

and save as the con game area because that's where we're going to be putting in this item into the game area. Great.

121:18

Now, after this object has been written, but still in the spawn item object function, we're going to check if the

121:25

type is a mushroom. Because if it's a mushroom, we

121:32

want it to do one thing. To be precise, we want it to fall. else if type equals

121:39

coin. We don't want it to fall. We actually want it to rise. Uh so kind of

121:44

float in the air. So I think that coin is easier. Let's do that first. And

121:50

we're just showing something visually, right? So all I'm going to do is let

121:55

frames equals zero because we're going to start off with the frame counter

122:01

being zero to remove the coin after 3 seconds. And now I'm going to create an interval to animate the coin floating

122:06

up. So const float interval equals set interval.

122:14

And then I'm simply going to get the item object

122:20

Y and minus one to it. So it goes up and

122:26

then I'm going to get the item style top. So this is applying it visually,

122:32

right? This is not visual. This is the visual part. And I'm going to get the item object Y after I minus one to it

122:42

and just add pixels. Great. And then I'll increase frames + one. Now let's

122:51

stop it. So if frames is suddenly larger than or equal to 180 I simply want to

123:00

clear the interval of float interv

123:10

item. So item remove just like that. And I want this

123:15

to run every 16 milliseconds. So roughly 60 frames per second. So, I'll just put

123:21

that there. Wonderful. So, that is our coin object.

123:27

For now, I'm just going to console log out the word mushroom here so we can test it out. So, where do we want to

123:34

spawn the object? I'm just going to copy that. Well, I guess after we collide with a surprise block. So, let's check

123:42

for the surprise block collision. So, here we go. After block hit true. Yes.

123:49

So irrelevant what it is, we're going to spawn an object. We're going to pass

123:54

through the block. That's right. And the block type. So the type is actually attached to the block itself.

124:03

Great. So let's go back to here. So here we go. Let's test it out.

124:12

So let's find one that's a coin. So it should be here.

124:18

And great, our coin is indeed floating up. I love it. Let's just check we got

124:24

the right amount of points as well. So, let's go back here. 100. And tada. Yes,

124:33

we did indeed get points. So, this is looking good to me.

124:40

We got 50 for that one and 50 for that one. Everything is looking good. Let's

124:46

work on the mushroom next. Okay. So, if mushroom

124:53

just going to delete that so we can work with it first. Let's work on showing it visually. So, I'm going to create

125:00

another for loop. So, const fall for interval equals set interval

125:11

just like so. We'll get the item object velocity

125:18

y and this time because it's falling we're just going to use our gravity

125:24

const. Okay. So apply gravity to increase downward velocity and then we'll move the mushroom downwards

125:30

according to its velocity. So, we're simply going to get the item object again yaxis and add

125:39

the item object velocity after we just changed it above.

125:46

So, velocity y. Great. Now, let's check if the mushroom has landed on a

125:52

platform. So, let's start with on platform being

125:57

false to clear everything. and we're going to have to check for a collision with a platform. So once again, there

126:04

are many platforms that it could collide with. So let's loop over them. So let's

126:10

get the game objects and let's get the platforms array

126:17

platform forums and if

126:22

okay, so here we're going to do some more logic. We're going to check for a horizontal overlap.

126:29

And then we're going to check for another horizontal overlap, but this

126:35

time checking the width of the item object itself. Then we're going to check

126:40

the bottom of mushroom at the platform top. And then also allow a small

126:48

tolerance. So if there is a collision, we know that on platform

126:53

has to be true. And then what we're going to do is position the mushroom on

126:58

top of the platform and stop the downward movement because at the moment

127:04

it's falling. Item object velocity y equals zero. And then we'll break. So

127:13

exit the platform loop. Wonderful. But still inside here. So after these two

127:19

parentheses, we need to update the DOM element to match the new Y position. So actually show it. So we're going to get

127:24

the item style top item object

127:30

Y and add pixels to it. So we can see the mushroom visually. And if on

127:36

platform form is true, we're going to stop the

127:42

mushroom falling. Of course, we also need to clear the interval from running. So clear interval for in

127:52

great and we want this to run every 16 milliseconds again. So wonderful. This

127:59

looks good to me. The one thing we do need to do is add a mushroom style. So

128:05

I'm just going to go ahead and do that now. The style name of mushroom. Uh

128:12

let's just copy this. So just like that. and add a background

128:19

image. Again, I've already uploaded these onto Imager a while ago.

128:25

So, here we go. So, let's test it out.

128:30

So, now if I interact with this, ta, a mushroom falls down.

128:37

Now, in my game of Mario, I'm just going to actually remove the mushroom when it

128:43

hits the ground. I'm happy with this. Of course, you can take this further. Um, I

128:49

will leave a little note at the end of this video for you. For now, however, you might be noticing some lag. Uh, for

128:56

example, if you're viewing this the next day or in a different browser, I'm going to actually remove the set interval so

129:04

that everything is consistent with working with the request animation frame. So this should get rid of any lag

129:12

and your Mario should be moving at the same pace. So to do this, all we're going to do is just write a function

129:18

called fall like so. And I'm simply going to take everything from the set interval, everything, and remove the set

129:26

interval and paste that code. So that is now all in the full function. And I'm

129:32

going to remove this two and put request animation frame and pass through and

129:37

reverse this. So when we're not on the platform, this gets called. Great. And

129:42

let's call the full function. Let's do the same for the coin now. So once again, let's write a function called

129:48

float up. And I'm going to remove the frames. I'm going to take all the code from the set interval. Delete the set

129:54

interval. And once again, just change this to be requests animation frame and pass through floating up as well as just

130:03

make sure that that is attached to the object. So item object was item ob

130:09

remove remove this item remove and put that in an else because we need to reverse this. Okay. So I've just

130:16

reversed that. This time we're checking if it's above 180. Okay. If it's above

130:21

180 we call the request animation frame function. Great. So that is now much

130:26

more consistent. I'm just going to add frames to my item object. Okay. And

130:33

great. Okay. Okay, so there's one fix we need to do. When we go to the next level

130:39

and Mario is big. He appears to be floating. Let's get rid of that. So, let's remove big and reset the player

130:46

width and height when he goes to the next level before loading the game.

130:51

Done. Okay, good. One more thing we should do. There is a weird glitch. Say

130:56

if Mario's big and he gets hit by an enemy, because he gets made small so

131:02

quickly, the enemy is still there. So, he loses a life. No, let's get rid of that. So, I'm just going to check if the

131:08

player is grounded as well, so not falling. And that should fix that glitch

131:13

for us. Of course, feel free to take this further if you want to then have

131:19

the mushroom move and Mario chase it and only grow big once he eats the mushroom.

131:24

That is totally up to you. I'm just going to stop here for now. My intention

131:29

was to of course help you practice JavaScript and take this game, take it

131:36

to the next level, add more levels, add different functionality, you name it.

131:42

Okay, so please again if you want to fork this project and take it on, go

131:48

ahead. It's up to you. Hopefully you're happy with it in the state that it's in. and I produce a cool game for you while

131:55

we practice our JavaScript. Okay, so wonderful. I hope to see you

132:02

again on here soon.

No comments:

Post a Comment