Creating A Snake Game Tutorial With HTML5 October 18, 2015 in Articles »Canvas Example Game HTML5 JavaScript Tutorial This tutorial shows you how to create a Snake Game using JavaScript and HTML5. A Snake Game is an action game that consists of a snake that is constantly moving inside a level. The player controls the direction of the snake. I'd like to use these ideas for math centers. This teacher has been using Daily 5 Math for two years. She has some good ideas that could be adapted to most grades. Find out how sh.
Active3 years, 6 months ago
$begingroup$
I created a classic snake game in the canvas element. I have not considered best practices when doing this, I just wanted to finish it first. Now it's time to improve the coding practice. You can help me out by mentioning bad practices, giving code improvements and suggesting anything else.
![]()
You can see a live version of the game here.
Pimgd
21.3k55 gold badges5959 silver badges142142 bronze badges
VigneshVignesh
23811 gold badge55 silver badges1313 bronze badges
$endgroup$
4 Answers$begingroup$
From a once over:
Good
Not so good
27.7k44 gold badges5858 silver badges237237 bronze badges
$endgroup$$begingroup$
Some thoughts on your general code style (some points might depend on personal preference):
SyjinSyjin
$endgroup$$begingroup$
![]() Airport Code Flash Cards
I would suggest drawing the snake as a single polyline instead of as a bunch of blocks, so that you only call
stroke once instead of calling fillRect as many times as blocks has the snake.
In general, it would be more efficient to stroke one complex shape instead of a bunch of simple ones. You can see that in this test.
Another option that I would consider is to
clearRect only the last block of the snake and then draw (fillRect ) only the new one, so that you don't have to redraw all the scene, only the parts that have changed. Another test here.
You will have to test both options and see which is better for you, but I would go for the second one.
I would also consider using
requestAnimationFrame insted of setTimeout . From the MDN doc:
The
window.requestAnimationFrame() method tells the browser that you wish to perform an animation and requests that the browser call a specified function to update an animation before the next repaint. The method takes as an argument a callback to be invoked before the repaint.
Also check this post about that. It explains the basics and also how to set a custom frame rate, so that you can still use
refresh_rate in your code.
There are two options:
Wrapping the
requestAnimationFrame in a setTimeout :
Or using deltas:
In your case I think it would be better the first option because the snake can only be drawn in certain blocks, so it doesn't make much sense to me to use deltas here.
DanzigerDanziger
$endgroup$
Colorado Criminal Code Flash Cards$begingroup$
To @Sykin's answer, I would add:
If you press down and then left faster than the snake takes to slither 'one step', the snake turns around and slithers over itself.
I find that JSLint really helps me get a consistent code style.
I suggest you use strict mode in your JS, by starting with a line
'use strict';
Community♦
ANevesANeves
$endgroup$
protected by Jamal♦Dec 2 '15 at 16:26California Penal Code Flash Cards
Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead? Not the answer you're looking for? Browse other questions tagged javascripthtml5canvassnake-game or ask your own question.The Big Irregular Verb Card Game - RulesLevel: elementary Age: 7-17 Downloads: 226 The Big Irregular Verb Card Game - Set 1 Level: elementary Age: 7-17 Downloads: 223 Places Cards Game Level: elementary Age: 6-17 Downloads: 223 Morse Code Flash CardsESL Interactive Grammar & Vocabulary Games - Play Now! Play games to practice essential English grammar and vocabulary skills. Learning is fun with Turaco Games - Practice and play now. Live Worksheets Worksheets that listen. Worksheets that speak. Worksheets that motivate students. Worksheets that save paper, ink and time. English Lessons for Kids - Videos, Printables, Games, Online Tests Teach kids with an engaging blended English program. A multi-level English curriculum featuring cartoon animated videos, engaging games, interactive tests and a progress tracker. Take a tour now! The Clever Worksheet Maker - From just $3.33 p/m 25 smart cloud-based worksheet generators for making fun, effective lesson materials. Make worksheets from your own wordlists, or start with one shared by our members and customise it to suit your needs. Sign up today and try 3 for free! Teach Children to Read with Phonics, Worksheets, Games, Videos, Books These are among the best phonics worksheets, games, videos and flash cards you will find online. The has everything you need to help a child learn to read through phonics: decodable stories, listening exercises, you name it. Visit this page now! High Quality ESL Lesson Plans - Free Samples - $26 Membership Be a better teacher! Hundreds of PDF lesson plans. Grammar, reading, vocabulary, speaking. All with comprehensive Teacher Notes included. Zero preparation time required. Listening lesson plans with mp3 files also available. Plus flashcards. Benefit from fifteen years of ESL experience. Use coupon code 'ESLPR' on registration for discount! Advertise here Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |