OLSFK.Random = function() { if (OLSFK.Options.nextBlock != 0) { OLSFK.Options.curBlock = OLSFK.Options.nextBlock; var block = OLSFK.Next[OLSFK.Options.nextBlock]; if (block) { for (var i=1; i<=4; i++) { var itm = block[i]; var box = Lib.Getid('cur_'+itm.x+'_'+itm.y); box.style.background = '#ddd'; //OLSFK.Options.GampMap[itm.x+'_'+itm.y] = 0; } } } else { OLSFK.Options.curBlock = Math.floor(Math.random() * 7 + 1); } OLSFK.Options.nextBlock = Math.floor(Math.random() * 7 + 1); OLSFK.drawNext(); }
随机生成下次预下落的方块;并显示到右上角的预览表格里;
OLSFK.play = function(speed) { var block = OLSFK.Items[OLSFK.Options.curBlock]; if (block && OLSFK.Options.Move) { var flag = true; for (var i=1; i<=4; i++) { var x = block[i].x; var y = block[i].y; if (y+1 >= OLSFK.Options.height) { flag = false; break; } if (OLSFK.Options.GampMap[x+'_'+(y+1)] == 1 && !OLSFK.isMe(x,y+1)) { flag = false; break; } } if (flag) { for (var i=1; i<=4; i++) //清除图形 { var itm = block[i]; var box = Lib.Getid('box_'+itm.x+'_'+itm.y); box.style.background = '#ddd'; OLSFK.Options.GampMap[itm.x+'_'+itm.y] = 0; } for (var i=1; i<=5; i++) { var x = block[i].x; var y = block[i].y; OLSFK.Items[OLSFK.Options.curBlock][i] = {x:x,y:(y+1)}; } OLSFK.draw(); var S = OLSFK.Options.Levels[OLSFK.Options.curLevel]; if (speed) { S = 50; } OLSFK.Options.Timer = setTimeout(function() { OLSFK.play(); }, S); } else { OLSFK.ReItems(OLSFK.Options.curBlock); OLSFK.newRun(); } } }
OLSFK.play 正常下落的方法,也得判断下落一格是否有被占用的格,如果没有,清除当前方块,绘制方块新位置;
当方块不能再下落时(flag = false),重置当前方块坐标配置; OLSFK.ReItems(OLSFK.Options.curBlock);
进入 OLSFK.newRun(); 新下落方块下落过程准备;
OLSFK.newRun = function() { clearTimeout(OLSFK.Options.Timer); OLSFK.DelFunc(); if (OLSFK.Options.deline >= 10) { OLSFK.Options.deline = 0; OLSFK.Options.curLevel ++; OLSFK.Element.CurLevel.setHTML("级:"+OLSFK.Options.curLevel); } OLSFK.Element.Score.setHTML("分:"+OLSFK.Options.Score); if (OLSFK.Options.curLevel <= OLSFK.Options.lineNum) { OLSFK.Random(); //判断是否结束 OLSFK.ChkEnd(); } else { OLSFK.Options.Move = false; OLSFK.Options.Start = false; OLSFK.Options.Eventing = false; OLSFK.Options.Deling = false; Lib.Getid('spn').innerHTML = 'Game Is Over! You Win the Game!'; Lib.Getid('dobtn').innerHTML = ' 开始 '; } }
当下落结束时,清除定时器,暂停新方块下落,检测是否有可消除的行;减了多少行;
每减去一行 加分
OLSFK.Options.Score += OLSFK.Options.ScoreNum;
这个方法在 :
OLSFK.DelFunc = function() { OLSFK.Options.Deling = true; OLSFK.Options.Move = false; OLSFK.Options.Eventing = false; var Fn = 0; for (var i=OLSFK.Options.height-1; i>=0; i--) { Fn = 0; for (var j=0; j<OLSFK.Options.width; j++) { if (OLSFK.Options.GampMap[j+'_'+i] == 1) { Fn++; } } if (Fn == OLSFK.Options.width) { OLSFK.Options.deline ++; OLSFK.Options.Score += OLSFK.Options.ScoreNum; OLSFK.DelLine(i); i++; } } OLSFK.Options.Deling = false; OLSFK.Options.Move = true; OLSFK.Options.Eventing = true; }
中执行;
减完一行,就重置该行以上所有行往下降一行;并重置 :
OLSFK.Options.GampMap[x+'_'+y] = OLSFK.Options.GampMap[x+'_'+(y-1)];
该减行为上行的数据;
if (Fn == OLSFK.Options.width) { OLSFK.Options.deline ++; OLSFK.Options.Score += OLSFK.Options.ScoreNum; OLSFK.DelLine(i); i++; }
该判断表示该行上所有格都被占用到;
回到 newRun 上,当判断消行超过几行时,即加级;
if (OLSFK.Options.curLevel <= OLSFK.Options.lineNum) { OLSFK.Random(); //判断是否结束 OLSFK.ChkEnd(); }
如果级数小于配置的总级数,则进入 OLSFK.random();
设置当前下落方块,并随机生成下次下落方块并预览右上角表格上;