帮忙分析一下这个程序

timerainbow 2014-06-19 10:21:31
首先声明,这个程序不是我自己写的,是从网上找来的,具体是那个网站已经不记得了。
这是一个俄罗斯方块的程序,用javascript写的,相信很多人可能见过,好了,先下代码:
<!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>
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],////////这个V表示的是什么,本人清楚,有人知道么?
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);/////////[0,length-1]
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>
<buttononclick="javascript:this.innerHTML={'true':'Start','false':'Pause'}[G.pause=!!!G.pause];">Pause</button>
</div>
</GMain>
</body>
</html>
...全文
386 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
清晨v 2014-06-27
  • 打赏
  • 举报
回复
timerainbow 2014-06-20
  • 打赏
  • 举报
回复
引用 10 楼 jslang 的回复:
[quote=引用 9 楼 xuzuning 的回复:] 才看到还有这句 m[y+k] = (G.d[G.n][t]>>(3-k)*4&0xF)<<Math.max(x,0)>>-Math.min(x,0);///////////麻烦重点分析下这条语句 >> 右移,右移一次就是除以 2 << 左移,左移一次就是乘以 2 & 位与, & 0x0f 就是只取二进制的低 4 位 比如 56 的二进制表示是 00111000 运算后就是 00001000 即十进制 8 了
不要想的太复杂了 在这个程序中根本不考虑数据的二进制或十进制值是多少,只是单纯的在对0和1进行移动和裁剪,就好像在操作字符串 [/quote] 好吧,你们说的我都懂,只是这个程序理解起来很吃力,下载时一点注释都没有,有哪位可以将这个程序的设计思想详细描述一下
yaojunyi3721 2014-06-20
  • 打赏
  • 举报
回复
没积分法问题了 楼主赏点分吧 谢谢
xuzuning 2014-06-19
  • 打赏
  • 举报
回复

就是左边 12x20 的区域
0x801 化为二进制是 1000000001
凡是有 1 的地方就是墙
timerainbow 2014-06-19
  • 打赏
  • 举报
回复
引用 3 楼 xuzuning 的回复:
这个V表示的是什么? 表示的是场地
大概是了解,但是具体是什么,边界么,是的话是如何表示出来的……说出来大家学习学习
xuzuning 2014-06-19
  • 打赏
  • 举报
回复
这个V表示的是什么? 表示的是场地
timerainbow 2014-06-19
  • 打赏
  • 举报
回复
不是说好的吗 2014-06-19
  • 打赏
  • 举报
回复
天际的海浪 2014-06-19
  • 打赏
  • 举报
回复
引用 9 楼 xuzuning 的回复:
才看到还有这句 m[y+k] = (G.d[G.n][t]>>(3-k)*4&0xF)<<Math.max(x,0)>>-Math.min(x,0);///////////麻烦重点分析下这条语句 >> 右移,右移一次就是除以 2 << 左移,左移一次就是乘以 2 & 位与, & 0x0f 就是只取二进制的低 4 位 比如 56 的二进制表示是 00111000 运算后就是 00001000 即十进制 8 了
不要想的太复杂了 在这个程序中根本不考虑数据的二进制或十进制值是多少,只是单纯的在对0和1进行移动和裁剪,就好像在操作字符串
xuzuning 2014-06-19
  • 打赏
  • 举报
回复
才看到还有这句 m[y+k] = (G.d[G.n][t]>>(3-k)*4&0xF)<<Math.max(x,0)>>-Math.min(x,0);///////////麻烦重点分析下这条语句 >> 右移,右移一次就是除以 2 << 左移,左移一次就是乘以 2 & 位与, & 0x0f 就是只取二进制的低 4 位 比如 56 的二进制表示是 00111000 运算后就是 00001000 即十进制 8 了
timerainbow 2014-06-19
  • 打赏
  • 举报
回复
没人会么?
timerainbow 2014-06-19
  • 打赏
  • 举报
回复

87,922

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧