Javascript Falling Ball Game Tutorial

preview_player
Показать описание
Learn how to code a Falling Ball game with the basics of web development. (HTML, CSS & Javascript)

Follow me below and reach out if you need any help!

Create HTML elements, a div with ID “game” and ID “character”. Style the game div. A width, height, border and margin: auto. Style the character div. A width, height and a background color of red, a border radius of 50%, position: relative, left and to, z-index of 1million.

Create two very similar functions, moveLeft and moveRight. They create a variable equal to the current left position of the character, and then set a new left position for the character while either adding or subtracting 2 pixels.

Create an event listener that runs whenever you press the left and right arrow keys. Inside create an interval that runs the right function depending which arrow key they pressed.

Create another event listener, when you unpress any of the keys, clears the interval and stops the ball from moving.

Now our block and hole need styling. First add a width, height and background color to the block, position relative and set the top to 100px. Then the hole, a width, height and a background color of white, position relative and a top of 100px as well.

Create a variable “random” and set it to a random number lower than the width of the game minus the width of the hole. Set the left position of the hole to that random variable.

Created an interval function so that we create more blocks and holes. Create a variable called counter and set it to 0. Increment it every time we make a new block. Now add it to the ID of the block and hole.

Now we have a bunch of blocks and holes all being created ontop of eachother so we need to find a way to find the block and hole that was created last and add 100px to the top so that the new one is below it.

Create two variables called holeLast and blockLast, equal to the block or hole with the ID one below the current counter. Create two new variables equal to the top positions of the last hole and block.

Add an if statement around our code so it will only execute if the last hole and blocks top position is below 400. We still need to force it to create the first block and hole, to do that add an OR to the if statement and make it counter==0.

Now it will only create blocks and holes if they fit inside our game div.

Create an array called current blocks, whenever we create a new block and hole, append the counter to the array.

Then create a for loop that loops over the length of the array. Inside create a new variable called current which is equal to the value in the array. Then create two variables ihole and iblock which are equal to the blocks and holes with the ID the same as the current variable. Next create a variable that is equal to the top position of the block we just got. Then we’re going to set new top positions for the block and hole by making it the same as it was, but then removing 0.5px from it.

Create two new variables characterLeft and characterTop. Create a variable called drop and set it to 0. Create a if statement inside the for loop that increments the drop variable if the character is currently on top of a block. Inside of that if statement, create another if statement that will set drop back to 0 if you’re currently over a hole.

Then after our for loop we’ll create an if statement that makes the ball fall if drop == 0 or makes it raise up if drop != 0.

Now our game is done all we need to do is add an if statement, if the character top is above the top, then the game is over, alert game over with the users score, and then refresh when they click okay.

Thats it!
Рекомендации по теме
Комментарии
Автор

the king of circle and square games. just wait until he discovers HTML5 canvas

elijahbuscho
Автор

him: thats where its gonna get a bit complicated
me: sad js beginner noises

mattyxow
Автор

Hi ! Thanks for this tutoriel !! I just had a little issue with one variable which I think you forgot to talk about at 10:50 . We just need to declare iholeLeft and everything will work properly.

kouloukou
Автор

i do not like that you show your self too much it is sometimes ignoring, but i like the way you teach so much

argo
Автор

Hey man, you could've just set the position of character, block, and hole div to absolute and the game div to relative, that way the position of character, hole, and block all stacks at the top left corner of the game div and you can set the top or left of those divs without thinking of the width of height of the other divs, if you set all the position to relative at "top: 0px;" the character div would be at the top corner but the block would be below it and the hole below the block, in my opinion it cuts a good amount of work time just by setting it to absolute

hahayes
Автор

I think this is a much better and more fun way to learn how to code, thank you so much!

bigd
Автор

Thank you for helping me solve a problem which stumped me for 5 days

thechinaman
Автор

i just really love this and how you give the tutorial

eurjpylightskinfx
Автор

Hello Fellow!! love your JS game teaching!! all of your videos are really educational in learning game development and understanding JS.
Also, could you show how we can add or enable touch events in these games?
Anyhow your'e doing a gr8 job!!
Kudos Fridos!!

anavon
Автор

I have created That Jumping game with the help of your video. Please make more videos on javascript projects.

ajayrabari
Автор

Thank you so much bro. I feel like I can actually learn to code haha

halstrong
Автор

This game doesn't work when I completed all the codes 😭😭😭😭

avikahimalyan
Автор

Great tutorials bro. Helped me understand JavaScript more

joshuadev
Автор

why are you still using var I thought we use let now

gozzybicks
Автор

I love your vids! They are so great! They inspired me to start coding a website! Btw, what site do you use to get your web address? Thx :)

Waveclaw
Автор

This video is amazing!! Learning so much lol

SkeezerMui
Автор

please continue recording videos like these and games i like those so

softwareengineer
Автор

Wazzup!! i love your teaching style .. thank you smoke and fly high....

markmillare-rg
Автор

Is there any extra code which would be able to output your score real time on the screen, and maybe a high score underneath? Thanks.

bendavies
Автор

For anyone who's having issues with the hole at 5:32, make sure that your script.js file has the correct "div" and the right hole.setAttribute.

SirInfamous