Ver 0.2版本已经出来了哦(2014-12-26),最新的代码:
此次修正的东西:
1.左边右边可以移动。
2.可以旋转
3.可以消除满行的方块。
代码:
无标题文档 script> //2维数组,用来存放俄罗斯方块的坐标 var xYAxis=[]; xYAxis.push([1,2],[3,4]); //alert(xYAxis[1][0]); //复制节点 /*$(document).ready(function(e) { for(i=0;i<2;i++) { if(i==1) { // $("#test").append("
"); //加上换行符 } $(".t").clone().appendTo("#test"); } //动态得到test(container)的宽度,包含两边的边框BORDER $("#test").width(($(".t").width()+2)*2+1); }); */ //获得区域的横坐标和纵坐标 function getArea(x,y,unit,id) { this.x=x; this.y=y; this.unit=unit; //每个单元的大小,单位为像素 this.el=document.getElementById(id); //得到ID对象 this.board=[]; //面板,即在区域范围内的元素(俄罗斯方块) /*创建2D范围矩阵*/ for(var y=0;y=0&&xBegin<=this.x&&yBegin>=0&&yBegin<=this.y) { this.board[yBegin][xBegin]=el; //确定元素的位置 } } //消掉所有的行 this.removeFullLines=function() { var lines=0; for(var i=this.y-1;i>0;i--) { if(this.linesRelated(i)) { this.removeLines(i); lines++; y++; } } return lines; //返回线条 } //和线性有关的东西(判断是否满了) this.linesRelated=function(y) { for(var x=0;x 0;y--) { /*今天暂时写到这里*/ /*继续于2014-12-21*/ for(var x=0;x =0;y--) { for(var x=0;x =0;yAxis--) { for(var xAxis=0;xAxis<=operate[yAxis].length;xAxis++) { if(operate[yAxis][xAxis]) { var el=document.createElement("div"); el.className="block"+this.types; //确定这个元素的CLASSNAME //确定左边距和上边距 el.style.left=(this.x+xAxis)*this.area.unit+"px"; el.style.top=(this.y+yAxis)*this.area.unit+"px"; this.area.el.appendChild(el); //这个EL去APPEND主要的EL。 this.board[yAxis][xAxis]=el; this.elements.push(el); //推入elements中 } } /*个人感觉这个功能应该是加速往下掉落的方法?不明觉厉*/ if(lines) { yAxis--; } if(foundLines) { lines++; } } this.running=true; this.fallDownId=setTimeout(this.fallDown,this.speed); //间隔时间,掉落下的 var nextOperate=this.blockComplex[this.nextType]; for(var y=0;y this.area.y){return false;} if(newX<0){return false;} if(newX>=this.area.x){return false;} if(this.area.getBlock(newY,newX)){return false;} //获得区域 } } } return true; } //旋转俄罗斯方块 this.rotate=function() { var puzzle=this.createEmpty(this.board.length,this.board[0].length); //创建一个空的矩阵 for(var y=0;y =this.area.y){this.stopGame=true;return false;} //如果触底,那么就停止游戏 if(this.area.getBlock(this.getY()+y+1,this.getX()+x)){this.stopGame=true;return false;} } } } return true; } //下落 this.moveDown=function() { //用一个循环去控制下落 for(var i=0;i =this.area.x){return false;} if(this.area.getBlock(this.getY()+y,this.getX()+x+1)){return false;} } } } return true; } /*向右移动*/ this.moveRight=function() { for(var i=0;i Game Over
演示图:
项目git地址:
http://git.oschina.net/KMSFan/Tetris_Yang
项目演示地址:http://runjs.cn/detail/ggo07ery
以上所述就是本文的全部内容了,希望能够对大家学习javascript有所帮助。