1+ let direction = 4 ;
2+ let row = 40 ;
3+ // let isThroughTheWall = true;
4+
5+ let headColor = 'red' ;
6+ let bodyColor = 'orange' ;
7+ let foodColor = 'green' ;
8+ let borderColor = 'grey' ;
9+ eaten = false ;
10+ head = {
11+ x : 3 ,
12+ y : 0
13+ } ;
14+ body = [
15+ { x : 2 , y : 0 } , { x : 1 , y : 0 } , { x : 0 , y : 0 }
16+ ] ;
17+ food = { x : 0 , y : 0 }
18+ document . onkeyup = function ( e ) {
19+ switch ( e . code ) {
20+ case "ArrowUp" : {
21+ if ( direction != 2 ) {
22+ direction = 1 ;
23+ }
24+ break ;
25+ }
26+ case "ArrowDown" : {
27+ if ( direction != 1 ) {
28+ direction = 2 ;
29+ }
30+ break ;
31+ }
32+ case "ArrowLeft" : {
33+ if ( direction != 4 ) {
34+ direction = 3 ;
35+ }
36+ break ;
37+ }
38+ case "ArrowRight" : {
39+ if ( direction != 3 ) {
40+ direction = 4 ;
41+ }
42+ break ;
43+ }
44+ case "Space" : {
45+ direction = 4 ;
46+ break ;
47+ }
48+ }
49+ }
50+
51+ document . onkeyup = function ( e ) {
52+ switch ( e . code ) {
53+ case "ArrowUp" : {
54+ if ( direction != 2 ) {
55+ direction = 1 ;
56+ }
57+ break ;
58+ }
59+ case "ArrowDown" : {
60+ if ( direction != 1 ) {
61+ direction = 2 ;
62+ }
63+ break ;
64+ }
65+ case "ArrowLeft" : {
66+ if ( direction != 4 ) {
67+ direction = 3 ;
68+ }
69+ break ;
70+ }
71+ case "ArrowRight" : {
72+ if ( direction != 3 ) {
73+ direction = 4 ;
74+ }
75+ break ;
76+ }
77+ // case "Space": {
78+ // direction = 5;
79+ // break;
80+ // }
81+ }
82+ }
83+
84+ // code捕获函数
85+ // document.onkeydown = function (e) {
86+ // console.log('code: ', e.code);
87+ // }
88+
89+ function Start ( ) {
90+ Init ( ) ;
91+ //蛇头蛇身食物上色
92+ document . getElementById ( head . x + "-" + head . y ) . style . backgroundColor = headColor ;
93+ for ( i of body ) {
94+ document . getElementById ( i . x + "-" + i . y ) . style . backgroundColor = bodyColor ;
95+ }
96+ //随机食物位置
97+ randomFood ( )
98+ //循环开始
99+ var loop = setInterval ( function Game ( ) {
100+ if ( isFail ( ) ) {
101+ clearInterval ( loop ) ;
102+ alert ( "Game Over" )
103+ } else {
104+ if ( ( food . x === head . x && food . y === head . y ) ) {
105+ eaten = true ;
106+ randomFood ( ) ;
107+ }
108+ //蛇头加入蛇身
109+ body . unshift ( { x : head . x , y : head . y } ) ;
110+ //蛇身上色
111+ document . getElementById ( head . x + "-" + head . y ) . style . backgroundColor = bodyColor ;
112+ //移动蛇头
113+ switch ( direction ) {
114+ case 1 :
115+ head . y -- ;
116+ break ;
117+ case 2 :
118+ head . y ++ ;
119+ break ;
120+ case 3 :
121+ head . x -- ;
122+ break ;
123+ case 4 :
124+ head . x ++ ;
125+ break ;
126+ }
127+ //蛇头上色
128+ document . getElementById ( head . x + "-" + head . y ) . style . backgroundColor = headColor ;
129+ //如果没吃东西去掉末尾的颜色并裁剪末尾
130+ if ( ! eaten ) {
131+ document . getElementById ( ( body [ body [ 'length' ] - 1 ] ) . x + "-" + ( body [ body [ 'length' ] - 1 ] ) . y ) . style . backgroundColor = ""
132+ body . pop ( )
133+ } else {
134+ eaten = false ;
135+ }
136+ }
137+ } , 300 ) ;
138+ }
139+
140+ function isFail ( ) {
141+ if ( isBody ( head . x , head . y ) ) {
142+ return true ;
143+ } else if ( ( head . x >= row || head . x < 0 ) || ( head . y >= row || head . y < 0 ) ) {
144+ return true ;
145+ } else {
146+ return false ;
147+ }
148+ }
149+
150+ function isBody ( x , y ) {
151+ ret = false ;
152+ for ( i of body ) {
153+ if ( ( i . x === x ) && ( i . y === y ) ) {
154+ ret = true ;
155+ }
156+ }
157+ return ret ;
158+ }
159+
160+ function moveUp ( ) {
161+ if ( direction == 1 ) // Right
162+ {
163+ if ( array [ i ] [ j ] == '*' ) {
164+ n ++ ; // length++
165+ p [ n ] = p [ n - 1 ] ;
166+ randomApple ( ) ;
167+ }
168+ }
169+ }
170+ function Init ( ) {
171+ //清空网页
172+ document . body . innerHTML = "" ;
173+ //边框大div
174+ let Container = document . createElement ( "div" ) ;
175+ Container . style . border = "1px solid " + borderColor ;
176+ //每个小格子的长宽均为20px
177+ Container . style . height = 20 * row + 1 + "px" ;
178+ Container . style . width = 20 * row + 1 + "px" ;
179+ Container . style . position = "relative" ;
180+ //写入网页
181+ document . body . appendChild ( Container ) ;
182+ //循环创建小div格子并给予一个唯一的id以便后面判断以及修改颜色
183+ for ( let i = 0 ; i < row ; i ++ ) {
184+ let marginTopI = 20 * i ;
185+ for ( let j = 0 ; j < row ; j ++ ) {
186+ let marginLeftj = 20 * j ;
187+ let Column = document . createElement ( "div" ) ;
188+ //绝对定位
189+ Column . style . position = "absolute" ;
190+ //记录id(x-y)
191+ Column . id = j + "-" + i ;
192+ //背景色白色
193+ Column . style . backgroundColor = "white" ;
194+ //长宽均为19px 边框0.5px
195+ Column . style . width = "19px" ;
196+ Column . style . height = "19px" ;
197+ Column . style . marginTop = marginTopI + "px" ;
198+ Column . style . marginLeft = marginLeftj + "px" ;
199+ Column . style . border = "0.5px solid " + borderColor ;
200+ //append到Container div
201+ Container . appendChild ( Column ) ;
202+
203+ }
204+ }
205+ }
206+ function randomFood ( ) {
207+ do {
208+ food . x = Math . floor ( Math . random ( ) * row )
209+ food . y = Math . floor ( Math . random ( ) * row )
210+ } while ( isBody ( food . x , food . y ) || ( food . x === head . x && food . y === head . y ) )
211+ document . getElementById ( food . x + "-" + food . y ) . style . backgroundColor = foodColor ;
212+ }
0 commit comments