Learn how to code a Falling Ball game with the basics of web development. (HTML, CSS & Javascript)
LINK TO GAME:
LINK TO CODE:
Follow me below and reach out if you need any help!
Twitter:
Instagram:
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.
Create new HTML elements with JS, create variables block and hole and make them equal to document.createElement(“div”). Add some attributes to them, classes and IDs. Now append them to our game div.
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!
Random Playz
06.05.2023if this is basic i dont wanna see compicated
Ben Davies
06.05.2023Is there any extra code which would be able to output your score real time on the screen, and maybe a high score underneath? Thanks.
Calix Urie
05.05.2023but how can I make it faster as the score goes high?
TeamGo
05.05.2023can you teach how to do a basic gravity?
Odd _pistachio1
05.05.2023My high score was 282.
Quant Psych
05.05.2023i got 653 then, i got bored
Software engineer
05.05.2023please continue recording videos like these and games i like those so muchhhhh
Paco Leung
05.05.2023Did someone know when I want the circle drop more than 15 then finished the page and jump into other page, how can I do that? Can someone help me?
Betül Kanmaz
05.05.2023How can I place the score so that the score is written next to the game each time the ball falls off the block?
Dudkipl
05.05.2023i did everything exactly how the tutorial shows how to but when i open the live server all i can see is a very thin black line at the top of the page an nothing else no red ball, nothing. please hep me im new to coding and scripting and i really need help
jfromthegoonies
05.05.2023this 11 min video really got like 1 week of js lessons
ToastStealer
05.05.2023Hi, I followed the tutorial and my ball could not fall through the hole. It would be great if you could help, Thanks.
Lapantouflemagic0
04.05.2023interesting to watch, but since i don't know enough javascript i just don't where all those functions come from 😅
Prog The Code
04.05.2023Can you show how to make this game mobile responsive o is it only to do the same thing like when you created the "subway surfers" copy?? I love your work!
Ozark Gears
04.05.2023Welp…. me knowing nothing about js
completely failed this entire thing 😧
Cadet Sazzad
04.05.2023the ball is not going up with the blocks in my case. Can you plz help me?
plzplzplzplzplzplzplzplzplz
JoshuaDev
04.05.2023Great tutorials bro. Helped me understand JavaScript more
Mobbin6500
04.05.2023i made mine a box and it sutters up and down you can see it jumping up and dow like 1px at all times.
Benjamin Smink
04.05.2023Pritty nice but wat van not charge something
Тимур Мухамеджанов
04.05.2023+ 1 project for my git😊 thx;)
Kuro B
04.05.2023The red ball escaping from its job as the center of Japan's national flag.
Big D
04.05.2023I think this is a much better and more fun way to learn how to code, thank you so much!
milo
03.05.2023What software do you use
j serr
03.05.2023GREAT JOB !!! Defenely Thumbs up
Wawrzyniec
03.05.2023Thank you, really great tutorial! You made me love JavaScript <3
BLA1Z
03.05.2023please can anyone help me out.I want to make this game on android .
so does anyone know how to move the ball left and right on android; cause I cant use onkeydown on android. please reply.
UI Monk
03.05.2023dont stop this awesome playlist. keep going! GBU
Алексей Троцкий
03.05.20231 При зажатии кнопок влево или вправо шар начинался дрожать
2 Препятствия не реагировали на мяч
Brendan
03.05.2023Is there a way to prevent the ball from glitching out. (Which is caused from holding the arrow keys for too long). Thanks for any replies!
Jad Sawalmeh
03.05.2023Hey thanks for teaching me how to create this game, but after doping some research i couldn't find how i can make the game progressively get faster, do you know how i can do this?
PicBro'S
03.05.2023What if i wanna let play 3 balls against each other and making a score place
Bhuku
03.05.2023Thanks bro 😍
gozzy bicks
02.05.2023why are you still using var I thought we use let now
Another CBT and gust, the hacker
02.05.2023Sorry shawn but uuuuuuuuuuuuhhhhhhhh … yoink
Mitch
02.05.2023this doesnt work for me i followed all the step/code then opened it and it showed a white screen and doesn't say anything
Manav Pradeep Pillai
02.05.2023Its not working 3:49
Keos IgnIte Digital
02.05.2023superb material helps !
TFK computer science
02.05.2023your document source is very good
Emmanuel Izuchukwu
02.05.2023Bro, I want to start programming, how should I start
TheKingINTP
02.05.2023it just doesn't work 🙁
S.J
02.05.2023Why doesnt the black block pop up on my screen when ive done all the same stuff 😅?
P0werman1
02.05.2023When I did document.getElementById(“character”); it said ERROR: ‘document’ is not defined. [no-undef] var character = document.getElementById(“character”);
LAE
01.05.2023Thanks, I need to learn how to make games using JS.
JackOfAllRAIDs
01.05.2023You totally skipped over the iholeLeft variable definition line. It kept giving me an "Uncaught ReferenceError: iholeLeft is not defined" error. I figured it out myself before seeing that portion at 11:20.
eliot fiszer
01.05.2023i got 637 score on the game try to beat
Menna Rouby
01.05.2023Thanks
WizardDinoDude
01.05.2023thanks
amlake
01.05.2023hey, what javascript crash course should i learn to understand this
Let's Learn Code Together
01.05.2023Could you please how the both variable is working and how the increaments of one is helping please.
Abdoulie
01.05.2023Great job 🎉❤