脚本的厉害——贪食蛇
这是朋友给我的一个例子
用JavaScript写的贪食蛇,给大家展示一下:
脚本真是厉害!!!
用JavaScript写的贪食蛇,给大家展示一下:
脚本真是厉害!!!
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<HTML>
3<HEAD>
4<TITLE> New Document </TITLE>
5<META NAME="Generator" CONTENT="EditPlus">
6<META NAME="Author" CONTENT="">
7<META NAME="Keywords" CONTENT="">
8<META NAME="Description" CONTENT="">
9<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
10<STYLE>
11.btnup{
12 border-left:solid #ffffff 1px;
13 border-top:solid #ffffff 1px;
14 border-right:solid #828486 1px;
15 border-bottom:solid #828486 1px;
16 font-size: 9pt;
17 cursor:default
18 }
19tr{
20 font-family:"宋体";
21 font-size: 9pt;
22 cursor: default
23}
24</STYLE>
25</HEAD>
26<SCRIPT LANGUAGE="JavaScript">
27<!--
28var row = 25,col = 25,size=16;
29var delay = 300;//移动延迟
30var paused = false;
31var end = false
32var TimerID;
33var s = new snake();
34var n = new nut();
35var nutad = new Array();
36nutad[0] = -1;
37nutad[1] = -1;
38var Color = new Array(5);
39Color[0] = "#d0d0d0";
40Color[1] = "red";
41Color[2] = "green"
42Color[3] = "blue";
43Color[4] = "yellow";
44var move_direction = new Array();
45var curr_direction = new Array();
46move_direction[0] = 1;
47move_direction[1] = 0;
48
49//========================================
50//定义果实
51function nut()
52{
53 this.x = -1;
54 this.y = -1;
55}
56
57function nut_show()
58{
59 if (this.x<0 || this.y < 0 )
60 return;
61 obj = document.all("Main" + this.x + "#" + this.y);
62 obj.style.background = Color[2];
63}
64
65function nut_create()
66{
67 var inx = -1;
68 var iny = -1;
69 for(;;)
70 {
71 var cur = true;
72 inx = Math.round(Math.random() * (row-1));
73 iny = Math.round(Math.random() * (col-1));
74 for (var i = 0; i<s.items.length ; i++)
75 {
76 if (s.items[i].x == inx && s.items[i].y == iny)
77 {
78 cur = false;
79 break;
80 }
81 }
82 if (cur)
83 {
84 break;
85 }
86 }
87 this.x = inx;
88 this.y = iny;
89 nutad[0] = inx;
90 nutad[1] = iny;
91 this.show();
92}
93
94function nut_eated()
95{
96 if (this.x<0 || this.y < 0)
97 return;
98 obj = document.all("Main" + this.x + "#" + this.y);
99 obj.style.background = Color[0];
100}
101nut.prototype.create = nut_create;
102nut.prototype.show = nut_show;
103nut.prototype.eated = nut_eated;
104
105//========================================
106//定义蛇体
107function snake()
108{
109 this.items = new Array();
110}
111
112function snake_eat(nt)
113{
114 var s_length = this.items.length;
115 this.items[s_length] = nt;
116}
117
118function snake_move(dir)
119{
120 this.destroy();
121 var y = this.items[0].y;
122 var x = this.items[0].x;
123 var nx = x + dir[1];
124 var ny = y + dir[0];
125 if (nx <0 || nx >= row || ny <0 || ny >= col || crossed(nx,ny))
126 {
127 end = true;
128 return;
129 }
130 caneat(nx,ny);
131 for (var i = this.items.length-1 ; i>=0 ; i --)
132 {
133
134 if (i != 0)
135 {
136 this.items[i].move(this.items[i-1].x,this.items[i-1].y);
137 }
138 else
139 {
140 this.items[i].move(nx,ny);
141 }
142 }
143 this.reload();
144}
145
146function caneat(nx,ny)
147{
148 if (nx == nutad[0] && ny == nutad[1])
149 {
150 var lst = s.items[s.items.length-1];
151 var nd = new snakeNode(lst.x,lst.y);
152 s.eat(nd);
153 n.eated();
154 n.create();
155 }
156
157}
158//禁止穿越自身节点
159function crossed( nx, ny )
160{
161 for (var i = 0; i<s.items.length; i++)
162 {
163 if (s.items[i].x == nx && s.items[i].y == ny)
164 {
165 return true;
166 }
167 }
168 return false;
169}
170
171function snake_reload()
172{
173 for (var i = 0 ; i<this.items.length ; i ++)
174 {
175 var curNode = this.items[i];
176 obj = document.all("Main" + curNode.x + "#" + curNode.y);
177 obj.style.background = Color[1];
178 }
179}
180
181function snake_destroy()
182{
183 for (var i = 0 ; i<this.items.length ; i ++)
184 {
185 var curNode = this.items[i];
186 obj = document.all("Main" + curNode.x + "#" + curNode.y);
187 obj.style.background = Color[0];
188 }
189}
190
191function snake_clear()
192{
193 this.destroy();
194 this.items = new Array();
195}
196
197snake.prototype.eat = snake_eat;
198snake.prototype.move = snake_move;
199snake.prototype.reload = snake_reload;
200snake.prototype.destroy = snake_destroy;
201snake.prototype.clear = snake_clear;
202
203//蛇体节点
204function snakeNode(x,y)
205{
206 this.id = 0;//编号
207 this.x = x;//坐标X
208 this.y = y;//坐标Y
209 this.color = "#000000";
210}
211
212function snakeNode_setC(d_color)
213{
214 this.color = d_color;
215}
216
217function snakeNode_move(dx,dy)
218{
219 this.x = dx;
220 this.y = dy;
221}
222
223snakeNode.prototype.setColor = snakeNode_setC ;
224snakeNode.prototype.move = snakeNode_move ;
225
226//========================================
227//程序界面
228//初始化主控制区
229function DrawArea(row,col,name){
230 var s = "<TABLE BORDER=1 cellspacing=0 cellpadding=1 bgcolor=" + Color[0] + ">";
231 for(var i=0; i<row; i++){
232 s = s + "<TR Height=" + size + ">";
233 for(var j=0; j<col; j++){
234 var id = name + i + "#" + j;
235 s = s + "<TD Width=" + size + " class=btnup id=" + id;
236 s = s + " style=\"background:" + Color[0] + "\"> </TD>"
237 }
238 s = s + "</TR>";
239 }
240 s = s + "</TABLE>";
241 return s;
242}
243
244//初始化
245function Init(){
246 MainArea.innerHTML = DrawArea(row,col,'Main');
247}
248
249//==================================
250//程序控制区,用来接收输入、定义控制方法
251//控制输入
252function keyDown(){
253 switch(event.keyCode)
254 {
255 case 37:
256 if (curr_direction[0] != -1)
257 {
258 move_direction[0] = -1;
259 move_direction[1] = 0;
260 }
261 break;
262 case 38:
263 if (curr_direction[1] != -1)
264 {
265 move_direction[0] = 0;
266 move_direction[1] = -1;
267 }
268 break;
269 case 39:
270 if (curr_direction[0] != 1)
271 {
272 move_direction[0] = 1;
273 move_direction[1] = 0;
274 }
275 break;
276 case 40:
277 if (curr_direction[1] != 1)
278 {
279 move_direction[0] = 0;
280 move_direction[1] = 1;
281 }
282 break;
283 }
284}
285
286function Begin()
287{
288 end = false;
289 paused = false;
290 s.clear();
291 for (var i = 8; i >=0 ; i--)
292 {
293 sn = new snakeNode(0,i);
294 s.eat(sn);
295 }
296 n.eated();
297 n.create();
298 move_direction[0] = 1;
299 move_direction[1] = 0;
300 curr_direction[1] = s.items[1].x - s.items[0].x;
301 curr_direction[0] = s.items[1].y - s.items[0].y;
302 Start();
303}
304
305//启动开关
306function Start()
307{
308 if (end)
309 {
310 s.reload();
311 alert("挂了!");
312 return;
313 }
314 window.clearInterval(TimerID);
315 TimerID = window.setInterval("Run()",delay);
316}
317
318//运行主体
319var d = true;
320function Run(){
321 if(paused) return;
322 if(true){
323 window.clearInterval(TimerID);
324 s.move(move_direction);
325 curr_direction[1] = s.items[1].x - s.items[0].x;
326 curr_direction[0] = s.items[1].y - s.items[0].y;
327 Start();
328 }
329 }
330
331//暂停开关
332function Pause()
333{
334 if (paused)
335 {
336 paused = false;
337 if (!end)
338 {
339 Run();
340 }
341 }
342 else
343 {
344 paused = true;
345 }
346}
347
348//结束
349function Over()
350{
351 end = true;
352}
353//-->
354</SCRIPT>
355<BODY BGCOLOR="#FFFFFF" onload="Init()" onkeydown="keyDown()" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
356<TABLE border="0" cellspacing="0" cellpadding="0" align="center" width="80%">
357 <TR>
358<TR valign="top" width="80%">
359 <TD><SPAN ID="MainArea"></SPAN></TD>
360 <TD align="center">
361 <BUTTON ID="start" onclick="Begin()">Start</BUTTON><br>
362 <BUTTON ID="pause" onclick="Pause()">pause</BUTTON><br>
363 <BUTTON ID="over" onclick="Over()">End</BUTTON><br>
364 </TD>
365</TR>
366</TABLE>
367</BODY>
368</html>
369
2<HTML>
3<HEAD>
4<TITLE> New Document </TITLE>
5<META NAME="Generator" CONTENT="EditPlus">
6<META NAME="Author" CONTENT="">
7<META NAME="Keywords" CONTENT="">
8<META NAME="Description" CONTENT="">
9<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
10<STYLE>
11.btnup{
12 border-left:solid #ffffff 1px;
13 border-top:solid #ffffff 1px;
14 border-right:solid #828486 1px;
15 border-bottom:solid #828486 1px;
16 font-size: 9pt;
17 cursor:default
18 }
19tr{
20 font-family:"宋体";
21 font-size: 9pt;
22 cursor: default
23}
24</STYLE>
25</HEAD>
26<SCRIPT LANGUAGE="JavaScript">
27<!--
28var row = 25,col = 25,size=16;
29var delay = 300;//移动延迟
30var paused = false;
31var end = false
32var TimerID;
33var s = new snake();
34var n = new nut();
35var nutad = new Array();
36nutad[0] = -1;
37nutad[1] = -1;
38var Color = new Array(5);
39Color[0] = "#d0d0d0";
40Color[1] = "red";
41Color[2] = "green"
42Color[3] = "blue";
43Color[4] = "yellow";
44var move_direction = new Array();
45var curr_direction = new Array();
46move_direction[0] = 1;
47move_direction[1] = 0;
48
49//========================================
50//定义果实
51function nut()
52{
53 this.x = -1;
54 this.y = -1;
55}
56
57function nut_show()
58{
59 if (this.x<0 || this.y < 0 )
60 return;
61 obj = document.all("Main" + this.x + "#" + this.y);
62 obj.style.background = Color[2];
63}
64
65function nut_create()
66{
67 var inx = -1;
68 var iny = -1;
69 for(;;)
70 {
71 var cur = true;
72 inx = Math.round(Math.random() * (row-1));
73 iny = Math.round(Math.random() * (col-1));
74 for (var i = 0; i<s.items.length ; i++)
75 {
76 if (s.items[i].x == inx && s.items[i].y == iny)
77 {
78 cur = false;
79 break;
80 }
81 }
82 if (cur)
83 {
84 break;
85 }
86 }
87 this.x = inx;
88 this.y = iny;
89 nutad[0] = inx;
90 nutad[1] = iny;
91 this.show();
92}
93
94function nut_eated()
95{
96 if (this.x<0 || this.y < 0)
97 return;
98 obj = document.all("Main" + this.x + "#" + this.y);
99 obj.style.background = Color[0];
100}
101nut.prototype.create = nut_create;
102nut.prototype.show = nut_show;
103nut.prototype.eated = nut_eated;
104
105//========================================
106//定义蛇体
107function snake()
108{
109 this.items = new Array();
110}
111
112function snake_eat(nt)
113{
114 var s_length = this.items.length;
115 this.items[s_length] = nt;
116}
117
118function snake_move(dir)
119{
120 this.destroy();
121 var y = this.items[0].y;
122 var x = this.items[0].x;
123 var nx = x + dir[1];
124 var ny = y + dir[0];
125 if (nx <0 || nx >= row || ny <0 || ny >= col || crossed(nx,ny))
126 {
127 end = true;
128 return;
129 }
130 caneat(nx,ny);
131 for (var i = this.items.length-1 ; i>=0 ; i --)
132 {
133
134 if (i != 0)
135 {
136 this.items[i].move(this.items[i-1].x,this.items[i-1].y);
137 }
138 else
139 {
140 this.items[i].move(nx,ny);
141 }
142 }
143 this.reload();
144}
145
146function caneat(nx,ny)
147{
148 if (nx == nutad[0] && ny == nutad[1])
149 {
150 var lst = s.items[s.items.length-1];
151 var nd = new snakeNode(lst.x,lst.y);
152 s.eat(nd);
153 n.eated();
154 n.create();
155 }
156
157}
158//禁止穿越自身节点
159function crossed( nx, ny )
160{
161 for (var i = 0; i<s.items.length; i++)
162 {
163 if (s.items[i].x == nx && s.items[i].y == ny)
164 {
165 return true;
166 }
167 }
168 return false;
169}
170
171function snake_reload()
172{
173 for (var i = 0 ; i<this.items.length ; i ++)
174 {
175 var curNode = this.items[i];
176 obj = document.all("Main" + curNode.x + "#" + curNode.y);
177 obj.style.background = Color[1];
178 }
179}
180
181function snake_destroy()
182{
183 for (var i = 0 ; i<this.items.length ; i ++)
184 {
185 var curNode = this.items[i];
186 obj = document.all("Main" + curNode.x + "#" + curNode.y);
187 obj.style.background = Color[0];
188 }
189}
190
191function snake_clear()
192{
193 this.destroy();
194 this.items = new Array();
195}
196
197snake.prototype.eat = snake_eat;
198snake.prototype.move = snake_move;
199snake.prototype.reload = snake_reload;
200snake.prototype.destroy = snake_destroy;
201snake.prototype.clear = snake_clear;
202
203//蛇体节点
204function snakeNode(x,y)
205{
206 this.id = 0;//编号
207 this.x = x;//坐标X
208 this.y = y;//坐标Y
209 this.color = "#000000";
210}
211
212function snakeNode_setC(d_color)
213{
214 this.color = d_color;
215}
216
217function snakeNode_move(dx,dy)
218{
219 this.x = dx;
220 this.y = dy;
221}
222
223snakeNode.prototype.setColor = snakeNode_setC ;
224snakeNode.prototype.move = snakeNode_move ;
225
226//========================================
227//程序界面
228//初始化主控制区
229function DrawArea(row,col,name){
230 var s = "<TABLE BORDER=1 cellspacing=0 cellpadding=1 bgcolor=" + Color[0] + ">";
231 for(var i=0; i<row; i++){
232 s = s + "<TR Height=" + size + ">";
233 for(var j=0; j<col; j++){
234 var id = name + i + "#" + j;
235 s = s + "<TD Width=" + size + " class=btnup id=" + id;
236 s = s + " style=\"background:" + Color[0] + "\"> </TD>"
237 }
238 s = s + "</TR>";
239 }
240 s = s + "</TABLE>";
241 return s;
242}
243
244//初始化
245function Init(){
246 MainArea.innerHTML = DrawArea(row,col,'Main');
247}
248
249//==================================
250//程序控制区,用来接收输入、定义控制方法
251//控制输入
252function keyDown(){
253 switch(event.keyCode)
254 {
255 case 37:
256 if (curr_direction[0] != -1)
257 {
258 move_direction[0] = -1;
259 move_direction[1] = 0;
260 }
261 break;
262 case 38:
263 if (curr_direction[1] != -1)
264 {
265 move_direction[0] = 0;
266 move_direction[1] = -1;
267 }
268 break;
269 case 39:
270 if (curr_direction[0] != 1)
271 {
272 move_direction[0] = 1;
273 move_direction[1] = 0;
274 }
275 break;
276 case 40:
277 if (curr_direction[1] != 1)
278 {
279 move_direction[0] = 0;
280 move_direction[1] = 1;
281 }
282 break;
283 }
284}
285
286function Begin()
287{
288 end = false;
289 paused = false;
290 s.clear();
291 for (var i = 8; i >=0 ; i--)
292 {
293 sn = new snakeNode(0,i);
294 s.eat(sn);
295 }
296 n.eated();
297 n.create();
298 move_direction[0] = 1;
299 move_direction[1] = 0;
300 curr_direction[1] = s.items[1].x - s.items[0].x;
301 curr_direction[0] = s.items[1].y - s.items[0].y;
302 Start();
303}
304
305//启动开关
306function Start()
307{
308 if (end)
309 {
310 s.reload();
311 alert("挂了!");
312 return;
313 }
314 window.clearInterval(TimerID);
315 TimerID = window.setInterval("Run()",delay);
316}
317
318//运行主体
319var d = true;
320function Run(){
321 if(paused) return;
322 if(true){
323 window.clearInterval(TimerID);
324 s.move(move_direction);
325 curr_direction[1] = s.items[1].x - s.items[0].x;
326 curr_direction[0] = s.items[1].y - s.items[0].y;
327 Start();
328 }
329 }
330
331//暂停开关
332function Pause()
333{
334 if (paused)
335 {
336 paused = false;
337 if (!end)
338 {
339 Run();
340 }
341 }
342 else
343 {
344 paused = true;
345 }
346}
347
348//结束
349function Over()
350{
351 end = true;
352}
353//-->
354</SCRIPT>
355<BODY BGCOLOR="#FFFFFF" onload="Init()" onkeydown="keyDown()" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
356<TABLE border="0" cellspacing="0" cellpadding="0" align="center" width="80%">
357 <TR>
358<TR valign="top" width="80%">
359 <TD><SPAN ID="MainArea"></SPAN></TD>
360 <TD align="center">
361 <BUTTON ID="start" onclick="Begin()">Start</BUTTON><br>
362 <BUTTON ID="pause" onclick="Pause()">pause</BUTTON><br>
363 <BUTTON ID="over" onclick="Over()">End</BUTTON><br>
364 </TD>
365</TR>
366</TABLE>
367</BODY>
368</html>
369