问题描述
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html><style> div {display:block;background-color:black;position:absolute;font:17px Tahoma;color:#fff;} span {float:left;background-color:gray;width:12px;height:12px;border:4px gray outset;margin:0 1 1 0;overflow:hidden;} p {float:left;width:8px;height:8px;border:2px gray outset;overflow:hidden;margin:0;padding:0} #main {width:375px;height:430px;display:block;background-color:gray;position:relative;} #body {width:241px;height:401px;left:15px;top:15px;border:1px solid #999;} #score {width:80px;height:24px;left:270px;top:15px;padding:4px;} #level {width:80px;height:24px;left:270px;top:50px;padding:4px;} #next {width:50px;height:50px;left:270px;top:85px;border:19px black solid;} #ctrl {width:80px;height:55px;left:270px;top:360px;padding:4px;text-align:center;background-color:gray} #ctrl button{width:80px;height:25px;}</style><script> //http://www.codesky.net var G = { fs:[], fn:[], score:0, l:['#9F0','#FF3','#F33','#C0C','#33C','#0F3','#F93'], v:[0x801,0x801,0x801,0x801,0x801,0x801,0x801,0x801,0x801,0x801,0x801,0x801,0x801,0x801,0x801,0x801,0x801,0x801,0x801,0xFFFF], d:[[0xCC00],[0x4444,0xF0],[0x8C40,0x6C00],[0x4C80,0xC600],[0x44C0,0x8E00,0xC880,0xE200],[0x88C0,0xE800,0xC440,0x2E00],[0x4E00,0x8C80,0xE400,0x4C40]], init:function(){ var body = document.getElementById('body'); var next = document.getElementById('next'); for(var i=0;i<240;i++){ G.fs.push(body.appendChild(document.createElement("span"))); } for(var i=0;i<16;i++){ G.fn.push(next.appendChild(document.createElement("p"))); } G.domScore = document.getElementById('score'); G.domLevel = document.getElementById('level'); document.onkeydown=function(e){G.event(e||window.event,0)}; G.rand(); G.next(); }, timeTesk:function(){ if(G.pause)return; if(!G.move(G.x, G.y+1, G.t)){ var s = 0; for(var i=0;i<19;i++){ G.v[i]=G.v[i]|G.m[i]; if(G.v[i]==0xFFF){ for(var k=i;k>0;k--){ G.v[k] = G.v[k-1]; } G.score+=++s; } } G.next(); return false; } G.draw(); return true; }, move:function(x,y,t){ var m = []; for(var k=0;k<4;k++){//下面的这段代码也看不懂,请详细解答一下。 m[y+k] = (G.d[G.n][t]>>(3-k)*4&0xF)<<Math.max(x,0)>>-Math.min(x,0); if(m[y+k] & G.v[y+k]){ return false; } } G.x = x; G.y = y; G.t = t; G.m = m; G.draw(); return true; }, rand:function(){ G.n = G._n; G.t = G._t; G.c = G._c; G._n = parseInt(Math.random()*G.d.length); G._t = parseInt(Math.random()*G.d[G._n].length); G._c = parseInt(Math.random()*G.l.length); }, next:function(){ G.rand(); if(parseInt(G.score/20)!=G.level){ G.level = parseInt(G.score/20); clearInterval(G.handle); G.handle = setInterval("G.timeTesk()",500/(G.level+1)); } G.domScore.innerHTML = 'Score:'+G.score; G.domLevel.innerHTML = 'Level:'+G.level; var i = 0; while(!(G.d[G.n][G.t]>>i*4&0xF))i++; if(!G.move(3, i-3, G.t)){ alert('Game over!'); clearInterval(G.handle); } }, draw:function(){ for(var i=0;i<240;i++){ if((G.v[parseInt(i/12)]>>(11-i%12))&0x1){ G.fs[i].style.visibility = ''; }else if((G.m[parseInt(i/12)]>>(11-i%12))&0x1){ G.fs[i].style.visibility = ''; G.fs[i].style.borderColor = G.fs[i].style.background = G.l[G.c]; }else{ G.fs[i].style.visibility ='hidden'; } } for(var i=0;i<16;i++){ if(G.d[G._n][G._t]>>(15-i)&0x1){ G.fn[i].style.visibility = ''; G.fn[i].style.borderColor = G.fn[i].style.background = G.l[G._c]; }else{ G.fn[i].style.visibility ='hidden'; } } }, event:function(e,t){ switch(e.keyCode){ case 37: G.move(G.x + 1, G.y, G.t); break; case 39: G.move(G.x - 1, G.y, G.t); break; case 38: G.move(G.x, G.y, (G.t + 1) % G.d[G.n].length); break; case 40: G.timeTesk(); break; case 32: while(G.timeTesk()); } } }</script><body onload='G.init();'> <div id='main'> <div id='body'></div> <div id='score'></div> <div id='level'></div> <div id='next'></div> <div id='ctrl'> <button onclick="javascript:location.href=location.href;">New</button>//这下面的代码看不懂啊//[G.pause=!!!G.pause]和[G.pause=!G.pause]有去区别吗??//有,那有什么区别,请详细讲讲。 <button onclick="javascript:this.innerHTML={'true':'Start','false':'Pause'}[G.pause=!!!G.pause];">Pause</button> </div> </GMain></body></html> 上面我注视的地方是我看不懂的地方,请帮帮我看看,如果知道麻烦给详细讲解一下,谢谢。 问题补充:<div class="quote_title">clue 写道</div><div class="quote_div">我觉得没区别,这里就是点击按钮切换pause值(true/false),并且当切换为true时显示Start,false时显示Pause</div><br />(G.d[G.n][t]>>(3-k)*4&0xF)<<Math.max(x,0)>>-Math.min(x,0)<br />那这个了,这个表达式我也看不懂啊。>>表示的是位移运算,但是这样连在一起G.d[G.n][t]>>(3-k)*4&0xF怎么解释呢?<br />希望能讲解一下,上面的表达式的意义,及执行的顺序。<br />
解决方案
囧,这个是俄罗斯方块啊...运算符优化级从高到低:()*-<< >>&这里G.d中存储的是各种形状,及它们各旋转形态下的点阵。G.v存储的是当前游戏区的点阵(每元素为1行,共20行)move的3个参数代表当前形状的x, y, 以及旋转形态索引 move:function(x,y,t){// m存储当前形状在游戏区的点阵计算结果 var m = []; for(var k=0;k<4;k++){ // 遍历4行(图形以4*4的点阵存储)// G.d[G.n][t]读取形状的4*4点阵,以M表示// M>>((3-k)*4) & 0xF 分别取从高至低的每4节点点阵,即取形状的每一行,以L表示// 最后L<<Math.max(x,0)>>-Math.min(x,0)根据当前形状的x坐标计算映射到游戏区的点阵 m[y+k] = (G.d[G.n][t]>>(3-k)*4&0xF)<<Math.max(x,0)>>-Math.min(x,0);// 与游戏区的点阵进行按位与,看有无重叠,有则代表有碰撞,不能移动 if(m[y+k] & G.v[y+k]){ return false; } } G.x = x; G.y = y; G.t = t; G.m = m; G.draw(); return true; },
解决方案二:
我觉得没区别,这里就是点击按钮切换pause值(true/false),并且当切换为true时显示Start,false时显示Pause