We will create the Snake game using JavaScript only, means no framework is been used during the tutorial, the tutorial has two parts:
1st part : we understand everything about the game, we discuss things before we code.
2nd part : Type in the Code.
****************************************************************************************
Download the starter template from gitHub, so you can follow the tutorial step by step :
****************************************************************************************
The Game files link : https://github.com/CodeExplainedRepo/Snake-JavaScript
****************************************************
Other Tutorials for games created using JavaScript :
****************************************************
Tetris : https://www.youtube.com/watch?v=HEsAr2Yt2do
Flappy bird : https://www.youtube.com/watch?v=L07i4g-zhDA
*************
Social Media
*************
Facebook : https://web.facebook.com/codeXplained
Twitter : https://twitter.com/code_explained
************
HASHTAGS
************
#JavaScript #Games #JavaScriptGames
Original source
45 responses to “Snake Game Using JavaScript”
How to do that without background image?
Tail is not removing when i have a blank canvas..
@code Explained Thanks ,The task I want to do is when apple generate randomly .and snake ate it . I want to add another fruit ( banana ) on canvas and disable apple fruit at that time. and after eating banana I want to again generate apple ..How can I do this ? please help
hii i learn from you a lot! and do some extra tnx 🙂
https://omergal2.glitch.me/
Hi. Like i wrote in the comment section of another video of yours, your tutorials are great!
I have a question. Do you have knowledge of design patterns? And if so which one should I use? I am planning on rewriting the code using one, but I am new to this so I am a bit stuck.
Or if anyone of the viewers cares to give some suggestion, the help will be greatly appreciated.
Please, sir, how do I open the html5 and the javascript tabs?
in another comment, I saw you advised to download brackets.io and I download it and install it and I still don't get how you opened these two tabs?
Edit: (to CodeExplained) Sorry for not making myself clear. (Buttons hirt is my other account)
Skip Ad
Who else?
My images Wont load. Can Anyone help me. I connected my pages but nothing..
Brilliant For A Newbie Like Me This Is An Easy To Follow Tutorial, Thank You Very Much For Your Effort
All of your videos are just pure gold! They should teach us at school like this! You deserve at least 100K subscribers!
Dude youre si awesome, i jilust wondering if i could change the snake with an png image.. how could we did it?
Best Explanation. Fantastic
If your images don't load, add this code to your index.html file:
<img id="ground" src="img/ground.png" style="display: none;">
<img id="food" src="img/food.png" style="display: none;">
Then, replace this:
const ground = new Image();
ground.src = "img/ground.png";
const foodImg = new Image();
foodImg.src = "img/food.png";
with this:
const ground = document.getElementById('ground');
const food = document.getElementById('food');
in your snake.js file. It should now work as expected. Good luck!
Does it matter what the names of the files are? I have been running into problems with this project and other javascript projects where all the code is identical except the name of the source file and when I try to load the webpage it is just blank.
i'm trying to follow the tutorial and using the links but for some reason the files aren't working
Great Job 👍👍
I will wait for your next work! Best of the video!
Best Snake game tutorial, I've come across – Great approach to explain visually in advance. With sound to the game. Great concept. Looking forward to seeing more. What I miss with most tutorials that there is no series that build in more features to the fundamental game. Well done!
Oh this is a lot of work. Game logics + javascript + massive animations & good presentation. Why people down vote?
Awesome video , learned a lot ! Can you make a video about building a user interface in game ( such as new game, scores, exit) ? Keep up with videos, you deserve alot more views/subs !
Does the code work if instead of the green background image, I just use black color?
keep going you are doing well
So strange, the draw function refreshes but keep old trail positions and score when there's no ground image. Tested and tested again without ground, the snare trail grow and grow again, the new score doesn't replace the older one but is added above. Then just adding a background fixes everything. Strange behaviour to me.
Excellent tutorial. The pace was perfect! To many tutorials explain the basics over and over and just drag on. This was concise, detailed and easy to follow!
Thank you. Very good.
Can anybody help me fix my prob. My problem is once you open the net it only pops out the background picture but not the snake and i followed the tutorial.
Hello sir, I have one question.
How can I create 2 snakes with different speed ?
Thank you.
I followed all the steps. My snake is not moving. Please help.
wowww!! superb explaination.
can i do the whole thing without having background image? and with box = 5px? assuming minor changes in calculations
very nice tutorial for me to grasp…. is there a significance to the value of width and height being 608px? is it having to do with the ground image? any relations? please explain im at a basic level of javascript understanding
I don't understand which parts of this program make snake longer
I've never seen a better tutorial than yours. I'm from Germany, but I understood all the code, because you spooke
you helped me make my first snake game ….thakyou
Amazing video very detailed! Thank you!
this video is so detailed and amazing good job
good work. Thanks a lot. This cured a lot of headaches. Keep'em coming.
first sorry for my english
can you explain why
snake[0] = {
x : 9 * box,
y : 10 * box
}
the x is correct i count the box is 9 but the y is not 10 box, it's 8 box?, why can you tell me
Your tutorials are awesome man create a new JavaScript game tutorial about cars racing.
Good luck 🙂
Hello and tks for the video.
I do have a question about the color of the snake.
Like you can see in the code below:
c.fillStyle=(i==0)?"yellow":"green";
What is instead of (i==0) I like to have something like (i=="even numbers")
So that all even numbers in the Array are getting a yellow color and all odd numbers green.
Does somebody know where I can look this up or is there some kind of a solution for this problem?
thank you for reading and have a nice day.
does anybody know where i can find the images used to create this game?? Some help would be amazing!!
Hey code explained, I'm not sure if you're still replying to comments but I could really use some help. I've copied the names, files, and code in the .html and .js correctly (at least I think so) however when i open the html in google chrome it opens as a blank page: heres my html code:
<html>
<head>
<title>Snake Game</title>
</head>
<canvas id="snake" width="608" height="608"></canvas>
<script src="snake/snake.js"></script>
</html>
Let me know if you need anymore of my code to look at, thanks
this channel deserves more subscribers!
sanke
awesome
the best tutorial i've seen ever
Thanks a whole lot!!