Beginner JavaScript Project - Snake Game Tutorial

  Рет қаралды 86,896

freeCodeCamp.org

freeCodeCamp.org

Күн бұрын

In this beginners coding tutorial, you will learn to create a Retro Snake Game inspired by the iconic Nokia Snake game using vanilla HMTL, CSS and JavaScript! Using JS you will learn to draw the game board, create the snake and randomly generated food, make the snake move, increase the snake size when it eats, and increase its speed. you will also learn to keep the score, track collisions for the snake, and display your high score.
✏️ Course developed by @howtobecomeadeveloper
💻 Deployed Game: snake-game-howtobedev.netlify...
💻 GitHub Repo / Source Code: github.com/Ade-mir/snake-game-js
Snake AI-Generated Logo: github.com/Ade-mir/snake-game...
Google Fonts: fonts.google.com/specimen/VT3...
AI font generator / Lexica: lexica.art/
⭐️ Contents ⭐️
⌨️ (00:00) What we are building / Finished snake game
⌨️ (01:57) Setting up our HTML
⌨️ (10:25) Styling with CSS
⌨️ (27:05) Creating the snake game logic with JavaScript
🎉 Thanks to our Champion and Sponsor supporters:
👾 davthecoder
👾 jedi-or-sith
👾 南宮千影
👾 Agustín Kussrow
👾 Nattira Maneerat
👾 Heather Wcislo
👾 Serhiy Kalinets
👾 Justin Hual
👾 Otis Morgan
👾 Oscar Rahnama
--
Learn to code for free and get a developer job: www.freecodecamp.org
Read hundreds of articles on programming: freecodecamp.org/news

Пікірлер: 115
@howtobecomeadeveloper
@howtobecomeadeveloper 6 ай бұрын
It's an honor to be shared on this channel, hope you have fun coding it! 🙏
@zamsosam
@zamsosam 6 ай бұрын
It was awesome bro
@samimustafa7284
@samimustafa7284 6 ай бұрын
Youu did it brotherrr❤️❤️ yesss big fannn of youuu
@burjeduro9175
@burjeduro9175 6 ай бұрын
Thanks for sharing the knowledge you're sharing with us 👍
@howtobecomeadeveloper
@howtobecomeadeveloper 6 ай бұрын
@@burjeduro9175 My great pleasure!
@Bobbizymusic
@Bobbizymusic 6 ай бұрын
Please what is the software you used for writing the code and how do I get it thanks
@zona_i3d
@zona_i3d 6 ай бұрын
things like this keep me fighting depression and getting up in the morning. I've been out of work for six months and the holiday season is a horrible time but these things keep me going. Thank you
@Alexander_D_Shaffer
@Alexander_D_Shaffer 6 ай бұрын
May employment find you swiftly my friend.
@juandager5220
@juandager5220 5 ай бұрын
Keep fighting. Demand for tech jobs is always high. If you continue studying and making stuff, it's a matter of when... You'll get a tech job. We've all been there!
@lelin4732
@lelin4732 3 ай бұрын
Keep pushing the repos and we can do it dude. This is one of those only field where the dev community has always been supportive and helpful for newcomers. Keep building stuff!
@NAkt-vz4hq
@NAkt-vz4hq 5 ай бұрын
Just completed it in about 4 hours. Works like a charm! Thank you for the consistent awesome uploads:)
@DmitriyKochkarev
@DmitriyKochkarev 6 ай бұрын
Thanks for the great video! The only two things I think should be fixed in the game logic before starting playing with tweaks for it: 1. Reverse snake movement should be blocked. Now if snake length is 1 and you press arrow key opposite to the current movement direction the snake would go in that opposite direction and the snake is > 1 - it would reset the game which is obvious but would make players mad :) 2. Food generation should be adjusted so food would not generates inside the snake, for now it happens sometime. Probably would use the same logic as in the collision detection function. Thank you!
@harshitpant07
@harshitpant07 5 ай бұрын
else { switch (e.key) { case 'ArrowUp': case 'W': case 'w': if (direction !== 'down') { direction = 'up'; } break; case 'ArrowDown': case 'S': case 's': if (direction !== 'up') { direction = 'down'; } break; case 'ArrowLeft': case 'A': case 'a': if (direction !== 'right') { direction = 'left'; } break; case 'ArrowRight': case 'D': case 'd': if (direction !== 'left') { direction = 'right'; } break; } } }
@juandager5220
@juandager5220 4 ай бұрын
@@harshitpant07 Did you push this on GitHub?
@mohdusaid3956
@mohdusaid3956 3 ай бұрын
@@harshitpant07 In this case, it would not allow you to move in the other direction even if your snake is still just a head.
@bitcoinbunker
@bitcoinbunker 6 ай бұрын
Absolutely love this, thanks for taking the time to create and share it.
@tirtheshpawar4257
@tirtheshpawar4257 3 күн бұрын
Amazing Tutorial covering lots of different concepts!
@juandager5220
@juandager5220 4 ай бұрын
Amazing content! It really helps understand how elements interact. Great introduction to JS before going to React.
@reptilianis577
@reptilianis577 11 күн бұрын
One of the most valuable channels which I subscribe, thank you for your work ;)
@USER-A566
@USER-A566 6 ай бұрын
Thank you, I look forward to playing the finished game!
@zhendrey6730
@zhendrey6730 5 ай бұрын
Thank you very much, I really enjoyed creating the game with you! ❤
@vidhanshuborade5977
@vidhanshuborade5977 5 ай бұрын
Enjoyed creating it, Thank you!
@brunomoura7719
@brunomoura7719 6 ай бұрын
Nice one! ❤❤ Thanks for keep excellent material. 🙏🏻
@anurag_tarai
@anurag_tarai 21 күн бұрын
His teaching style is awesome and simple. Love this project
@robypsx
@robypsx 3 ай бұрын
Excelent tutorial, makes learning javascript a lil less tedious when you're building something you can play with
@rockNbrain
@rockNbrain 6 ай бұрын
Always fun to watch snake games coding ... tks a lot!
@gameon4298
@gameon4298 5 ай бұрын
Well explained each and every line of JavaScript code. Thanks man 👍
@Dedsec-22
@Dedsec-22 6 ай бұрын
Thank you so much! For your generosity and hospitality.
@Khanasad_
@Khanasad_ 6 ай бұрын
why hospitality bro ?
@Dedsec-22
@Dedsec-22 6 ай бұрын
this channel is like a virtual home encouraging IT enthusiast to come in and be fed skillfully. It's an undeserved honor that they make all these provisions accessible to people like us@@Khanasad_
@MGTRN-tw5uq
@MGTRN-tw5uq 2 ай бұрын
This was a lovely and fun lesson. Really well explained. Thank you
@fouadcotex
@fouadcotex 6 ай бұрын
great tuto, u can follow along from the begining to the end and erything is just ok.
@egsablin
@egsablin 6 ай бұрын
Thnx for this video! +1 to my VSC skill and how professional coding looks like 😀
@AbdussalamPopoola
@AbdussalamPopoola 6 ай бұрын
I know right! Enjoyed this one
@likandokayombo
@likandokayombo 6 ай бұрын
Powerful content right there👏
@user-ct6wq1jz7p
@user-ct6wq1jz7p 6 ай бұрын
I love the defer I always had to put my script at the end of my html
@camiloforero4996
@camiloforero4996 3 ай бұрын
Thank you very much, I followed and completed the tuturual succesfully, now as extra features I have added a background music and sounds when you lose and when the snake eats food. Besides I used LocalStorage to save the highest score even if you reloaded. I also blocked the option to go to an opposite direction. so the snake won't die I'm so happy ! Next step is to add a new game element called trap and don't generate food inside the snake that's something that sometimes happens
@afaqahmad-ef4ky
@afaqahmad-ef4ky 6 ай бұрын
Quality content ❤
@D3ll316
@D3ll316 6 ай бұрын
Thanks for making this video.
@Android-17
@Android-17 6 ай бұрын
Awesome 🔥
@omarious
@omarious 6 ай бұрын
Thank you!
@bryansuarez2396
@bryansuarez2396 3 ай бұрын
One can also omit the div when creating ids (using #) and classes (using .) with the emmet abbreviations and shorten it to ".game-border-1" or "#game-border-1" for example and then press tab and that will automatically create a div with the class or id that you named.
@user-jg6jo8lv4o
@user-jg6jo8lv4o 4 ай бұрын
beautiful
@jamiukayode7
@jamiukayode7 3 ай бұрын
Amazing 🤩 ❤❤
@TheFranszo
@TheFranszo 6 ай бұрын
Sweet! Coild have use a class
@rishiraj2548
@rishiraj2548 6 ай бұрын
Thanks
@enriqueviveros72
@enriqueviveros72 3 ай бұрын
Good explanations
@Satoshi_Nakamoto_1
@Satoshi_Nakamoto_1 6 ай бұрын
What i give for his lectures❤❤❤
@AbdussalamPopoola
@AbdussalamPopoola 6 ай бұрын
Thank you
@not_amanullah
@not_amanullah 2 ай бұрын
thanks ❤
@abulhasan923
@abulhasan923 6 ай бұрын
Please make a Bootcamp video for Google Cloud, I want to start GCP but I couldn't find any roadmap or resources on where to start.
@StrangerDanger352
@StrangerDanger352 5 ай бұрын
Which extension is that show which code block we are in anyone??
@triz9267
@triz9267 6 ай бұрын
Hey can you guys please make a course on robotics I think that would be really interesting and a lot of people would be invested on that topic.
@chaks2432
@chaks2432 6 ай бұрын
I was literally starting to make a python Snake game last night at like 4am. How the hell do you guys always release tutorials on projects I'm working on or planning to do?
@briabytes
@briabytes 6 ай бұрын
they're in the walls
@fakharjafri3505
@fakharjafri3505 6 ай бұрын
Can we please have a CompTIA security+ course. You guys amazing. I need help. Thaanks alot
@USER-A566
@USER-A566 6 ай бұрын
Not sure if it has been done, but A+ too if it hasn't been done yet.
@issahakumohammed
@issahakumohammed 6 ай бұрын
Great 😂
@dywa_varaprasad
@dywa_varaprasad 6 ай бұрын
1st❤
@St.Stepasha
@St.Stepasha 6 ай бұрын
26:51 It can be seen that the Logo has a lower margin that overlaps the text. 26:56 this has already been fixed. How? I can only remove this by zooming the browser to 80%, but this is not a solution. Upd: the z-index helped
@user-ps9ne8qz5c
@user-ps9ne8qz5c 6 ай бұрын
Set top:65% in css #instruction-text
@BrynnZeeGreat
@BrynnZeeGreat 2 ай бұрын
thanks for asking this question! i was confused too.
@Bobbizymusic
@Bobbizymusic 6 ай бұрын
Please what is the software you used for writing the code and how do I get it thanks
@glungusgongus
@glungusgongus 6 ай бұрын
Someone needs to answer what software is he using
@digigoliath
@digigoliath 6 ай бұрын
Watch the video?
@martinhogan9745
@martinhogan9745 6 ай бұрын
He is using VS Code. He mentions it once very quickly at the beginning of the video.
@gembojones8243
@gembojones8243 4 ай бұрын
do you know if theres a way to amend the code so it can be played on a mobile? still on the webpage that is (not an app). thanks for the great tutorial :)
@soumadip_skyy_banerjee
@soumadip_skyy_banerjee 6 ай бұрын
@ambole
@ambole Ай бұрын
Lee Makwiny - ujinga umekushinda
@xtremelinux
@xtremelinux 4 ай бұрын
KZfaq needs to add a Heart icon. Which means you super ❤ a video.
@thanhtinhtran843
@thanhtinhtran843 6 ай бұрын
what font u use in this video ?? pls answer me plssssssssssssss
@mahendranath2504
@mahendranath2504 6 ай бұрын
Thank you so much 🫡🙏👋✌️👌👍🫶🏻, liked 👍and subscribed your channel
@CoolestPossibleName
@CoolestPossibleName 6 ай бұрын
I haven't taken the course yet. I tried the game and I found a bug. if the length of the snake is less than 3 I can just go backwards by hitting the respective key. and if the snake's length is more than 3 the snake dies when doing the same
@pavanMudavath822
@pavanMudavath822 6 ай бұрын
haa
@harshitpant07
@harshitpant07 5 ай бұрын
else { switch (e.key) { case 'ArrowUp': case 'W': case 'w': if (direction !== 'down') { direction = 'up'; } break; case 'ArrowDown': case 'S': case 's': if (direction !== 'up') { direction = 'down'; } break; case 'ArrowLeft': case 'A': case 'a': if (direction !== 'right') { direction = 'left'; } break; case 'ArrowRight': case 'D': case 'd': if (direction !== 'left') { direction = 'right'; } break; } } }
@CoolestPossibleName
@CoolestPossibleName 5 ай бұрын
@@harshitpant07 that's neat
@miquan57312
@miquan57312 6 ай бұрын
that good
@jonathanv.hoffmann3089
@jonathanv.hoffmann3089 6 ай бұрын
🎉🎉🎉
@vivekkelodiya1
@vivekkelodiya1 2 ай бұрын
I think there is a bug in the game, when it is going in the direction let's say right and if we press the ArrowLeft button it starts to go in the left (opposite of what it was going to) direction, and if it has a length of more than 2 blocks it eats itself as the head is at the same position as one of its lengths position.🙏🙏🙏🙏
@IamSamirGabr
@IamSamirGabr 6 ай бұрын
i dont understand why we added the interval two times (in the gamestart function and in the if statement inside the move function) 😢
@mohdusaid3956
@mohdusaid3956 3 ай бұрын
in the gameStart function it is initially added to start the game and in the move function after checking that the food is eaten we clear the previous interval to avoid possible bugs so we assign it again in the next line to start moving the snake again.
@Shriramkrishnhari
@Shriramkrishnhari 6 ай бұрын
👍
@neshadtasnim7013
@neshadtasnim7013 4 ай бұрын
done in 2 hours :D
@Sfr339
@Sfr339 6 ай бұрын
Just chill
@blazer125
@blazer125 6 ай бұрын
My JS file won't run outside the body element. It works fine anywhere inside the body element. Any ideas on why that happens?
@juandager5220
@juandager5220 5 ай бұрын
Did you ask GPT? Or did you ask in another forum? Google a freeCodeCamp post titled "Read Search Ask"
@eliaswoldesenbet1915
@eliaswoldesenbet1915 6 ай бұрын
How game crater edutable or JavaScript game creator game credit game language game create
@gembojones8243
@gembojones8243 4 ай бұрын
I've followed up until 49:47 with no errors but when testing to view the snake and food in the browser, it's not working and I'm getting the following error: script.js:5 Uncaught ReferenceError: Cannot access 'generateFood' before initialization at script.js:5:12 any advice? Thanks.
@beebo33
@beebo33 4 ай бұрын
Is your program defining const gridSize after defining food? Food is defined by the drawFood function, which uses setPosition which uses gridSize
@gembojones8243
@gembojones8243 4 ай бұрын
@@beebo33 the difference I had was using arrow functions rather than regular functions. Changing them all to regular functions has fixed the issue although I'm not really sure why. It worked fine when I created the snake head
@pratikkc7232
@pratikkc7232 6 ай бұрын
can you plz make code for battle ships game on java where we can play with more than 2 friends
@Bumlife2Bomblife
@Bumlife2Bomblife 6 ай бұрын
Where should a beginner start to learn about machine learning & python?
@juandager5220
@juandager5220 5 ай бұрын
Your question is too broad... What foundational knowledge do you have on software and/or engineering? How much time do you have? And why specifically ML/Python? You could start by learning basic JavaScript with freeCodeCamp, then algorithms and data structure, and then Python. You could also try a full stack tutorial that uses Python, like Flask or Django, then jump to ML. Again, depends on your foundation and current skills, and you mentioned "beginner." JS is easy for beginners, even easier than Python IMO, because you can start learning now on your browser's console without any pre setup. Either way, learning software is a long road that never ends, and you must walk with small steps every day. Start now. Go to bed a little bit smarter than the day before. Good luck!
@Bumlife2Bomblife
@Bumlife2Bomblife 5 ай бұрын
@@juandager5220 thank you big bro. I have no code experience but I have played with huggingface, vscode, colab. I like how machine learning can train without any human interaction. I think thats how it goes! Im also using freecodecamp. For now I will start out wit JS & move myself up
@mr.kachoo8784
@mr.kachoo8784 6 ай бұрын
Thank you man , help a lot
@jamiukayode7
@jamiukayode7 3 ай бұрын
Can I play this game on mobile phone 📱…?
@hatredguy
@hatredguy 6 ай бұрын
What's his youtube channel?
@howtobecomeadeveloper
@howtobecomeadeveloper 6 ай бұрын
It's me 🤓
@Pinkijhabnp
@Pinkijhabnp 6 ай бұрын
Fox can't Bark
@oyeSAURABH
@oyeSAURABH 6 ай бұрын
i need that VS-Code theme
@sonic8873
@sonic8873 6 ай бұрын
it's github theme or ayu.
@learnwithajit29
@learnwithajit29 6 ай бұрын
It is available as "Dark High Contrast" as a preinstalled VS Code theme. No need to install any other theme.
@user-wc1sm8cj8s
@user-wc1sm8cj8s 6 ай бұрын
you should've used HTML canvas
@illegalsmirf
@illegalsmirf 6 ай бұрын
I hate JavaScript
@funtushgaming2596
@funtushgaming2596 4 ай бұрын
Thanks
NGINX Tutorial for Beginners
51:03
freeCodeCamp.org
Рет қаралды 209 М.
Build this JS calculator in 15 minutes! 🖩
15:20
Bro Code
Рет қаралды 367 М.
Whyyyy? 😭 #shorts by Leisi Crazy
00:16
Leisi Crazy
Рет қаралды 16 МЛН
🍟Best French Fries Homemade #cooking #shorts
00:42
BANKII
Рет қаралды 55 МЛН
UFC 302 : Махачев VS Порье
02:54
Setanta Sports UFC
Рет қаралды 1,4 МЛН
Asynchronous JavaScript Course (Async/Await, Promises, Callbacks)
1:36:23
freeCodeCamp.org
Рет қаралды 654 М.
God-Tier Developer Roadmap
16:42
Fireship
Рет қаралды 6 МЛН
The Async Await Episode I Promised
12:04
Fireship
Рет қаралды 1,1 МЛН
Git and GitHub for Beginners - Crash Course
1:08:30
freeCodeCamp.org
Рет қаралды 4 МЛН
Game Automation with YOLOv8: Python Bot Tutorial
21:40
ClarityCoders
Рет қаралды 16 М.
The JavaScript Survival Guide
14:47
Fireship
Рет қаралды 715 М.
A game of Snake written in JavaScript 🐍
28:50
Bro Code
Рет қаралды 65 М.
2048 на JavaScript разработка игры
40:47
Yura Koch
Рет қаралды 66 М.
Whyyyy? 😭 #shorts by Leisi Crazy
00:16
Leisi Crazy
Рет қаралды 16 МЛН