How to use 63 lines of code in html5 to realize the game of gluttonous snake
This article mainly introduces how to use 63 lines of code to achieve the gluttonous Snake game in html5. The article is very detailed and has a certain reference value. Interested friends must read it!
The code is as follows:
Var c=document.getElementById ("myCanvas"); var time = 160,160; / Snake Speed var cxt=c.getContext ("2d"); var x = y = 8 interval var a = 0; / / Food coordinates var t = 20; / / body length var map = []; / / record snake running path var size = 8; / / Snake body unit size var direction = 2; / / 1 upward 2 to right 0 left 3 down interval = window.setInterval (set_game_speed, time) / / Mobile snake function set_game_speed () {/ / Mobile snake switch (direction) {case 1vex = y case sizescapebreakcase 2burex = xMusizescapebreakCase 0Wholx = xMusizescaptCase 3ZX;} if (x > 400 | | y > 400 | | x