This article mainly introduces the complete example of a snake web edition game implemented by native js, which allows you to choose game difficulty independently and implement the function of a snake game, involving javascript Keyboard Events and page element operation skills, for more information, see the examples in this article to describe the snake web game implemented by native js. Share it with you for your reference. The specific implementation method is as follows:
A browser game written in native jsScript Star = {init: function () {var bigDiv = this. appendEle (this. addStyle (this. creatEle (), {w: '000000', h: '000000', p: 'absolute ', t: 10, l: 900 })); for (var I = 0; I <600/30; I ++) {Star. data. arrayAll [I] = []; for (var j = 0; j <900/30; j ++) {p = this. addStyle (this. creatEle (), {w :(! + [1,]? 30: 28), h :(! + [1,]? 30: 28), f: 'left', border: '1px solid #666 '}); p. setAttribute ('number', I * 30 + j) this. appendEle (p, bigDiv) Star. data. arrayAll [I] [j] = p ;}} bigDiv = this. appendEle (this. addStyle (this. creatEle (), {w: '000000', h: '000000', p: 'absolute ', t: 10, l: 900}); this. pushEleInSelect (Star. data. arrayAll [9] [15], Star. data. arrayAll [9] [14], Star. data. arrayAll [9] [13]) this. keyBoard. apply (this, arguments); this. appearPoint (); this. leftGo () ;}, ap PearPoint: function () {var arrayIn = []; var number; for (var I = 0; I <600; I ++) {if (! This. hasInArray (Star. data. arrayAll [parseInt (I/30)] [I % 30]. getAttribute ('number'), Star. data. arraySelect) {arrayIn. push (Star. data. arrayAll [I])} Star. data. foodNumber = number = parseInt (Math. random () * arrayIn. length); this. giveColor (number)}, giveColor: function (number) {var p = Star. data. arrayAll [parseInt (number/30)] [number % 30]; Star. timeInterval. timeB = setInterval (function () {if (p. className = 'shanghai '){ P. className = ''p. style. backgroundColor = '# fff'} else {p. className = 'shanshanghai'; p. style. backgroundColor = '# f00' }}, 500)}, disappearColor: function () {clearInterval (Star. timeInterval. timeB); Star. data. arrayAll [parseInt (Star. data. foodNumber/30)] [Star. data. foodNumber % 30]. style. backgroundColor = '# f00';}, hasInArray: function (number, array) {for (var I in array) {if (array [I] instanceof Array) {if (this. hasInArray (Number, array [I]) {return true ;}} if (array [I]. getAttribute & array [I]. getAttribute ('number') = number) return true;} return false;}, keyBoard: function () {var self = this; document. onkeydown = function (e) {e = e? E: window. event; switch (e. keyCode) {case 37: if (Star. keycode = 37 | Star. keycode = 39) {return;}; self. leftGo (); break; case 38: if (Star. keycode = 38 | Star. keycode = 40) {return ;}; self. upGo (); break; case 39: if (Star. keycode = 37 | Star. keycode = 39) {return;}; self. rightGo (); break; case 40: if (Star. keycode = 38 | Star. keycode = 40) {return ;}; self. downGo (); break ;}}, leftGo: function () {var p, number, self = this; Star. keycode = 37; clearInterval (Star. timeInterval. timeA) Star. timeInterval. timeA = setInterval (function () {number = Star. data. arraySelect [0]. getAttribute ('number'); if (number % 30 <= 0 | self. hasInArray (number-1, Star. data. arraySelect) {self. guanle ();} else {if (Star. data. foodNumber = number-1) {self. pushEleInSelect (Star. data. arrayAll [parseInt (Star. data. foodNumber/30)] [Star. data. foodNumber % 30]); self. disappearColor (); self. appearPoint ();} else {p = Star. data. arraySelect. pop (); p. style. background = '# fff'; self. pushEleInSelect (Star. data. arrayAll [parseInt (number/30)] [number % 30-1]) ;}}, Star. timeInterval. speed)}, upGo: function () {var p, number, self = this; Star. keycode = 38; clearInterval (Star. timeInterval. timeA) Star. timeInterval. timeA = setInterval (function () {number = parseInt (Star. data. arraySelect [0]. getAttribute ('number'); if (parseInt (number/30) <= 0 | self. hasInArray (number-30, Star. data. arraySelect) {self. guanle ();} else {if (Star. data. foodNumber = number-30) {self. pushEleInSelect (Star. data. arrayAll [parseInt (Star. data. foodNumber/30)] [Star. data. foodNumber % 30]); self. disappearColor (); self. appearPoint ();} else {p = Star. data. arraySelect. pop (); p. style. background = '# fff'; self. pushEleInSelect (Star. data. arrayAll [parseInt (number/30)-1] [number % 30]) ;}}, Star. timeInterval. speed)}, rightGo: function () {var p, number, self = this; Star. keycode = 39; clearInterval (Star. timeInterval. timeA) Star. timeInterval. timeA = setInterval (function () {number = parseInt (Star. data. arraySelect [0]. getAttribute ('number'); if (parseInt (number % 30)> = 29 | self. hasInArray (number + 1, Star. data. arraySelect) {self. guanle ();} else {if (Star. data. foodNumber = number + 1) {self. pushEleInSelect (Star. data. arrayAll [parseInt (Star. data. foodNumber/30)] [Star. data. foodNumber % 30]); self. disappearColor (); self. appearPoint ();} else {p = Star. data. arraySelect. pop (); p. style. background = '# fff'; self. pushEleInSelect (Star. data. arrayAll [parseInt (number/30)] [number % 30 + 1]) ;}}, Star. timeInterval. speed)}, downGo: function () {var p, number, self = this; Star. keycode = 40; clearInterval (Star. timeInterval. timeA) Star. timeInterval. timeA = setInterval (function () {number = parseInt (Star. data. arraySelect [0]. getAttribute ('number'); if (parseInt (number/30)> = 19 | self. hasInArray (number + 30, Star. data. arraySelect) {self. guanle ();} else {if (Star. data. foodNumber = number + 30) {self. pushEleInSelect (Star. data. arrayAll [parseInt (Star. data. foodNumber/30)] [Star. data. foodNumber % 30]); self. disappearColor (); self. appearPoint ();} else {p = Star. data. arraySelect. pop (); p. style. background = '# fff'; self. pushEleInSelect (Star. data. arrayAll [parseInt (number/30) + 1] [number % 30]) ;}}, Star. timeInterval. speed)}, guanle: function () {alert ('hit the wall, total score: '+ (Star. data. arraySelect. length-3) * parseInt (1000/Star. timeInterval. speed); location. reload () ;}, creatEle: function (tag) {var tagName = tag | 'div 'return document. createElement (tagName)}, appendEle: function (ele, father) {var father = father | document. body | document.doc umentElementfather. appendChild (ele) return ele;}, addStyle: function (ele, css) {for (var I in css) {switch (I) {case 'B': ele. style. background = css [I]; break; case 'l': ele. style. left = css [I] + 'px '; break; case 'r': ele. style. right = css [I] + 'px '; break; case 'T': ele. style. top = css [I] + 'px '; break; case 'D': ele. style. down = css [I] + 'px '; break; case 'p': ele. style. position = css [I]; break; case 'W': ele. style. width = css [I] + 'px '; break; case 'H': ele. style. height = css [I] + 'px '; break; case 'F': ele.style.css Float = css [I]; ele. style. styleFloat = css [I]; break; default: ele. style [I] = css [I]; break;} return ele;}, pushEleInSelect: function () {for (var I = 0; I
I hope this article will help you design javascript programs.