Beginner JavaScript Project - Snake Game Tutorial

Поделиться
HTML-код
  • Опубликовано: 13 июн 2024
  • 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 месяцев назад +145

    It's an honor to be shared on this channel, hope you have fun coding it! 🙏

    • @zamsosam
      @zamsosam 6 месяцев назад +2

      It was awesome bro

    • @samimustafa7284
      @samimustafa7284 6 месяцев назад +1

      Youu did it brotherrr❤️❤️ yesss big fannn of youuu

    • @burjeduro9175
      @burjeduro9175 6 месяцев назад +1

      Thanks for sharing the knowledge you're sharing with us 👍

    • @howtobecomeadeveloper
      @howtobecomeadeveloper 6 месяцев назад

      @@burjeduro9175 My great pleasure!

    • @Bobbizymusic
      @Bobbizymusic 6 месяцев назад +2

      Please what is the software you used for writing the code and how do I get it thanks

  • @zona_i3d
    @zona_i3d 6 месяцев назад +29

    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 месяцев назад +7

      May employment find you swiftly my friend.

    • @juandager5220
      @juandager5220 5 месяцев назад +2

      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 месяца назад +2

      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 месяцев назад +11

    Just completed it in about 4 hours. Works like a charm! Thank you for the consistent awesome uploads:)

  • @DmitriyKochkarev
    @DmitriyKochkarev 6 месяцев назад +19

    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 6 месяцев назад +1

      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 месяцев назад +5

    Absolutely love this, thanks for taking the time to create and share it.

  • @tirtheshpawar4257
    @tirtheshpawar4257 5 дней назад

    Amazing Tutorial covering lots of different concepts!

  • @juandager5220
    @juandager5220 4 месяца назад +2

    Amazing content! It really helps understand how elements interact. Great introduction to JS before going to React.

  • @reptilianis577
    @reptilianis577 13 дней назад

    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 23 дня назад

    His teaching style is awesome and simple. Love this project

  • @robypsx
    @robypsx 4 месяца назад

    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 месяцев назад +1

    Thank you so much! For your generosity and hospitality.

    • @Khanasad_
      @Khanasad_ 6 месяцев назад +1

      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 месяцев назад +1

    Thnx for this video! +1 to my VSC skill and how professional coding looks like 😀

  • @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 месяцев назад +1

    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 месяца назад +1

    beautiful

  • @jamiukayode7
    @jamiukayode7 3 месяца назад

    Amazing 🤩 ❤❤

  • @TheFranszo
    @TheFranszo 6 месяцев назад

    Sweet! Coild have use a class

  • @rishiraj2548
    @rishiraj2548 6 месяцев назад +1

    Thanks

  • @enriqueviveros72
    @enriqueviveros72 4 месяца назад

    Good explanations

  • @Satoshi_Nakamoto_1
    @Satoshi_Nakamoto_1 6 месяцев назад +4

    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 месяцев назад +2

    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 месяцев назад +5

    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 месяцев назад +1

      they're in the walls

  • @fakharjafri3505
    @fakharjafri3505 6 месяцев назад +1

    Can we please have a CompTIA security+ course. You guys amazing. I need help. Thaanks alot

    • @USER-A566
      @USER-A566 6 месяцев назад +1

      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 месяцев назад +1

    1st❤

  • @St.Stepasha
    @St.Stepasha 6 месяцев назад +3

    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 месяцев назад +5

      Set top:65% in css #instruction-text

    • @BrynnZeeGreat
      @BrynnZeeGreat 2 месяца назад +1

      thanks for asking this question! i was confused too.

  • @Bobbizymusic
    @Bobbizymusic 6 месяцев назад +3

    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.

  • @soumadip_skyy_banerjee
    @soumadip_skyy_banerjee 6 месяцев назад +2

  • @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 :)

  • @ambole
    @ambole Месяц назад

    Lee Makwiny - ujinga umekushinda

  • @xtremelinux
    @xtremelinux 4 месяца назад

    RUclips 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 месяцев назад +2

    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 6 месяцев назад +1

      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 6 месяцев назад

      @@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 5 месяцев назад

    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 месяцев назад +1

    Where should a beginner start to learn about machine learning & python?

    • @juandager5220
      @juandager5220 5 месяцев назад +1

      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 месяцев назад +1

    What's his youtube channel?

  • @Pinkijhabnp
    @Pinkijhabnp 6 месяцев назад +1

    Fox can't Bark

  • @oyeSAURABH
    @oyeSAURABH 6 месяцев назад

    i need that VS-Code theme

    • @sonic8873
      @sonic8873 6 месяцев назад +1

      it's github theme or ayu.

    • @learnwithajit29
      @learnwithajit29 6 месяцев назад +1

      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 месяцев назад +1

    I hate JavaScript

  • @funtushgaming2596
    @funtushgaming2596 4 месяца назад

    Thanks