Html+js Snake

Source: Internet
Author: User

Game Area Limited In a UL, each of the snakes are composed of Li, each Li is used position positioning. Food is created as a Div, each time a snake head touches food, creating a new Li,li will automatically load the tail of the snake and then change the location of the food by random number. By responding to the keyboard's WASD control the direction of the snake head (with the onkeypress event and KeyCode), with the timer control the snake head automatically walk, with the array to store the snake head each time the top and left values, with the loop to assign these top and left values to the Li behind the Snake head, So the snake will move with the snake's head.

Callout: red circle is food, snake Head is black.

The code is as follows:

<!        DOCTYPE html>

Html+js Snake

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.