-
Notifications
You must be signed in to change notification settings - Fork 0
/
snakeeatbean.html
549 lines (490 loc) · 14.4 KB
/
snakeeatbean.html
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
43
44
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
84
85
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
119
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
180
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
<title>SnakeEatBean</title>
<script>
window.onload=function(){
//生成一个50x50的表格
var tableString = "";
for(var i=0;i<50;i++){
tableString +="<tr>";
for(var j=0;j<50;j++){
tableString +="<td></td>"
}
tableString += "</tr>";
}
var thisTable = document.getElementById("main");
thisTable.innerHTML = tableString;
//注册事件
function addEvent(target,type,handler){
if(target.addEventListener){
target.addEventListener(type,handler,false);
}
else if(target.attachEvent){
target.attachEvent("on" + type,function(event){
return handler.call(target,event);
});
}
else{
target['on' + type] = handler;
}
}
//bean对象
var bean = {
colors:['#d54e21','#f70','#ffc414','#78a300','#0e76a8','#6a5a8c'], //豆子的颜色
//产生豆子
createBean:function(){
while(true){
var x = Math.floor(Math.random()*50),
y = Math.floor(Math.random()*50),
colorIndex = Math.floor(Math.random()*6);
if(!Snake.isCellFilled(x,y)){
thisTable.rows[y].cells[x].style.backgroundColor = this.colors[colorIndex];
break;
}
}
},
}
//Snake对象
var Snake = {
body:[{x:25,y:25}],//每一节身体都用{x:x,y:y}表示
direction:0,//移动方向 0,1,2,3 分别表示 上,右,下,左
directionBuffer:[], //方向缓存数组
speed:125, //当前速度
allSpeed:{ // 三种不同的速度
slow:200,
middle:125,
fast:75,
},
colorNow:'#666', //现在蛇的颜色
timer:null, //把定时器加到这里,消除的时候用
runFlag:true, //是否在运行
initFlag:false, //是否已经初始化
aliveFlag:true, //是否还活着
startFlag:false, //是否已经开始
//初始化蛇
init:function(){
this.direction = Math.floor(Math.random()*4);
thisTable.rows[25].cells[25].style.backgroundColor = "#666";
for(var i=0;i<3;i++){
bean.createBean();
}
addEvent(document,"keydown",Snake.moveSwitch);
this.move();
this.initFlag = true;
},
//变换方向
moveSwitch:function(event){
var code = event.keyCode;
switch(code){
case 65://左
case 37:{
Snake.directionBuffer.push(3);
break;
}
case 87://上
case 38:{
Snake.directionBuffer.push(0);
break;
}
case 68://右
case 39:{
Snake.directionBuffer.push(1);
break;
}
case 83://下
case 40:{
Snake.directionBuffer.push(2);
break;
}
case 32:{//暂停
Snake.suspend();
break;
}
}
},
//确定方向
confirmDirection:function(){
var directionWill;
if(this.directionBuffer.length === 0){
;
}
else{
directionWill = this.directionBuffer.shift();
if(Math.abs(directionWill-this.direction) === 2){
;
}
else{
this.direction = directionWill;
}
}
},
//移动功能
move: function(){
this.timer = setInterval(function(){
Snake.confirmDirection();
var nextPod = Snake.getNextPos(),
x = nextPod.x,
y = nextPod.y,
check = Snake.checkNextStep();
if(check === -1){ //死亡
clearInterval(Snake.timer);
document.getElementById("game_over_blank").style.display = "block";
Snake.aliveFlag = false;
}
else if(check === 1){ //前面有豆子
Snake.addNode(x,y);
Snake.colorNow = thisTable.rows[y].cells[x].style.backgroundColor;
Snake.eraseNode();
Snake.paintAllNode();
bean.createBean();
Score.addScore();
}
else{ //正常行进
Snake.addNode(x,y);
Snake.eraseNode();
Snake.popNode();
Snake.paintNode(x,y);
}
}, this.speed);
},
//暂停功能
suspend:function(){
if(Snake.aliveFlag){
if (this.runFlag) {
clearInterval(this.timer);
this.runFlag = false;
document.getElementById("suspend_blank").style.display = "block";
}
else{
document.getElementById("suspend_blank").style.display = "none";
this.move();
this.runFlag = true;
}
}
},
//蛇头前添加节点
addNode:function(x,y){
this.body.unshift({x:x,y:y});
},
//抛弃蛇尾巴
popNode:function(){
this.body.pop();
},
//擦除最后一个节点
eraseNode: function(){
var bodylength = this.body.length,
node_y = this.body[bodylength-1].y,
node_x = this.body[bodylength-1].x;
thisTable.rows[node_y].cells[node_x].style.backgroundColor = "";
},
//绘制新节点
paintNode: function(x,y){
thisTable.rows[y].cells[x].style.backgroundColor = this.colorNow;
},
//变颜色
paintAllNode:function(){
for (x in this.body){
this.paintNode(this.body[x].x,this.body[x].y);
}
},
//检测一个格子是不是被填充
isCellFilled:function(x,y){
if(thisTable.rows[y].cells[x].style.backgroundColor == ""){
return false;
}
return true;
},
//检查下一步状态
checkNextStep: function(){
var nextPod = this.getNextPos();
var x = nextPod.x;
var y = nextPod.y;
if(x<0||x>49||y<0||y>49){
return -1;
}
for(i in this.body){
if(this.body[i].x==x&&this.body[i].y==y){
return -1;
}
}
if(this.isCellFilled(x,y)){
return 1;
}
return 0;
},
//得到将要到达的位置
getNextPos: function(){
var x = this.body[0].x;
var y = this.body[0].y;
if(this.direction==0){
y--;
}
else if(this.direction==1){
x++;
}
else if(this.direction==2){
y++;
}
else{
x--;
}
return {x:x,y:y};
}
}
//计分系统
var theScore = document.getElementById("score");
var Score = {
score:0,
addScore:function(){
this.score+=1;
theScore.innerHTML = "分数: "+this.score;
}
}
//开始按钮以及功能
var start_button = document.getElementById("start_button");
addEvent(start_button,"click",function(){
if(Snake.startFlag){
;
}else{
Snake.init();
Snake.startFlag = true;
}
});
//速度系统
var speed_all = document.querySelectorAll("#speed_radio li"),
speed_all_length = speed_all.length;
for(var i = speed_all_length-1 ; i>=0 ; i--){
addEvent(speed_all[i],"click",switchSpeed);
}
function switchSpeed(event){
for(var i = 0;i<speed_all_length;i++){
if(speed_all[i].className==="selected"){
speed_all[i].className = "";
}
}
Snake.speed = Snake.allSpeed[event.target.id]
if(Snake.initFlag === false){
;
}
else{
if(Snake.runFlag === true){
clearInterval(Snake.timer);
Snake.move();
}
else{
clearInterval(Snake.timer);
}
}
event.target.className = "selected";
}
//死亡时候出现的div,离开和再试一次功能
var leave_game = document.getElementById("leave_game"),
retry_game = document.getElementById("retry_game");
addEvent(leave_game,"click",leaveGameFun);
addEvent(retry_game,"click",retryGameFun);
function leaveGameFun(){
window.open('','_self');
window.close();
}
function retryGameFun(){
window.location.reload();
}
}
</script>
<style>
#wrapper{
width:500px;
margin:10px auto;
}
#content{
position: relative;
margin-top:10px;
width:500px;
}
table{
margin: 0px;
width: 500px;
height: 500px;
border-collapse: collapse;
border: solid #777 1px;
}
td{
height: 10px;
width: 10px;
padding: 0px;
margin: 0px;
border:0px;
}
#score_blank{
overflow: hidden;
}
#score{
font-size: 20px;
float: left;
}
#start_button{
float: right;
height:30px;
width: 60px;
border:none;
color:white;
background-color: #3A8000;
}
#speed_radio{
float: right;
height: 30px;
width: 130px;
}
ul{
overflow: hidden;
list-style: none;
width: 100%;
margin: 0;
padding: 0;
}
li{
float: left;
margin:2px 5px 0 0;
padding: 2px;
color:white;
}
#start_button:hover{
background-color: #3A9000;
}
#footer{
padding: 5px;
font-size: 12px;
color: #666;
}
#footer a{
text-decoration: none;
color: #666;
}
#footer a:hover{
text-decoration: underline;
color: #666;
}
#slow{
background-color: #fc0;
}
#slow:hover{
background-color: #fd0;
}
#middle{
background-color: #f70;
}
#middle:hover{
background-color: #f80;
}
#fast{
background-color: #d54e21;
}
#fast:hover{
background-color: #d55f21;
}
.selected{
margin:-2px 5px 0 0;
padding:2px 2px 6px 2px;
}
#game_over_blank{
display: none;
position:absolute;
z-index: 1;
width:200px;
height:100px;
top:200px;
left:50%;
margin-left: -100px;
text-align: center;
border: solid #777 1px;
background-color: white;
text-align: center;
}
#game_over_blank p{
margin:5px;
}
#retry_game{
position:absolute;
height:30px;
width: 70px;
top:35px;
left:20px;
margin: 0px;
border:none;
color:white;
background-color: #1b7e5a;
}
#retry_game:hover{
background-color: #78a300;
}
#leave_game{
position:absolute;
height:30px;
width: 70px;
top:35px;
left:110px;
margin: 0px;
border:none;
color:white;
background-color: #d54e21;
}
#leave_game:hover{
background-color: #e56c69;
}
#suspend_blank{
display: none;
position:absolute;
z-index: 1;
width:200px;
height:100px;
top:200px;
left:50%;
margin-left: -100px;
text-align: center;
border: solid #777 1px;
background-color: white;
text-align: center;
}
#suspend_blank p{
margin:15px 0px;
}
</style>
</head>
<body>
<div id = "wrapper">
<div id = "score_blank">
<div id="score">分数:0</div>
<button id="start_button">开始</button>
<div id="speed_radio">
<ul>
<li id="slow" class="">慢</li>
<li id="middle" class="selected">中</li>
<li id="fast" class="">快</li>
</ul>
</div>
</div>
<div id = "content">
<div id = "game_over_blank">
<p>游戏结束</p>
<button id = "retry_game">再试一次</button>
<button id = "leave_game">离开游戏</button>
</div>
<div id = "suspend_blank">
<p>暂停</p>
<p>按下空格继续</p>
</div>
<table id="main"></table>
</div>
<div id = "footer">
<p>点击开始按钮开始游戏,上下左右或wsad键控制小蛇,按空格暂停/继续</p>
<p>吃掉五颜六色的豆子得分,并且蛇的身体会变成豆子的颜色</p>
<p>点击上方的速度按钮可以选择速度</p>
<p>撞到墙和自己会死</p>
<a href="http://weibo.com/zyxcomeon">点此到我的微博</a>
</div>
</div>
</body>
</html>