Original JS realization of the Snake Web version of the game complete example

Source: Internet
Author: User
Tags setinterval tagname

This article illustrates the original JS implementation of the snake Web version of the game. Share to everyone for your reference. The implementation methods are as follows:

?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148-149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179-18 0 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209-210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240-24 1 <html> <head> <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/> <title> native js written by Snake Web version game </title> </head> <body> </body> <script > Star = {init:function () {var bigdiv = This.appendele (This.addstyle (This.createle (), {w: ' 900 ', h: ' m ', p: ' absolute ') , t:10,l:500})); for (var i = 0; i<600/30;i++) {star.data.arrayall[i] = []; for (var j = 0; j<900/30; j + +) {div = This.addstyle (this.cr Eatele (), {w: (!+[1,]? 30:28), H: (!+[1,]? 30:28), F: ' Left ', border: ' 1px solid #666 '}); Div.setattribute (' number ', I*30+j) This.appendele (div,bigdiv) star.data.arrayall[i][j] = div; } Bigdiv = This.appendele (This.addstyle (This.createle (), {w: ' 900 ', h: ' N ', p: ' Absolute ', t:10,l:500})); 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 (); }, Appearpoint: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 div = star.data.arrayall[parseint (NUMBER/30)][number%30]; Star.timeInterval.timeB = setinterval (function () {if (Div.classname = ' Shanshuo ') {div.classname = ' Div.style.backgroundColor = ' #fff '} else{div.classname = ' Shanshuo '; div.style.backgroundColor = ' #f00 '}},500)}, Disa Ppearcolor: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.K Eycode = 37 | | Star.keycode = =) {return;}; Self.leftgo (); Break Case 38:if (Star.keycode = 38 | | Star.keycode = =) {return;}; Self.upgo (); Case 39:if (star.keycode = 37 | | Star.keycode = =) {return;}; Self.rightgo (); Case 40:if (Star.keycode = 38 | | Star.keycode = =) {return;}; Self.downgo (); }}, Leftgo:function () {var div, 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{div = Star.data.arraySelect.pop (); div.style.baCkground = ' #fff '; Self.pusheleinselect (Star.data.arrayall[parseint (NUMBER/30)][number%30-1]); }},star.timeinterval.speed)}, Upgo:function () {var div, 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{div = Star.data.arraySelect.pop (); div.style.background = ' #fff '; Self.pusheleinselect (star.data.arrayall[ parseint (NUMBER/30) -1][number%30]); }},star.timeinterval.speed)}, Rightgo:function () {var div, 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{div = Star.data.arraySelect.pop (); div.style.background = ' #fff '; Self.pusheleinselect (star.data.arrayall[ parseint (NUMBER/30)][number%30+1]); }},star.timeinterval.speed)}, Downgo:function () {var div, 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{div = Star.data.arraySelect.pop (); div.style.background = ' #fff '; Self.pusheleinselect (star.data.arrayall[ parseint (NUMBER/30) +1][number%30]); }},star.timeinterval.speed)}, Guanle:function () {alert (' wall up, 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 | | Cument.documentelement Father.appendchild (ele) return ele; }, Addstyle:function (ELE,CSS) {for (var i in CSS) {switch (i) {case ' B ': Ele.style.background = css[i]; The case ' l ': E Le.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.cssFloat = css[i]; Ele.style.styleFloat = Css[i]; Break Default:ele.style[i] = css[i]; Break } return ele; }, Pusheleinselect:function () {for (var i = 0; i<arguments.length; i++) {Star.data.arraySelect = [arguments[i]].concat (Star.data.arraySelect) This.addstyle (arguments[i],{b: ' #f00 '})}} star.data={Arrayall: [], arrayselect:[], Newpoint : null, foodnumber:0} star.timeinterval={timea:null, timeb:null} star.keycode = 0; Window.onload = function () {var select = Star.createle (' select '); var optiondefault = star.createle (' option '); Optiondefa ult.innerhtml = ' Please select Checkpoint ' Star.appendele (optiondefault,select) Star.addstyle (select,{w:200,h:30,p: ' absolute ', left: ' 40% ', top: ' 40% '}] for (var i = 0; I <10 i++) {var option = star.createle (' option '); option.innerhtml = ' n ' + (i+1) + ' Guan ' Star.appendele (option,select); } star.appendele (select) Select.onchange = function () {Selectvalue = Select.options[select.selectedindex].value|| select.options[select.selectedindex].innerhtml var number = Selectvalue.match (/d+/) [0] Star.timeInterval.speed = parseint (200/number); Star.addstyle (select,{display: ' None '}); Star.init (); }} </script> </html>

The

wants this article to help you with your JavaScript programming.

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.