The example of this article for everyone to share the relevant code JS snake game, for your reference, the specific content as follows
Effect Chart:
Ideas:
function Newgame () {Reset
the data of snakes and fractions;
clear interval;
initialize canvas;
draw a snake;
place food;
Use the timer (setinterval) to make the snake move (moving function);
}
function Moves () {
Depending on the direction, change the position of the snake's head next to
it; determine if the game is over, And the reason for the end (including winning and losing); the
snake moves one cell (Movein function),
monitors the keyboard's arrow keys, modifies the value of the global variable Dir (for the storage direction) when changing the direction;
function Movein ( {Add a lattice to the front of the snake head and add the
coordinates of the snake head as the first element to the array representing the snake;
if (did not eat food) {
Remove the tail of a lattice and make a corresponding change in the canvas;
}
Note that if you need to set the width and height of canvas in JS, it is slightly different from setting other CSS properties.
Canvas.width = MaxWidth; Canvas.height = MaxHeight;
The small series also prepares the splendid topic for everybody:JavaScript Classic Small Game summary
The above is the entire content of this article, I hope to learn JavaScript program to help you.