JS Greedy snake game to achieve ideas and source _javascript skills

Source: Internet
Author: User

The example of this article for everyone to share the relevant code JS snake game, for your reference, the specific content as follows

<!
  DOCTYPE html>  

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.