forked from renfufei/renfufei.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2048.html
More file actions
472 lines (455 loc) · 16.6 KB
/
2048.html
File metadata and controls
472 lines (455 loc) · 16.6 KB
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
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>2048--华丽版</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
<meta name="author" content="wwl" />
<meta name="Robots" CONTENT="all"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="keywords"content="http://gongjingyu.com/game/2048.htm">
<meta name="description" content="http://gongjingyu.com/game/2048.htm">
<meta name="baidu-site-verification" content="9brM9oobkX" />
<meta name="baidu-site-verification" content="1aeb88e35e67fc1580ed6c9dbf5709b7"/>
<link rel="shortcut icon" href="/favicon.ico"/>
<link rel="bookmark" href="/favicon.ico"/>
<script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<script type="text/javascript">
!window.jQuery && document.write('<script src="/js/jquery.1.9.0.js"><\/script>');
</script>
<script src="javascripts/util.js"></script>
<link rel="stylesheet" type="text/css" href="stylesheets/main.css">
<link rel="stylesheet" type="text/css" href="stylesheets/2048.css">
<script>
$(function(){
setTimeout(function(){
window._bd_share_config = {
slide : [{
bdImg : 8,
bdPos : "right",
bdTop : 100
}]
}
//以下为js加载部分
with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?cdnversion='+~(-new Date()/36e5)];
},100);
});
</script>
</head>
<body class="body">
<div class="content">
<script>
function bodyAuto(){
var width = window.innerWidth;
var height = window.innerHeight;
$('.body').css({width:width+"px",height:height+"px"});
if(height>500){
var h=(height-500)/2-15;
$('.chessboard').css('top',+h<0?0:h+"px");
}else{
$('.chessboard').css('top','0px');
}
$('.block').css("height",$('.block').css("width"));
$('.temp_move').css({"height":$('.temp_move').css("width"),"line-height":$('.temp_move').css("width")});
//$('.chessboard').css('height',$('.game_area').height()+$('.info').height()+40+"px")
$('.game_area').css('height',(parseInt($('.block').css("width"))+2)*4+10+"px");
$('.shade').css({width:$('.chessboard').width(),height:$('.chessboard').height()});
}
(function(){
var n={};
n.chessboardClass="chessboard";//棋盘 class
n.gameAreaClass="game_area";//游戏区域 class
n.startClass="start";
n.line=4;//行数
n.column=4;//列数
n.objs=[];//每个块对象
n.lineArr=[];//每个块对象
n.columnArr=[];//每个块对象
n.speed=200;
n.moveCount=0;//需要移动的个数
n.moveNum=0;//已经移动的个数
n.stop=false;
n.over=false;
n.scroe=0;
//初始化
n.init=function(){
n.scroe=0;
n.initChessboard();
n.initGameArea();
//加载事件
n.events();
//开始游戏
n.startGame();
};
//初始化棋盘
n.initChessboard=function(){
$('.'+n.chessboardClass).remove();
$('body').append('<div class="'+n.chessboardClass+'"><div class="info"><div class="scroe" data-scroe="0">0</div><div class="btns"><div class="anew" title="重玩"></div><div class="stop" title="暂停"></div><div class="message" title="优化与建议"></div><div class="help" title="帮助"></div><div class="clear"></div></div></div>');
bodyAuto();
};
//初始化游戏区域
n.initGameArea=function(){
var tempTag='';
for(var i=0;i<n.line;i++){
for(var j=0;j<n.column;j++){
tempTag+='<div class="block b'+(i*n.line+j)+' l'+i+' c'+j+'" data-index="'+(i*n.line+j)+'" data-line="'+i+'" data-column="'+j+'" data-num="'+0+'"></div>';
}
}
tempTag='<div class="auto"><div class="'+n.gameAreaClass+'">'+tempTag+'</div><div class="back"></div><div class="shade alpha80"><div class="continuation"></div><div class="gameover"><div class="overinfo">游戏结束</div><div class="startinfo">重新开始</div></div><div class="msg"></div></div></div>';
$('.'+n.chessboardClass).append(tempTag);
};
n.getEmptyArea=function(){
var tempArray=[];
for(var i=0;i<n.objs.length;i++){
if($(n.objs)[i].obj.data('num')==0){
tempArray[tempArray.length]=n.objs[i].obj;
}
}
return tempArray;
}
//随机放置数字(每次只生成一个随机数字)
n.setNum=function(){
n.updataObjsByBlock();
//获取未放置数字区域
var tempArray=[];
tempArray=n.getEmptyArea();
//随机坐标
var rnum=Math.ceil(Math.random()*tempArray.length)-1;
while(rnum==-1){
rnum=Math.ceil(Math.random()*tempArray.length)-1;
}
//随机数字2||4(出现2的概率是4的4倍)
num=Math.ceil(Math.random()*10)-1;
if(num>7){
num=4;
}else{
num=2;
}
//num=Math.ceil(Math.random()*100)-1;
//放置
$(tempArray[rnum]).data('num',num).html('<div class="temp_move">'+num+'</div>');
//主动监测游戏是否结束
tempArray=n.getEmptyArea();
//随机坐标
if(tempArray.length==0&&n.isOver()){
//游戏结束
n.checkGameOver();
return -1;//gameovaer
}
bodyAuto();
return 1;
};
//开始游戏入口
n.startGame=function(){
//开始游戏的时候先生成两个随机数
n.setNum();
n.setNum();
};
//根据用户输入方向进行数字合并
n.compute=function(e){
//分为四个方向合并
//合并完成后再生成一个数字
n.setNum();
//判断是否有空白区域(用于检测是否游戏结束)
n.checkGameOver();
};
//gameOver
n.checkGameOver=function(){
//如果没有空白区域,提示用户游戏结束,显示分数,阻止用户操作,时间停止
n.over=true;
$('.continuation').hide();
$('.gameover').show();
$('.shade').fadeIn();
return;
};
//事件集合
n.events=function(){
$(document).on('click','.'+n.startClass,function(){
//开始游戏点击事件
n.startGame();
}).on('keydown','body',function(event){
if(n.moveCount!=n.moveNum||$(".block:animated").size()>0||n.stop)return;
//根据画面对象更新
n.updataObjsByBlock();
//左右上下 37,38,39,40
var event = event || window.event;
var code=event.keyCode;
if(code==37){
//左
//先循环行
//对数组进行整理排序
for(var i=0;i<4;i++){
var lb=$('.l'+i);
n.move2(lb,0);
}
}else if(code==38){
//上
//对数组进行整理排序
for(var i=0;i<4;i++){
var lb=$('.c'+i);
n.move2(lb,1);
}
}else if(code==39){
//右
//对数组进行整理排序
for(var i=0;i<4;i++){
var lb=$('.l'+i);
var tib=[];
for(j=lb.length-1;j>=0;j--){
tib[tib.length]=lb[j];
}
n.move2(tib,3);
}
}else if(code==40){
//下
//对数组进行整理排序
for(var i=0;i<4;i++){
var lb=$('.c'+i);
var tib=[];
for(j=lb.length-1;j>=0;j--){
tib[tib.length]=lb[j];
}
n.move2(tib,4);
}
}
});
};
//方案2的 移动
n.move2=function(lb,type){
/* 1.根据入参对象整理对象数组整理对象数组
2.整理新的对象数组
3.判断type开始移动
*/
n.over=true;
lb=$(lb);
var to=[],nto=[],tcount=0;
var tempf=-1;
for(var i=0;i<lb.length;i++){
//行,列,值
tcount+=parseInt(lb.eq(i).data('num'));
to[i]={line:lb.eq(i).data('line'),coloumn:lb.eq(i).data('column'),num:lb.eq(i).data('num'),tcount:parseInt(tcount)};
if(lb.eq(i).data('num')==0 && tempf==-1){
tempf=0;
}else if(lb.eq(i).data('num')!=0){
if(tempf==0){
tempf=1;
}
}
}
//排序
if(to[to.length-1].tcount>0){
//向左移动,合并
for(var i=1;i<to.length;i++){
var index=-1;
for(var j=i;j>0;j--){
if(to[j].num!=0&&to[j-1].num==0){
nto[j-1]=[n.deepCopy(to[j])];
to[j-1].num=to[j].num;
to[j].num=0;
index=j-1;
}else if(to[j].num==to[j-1].num&&to[j-1].num!=0&&!to[j].flag&&!to[j-1].flag){
nto[j-1]=[to[j-1],to[j]];
to[j-1].num=to[j].num*2;;
to[j].num=0;
to[j-1].flag=true;
index=j-1;
}
}
to[i].np=index;
}
//移动
for(var i=1;i<to.length;i++){
if(to[i].np>=0){
n.moveCount++;
(function(ii){
var distance={};
var y=$('.block').width();
if(type==0){
distance={left:(to[ii].coloumn-to[ii].np)*-y+"px"};
}else if(type==1){
distance={top:(to[ii].line-to[ii].np)*-y+"px"};
}else if(type==3){
distance={left:(4-to[ii].np-to[ii].coloumn-1)*y+"px"};
}else if(type==4){
distance={top:(4-to[ii].np-to[ii].line-1)*y+"px"};
}
lb.eq(ii).find('.temp_move').animate(distance,n.speed,function(){
n.moveNum++;
var t=lb.eq(to[ii].np);
if(t.data('num')==0){
lb.eq(to[ii].np).data('num',$(this).html());
lb.eq(to[ii].np).html('<div class="temp_move num'+$(this).html().length+'">'+$(this).html()+'</div>');
}else{
lb.eq(to[ii].np).data('num',$(this).html()*2);
$('.scroe').html(parseInt($('.scroe').data('scroe'))+$(this).html()*2);
$('.scroe').data('scroe',$('.scroe').html())
var tnum=$(this).html()*2;
lb.eq(to[ii].np).html('<div class="temp_move num'+tnum.toString().length+'">'+tnum+'</div>');
}
$(this).parent('div').data('num',0);
$(this).remove();
n.moveBack();
});
})(i);
}
}
}
};
//移动完成的回调
n.moveBack=function(){
//判断是否全部移动完
if(n.moveCount==n.moveNum){
n.moveCount=n.moveNum=0
n.setNum();
}
}
//根据排序好的 arr 更新到objs,再根据objsarray的数据set到画面上
n.updataObjs=function(arr){
for(var i=0;i<arr.length;i++){
var a=arr[i];
if(!a){
break;
}
for(var j=0;j<a.length;j++){
n.objs[a[j].obj.data('index')]=a[j];
}
}
}
//根据画面对象更新 class block
n.updataObjsByBlock=function(){
//更新n.objs array【object】
var tb=$('.block');
for(var i=0;i<n.column;i++){
n.columnArr[i]=[];
}
for(var i=0;i<n.line;i++){
n.lineArr[i]=[];
}
for(var i=0;i<tb.length;i++){
n.objs[i]=n.lineArr[tb.eq(i).data('line')][tb.eq(i).data('column')]=n.columnArr[tb.eq(i).data('column')][tb.eq(i).data('line')]={num:tb.eq(i).data('num'),obj:tb.eq(i)};
}
};
//对象复制
n.deepCopy= function(source) {
var result={};
for (var key in source) {
if(key!="no")
result[key] = typeof source[key]==='object'?deepCoyp(source[key]):source[key];
}
return result;
}
//监测游戏是否结束
n.isOver=function(){
for(var j=0;j<4;j++){
var tl=$('.l'+j);
var tc=$('.c'+j);
for(var i=0;i<4-1;i++){
if(tl.eq(i).data('num')==0||tl.eq(i+1).data('num')==0||tl.eq(i).data('num')==tl.eq(i+1).data('num')||tc.eq(i).data('num')==tc.eq(i+1).data('num')){
return false;
}
}
}
return true;
};
$(document).on('click','.anew',function(){
//重玩
$('.gameover').hide();
n.init();
}).on('click','.stop',function(){
//暂停
$('.gameover').hide();
$('.continuation').show();
$('.shade').fadeIn();
n.stop=true;
}).on('click','.message',function(){
//优化与建议
n.stop=true;
$('.shade').fadeIn();
$('.msg').show().html('<p>有您的参与我相信网站会越来越好!</p><p>QQ:644615565</p><p class="jx">继续</p>');
}).on('click','.help',function(){
//帮助
}).on('click','.continuation',function(){
//暂停后的开始
$('.shade').fadeOut();
$('.continuation').hide();
n.stop=false;
}).on('click','.startinfo',function(){
n.over=false;
n.init();
}).on('click','.jx',function(){
$('.shade').fadeOut();
$('.msg').hide();
n.stop=false;
});
n.init();
})();
</script>
<img src="" class="b background"/>
<img src="" class="b background1"/>
<script>
//全屏背景自适应 start
$(function(){
backImgAuto($('.background'));
backImgAuto($('.background1'));
bodyAuto();
});
//全屏背景自适应 end
var gi=-1;
var imgs=[ 'http://7ximtt.com1.z0.glb.clouddn.com/background/2048/1.jpg',
'http://7ximtt.com1.z0.glb.clouddn.com/background/2048/2.jpg',
'http://7ximtt.com1.z0.glb.clouddn.com/background/2048/3.jpg',
'http://7ximtt.com1.z0.glb.clouddn.com/background/2048/4.jpg',
'http://7ximtt.com1.z0.glb.clouddn.com/background/2048/5.jpg',
'http://7ximtt.com1.z0.glb.clouddn.com/background/2048/6.jpg',
'http://7ximtt.com1.z0.glb.clouddn.com/background/2048/7.jpg',
'http://7ximtt.com1.z0.glb.clouddn.com/background/2048/8.jpg',
'http://7ximtt.com1.z0.glb.clouddn.com/background/2048/9.jpg',
'http://7ximtt.com1.z0.glb.clouddn.com/background/2048/10.jpg' ]
setTimeout(function(){loadImg(0);},1);
var imgLoad=[];
function loadImg(index){
var img=new Image();
img.src=imgs[index];
img.onload = function() {
callback(index);
};
img.onerror = function() {
var i=index+1;
if(i>=imgs.length){
i=0;
}
loadImg(i);
};
}
var ImgS=[];
function callback(index){
$('.background').attr('src',imgs[index]).fadeIn(1000);
gi=index;
for(var i=0;i<imgs.length;i++){
imgLoad[i]={};
imgLoad[i].img = new Image();
imgLoad[i].img.src = imgs[i];
imgLoad[i].img.onload = function(img){
ImgS[ImgS.length]=$(this).attr('src');
};
}
setInterval(function(){
for(var i=0;i<ImgS.length&&ImgS.length>1;i++){
index++;
if(index>=ImgS.length)index=0;
$('.b:visible').fadeOut();
$('.b:hidden').attr('src',ImgS[index]).fadeIn(1000);
break;
}
},5000);
}
window.onresize = function() {
backImgAuto($('.background'));
backImgAuto($('.background1'));
bodyAuto();
};
</script>
</div>
<div class="full-overlay"></div>
</body>
</html>