纯js实现俄罗斯方块[效率待优化]
来源:互联网 发布:java如何随机生成汉字 编辑:程序博客网 时间:2024/06/05 21:06
var mapObject=null;
var baseShapObject=null;
var timeInterval=null;
$(document).ready(function()
{
mapObject=new MapClass();
mapObject.createMap();
createTable(mapObject);
baseShapObject=new BaseShapClass();
baseShapObject.createShap();
timeInterval=window.setInterval("doingClass()",150);
$(document).bind("keydown",function(event)
{
if(event.keyCode==38) //up
{
baseShapObject.changeShap();
}
else if(event.keyCode==37) //left
{
baseShapObject.moveLeft(mapObject);
}
else if(event.keyCode==39) //right
{
baseShapObject.moveRight(mapObject);
}
else if(event.keyCode==32) //space
{
baseShapObject.moveAtOnceDown(mapObject);
}
});
});
function doingClass()
{
if(baseShapObject.isFail(mapObject))
{
window.clearInterval(timeInterval);
timeInterval=null;
alert("失败了!");
}
if(baseShapObject.isMoveToNextLine(mapObject))
{
baseShapObject.moveDown();
}
else
{
mapObject.addShapToMap(baseShapObject);
mapObject.cleanLine();
baseShapObject.createShap();
}
mapObject.displayShapInMap(baseShapObject);
}
function BaseShapClass()
{
this.top=0;
this.left=3;
this.width=4;
this.height=4;
this.positionArray=new Array();
this.baseOneShap=null;
this.getTop=function ()
{
return this.top;
}
this.getLeft=function ()
{
return this.left;
}
this.getWidth=function ()
{
return this.width;
}
this.getHeight=function ()
{
return this.height;
}
this.createShap=function ()
{
var shapType=Math.floor(Math.random()*5)+1;
switch(shapType)
{
case 1:
this.baseOneShap=new ShapOne();
case 2:
this.baseOneShap=new ShapTwo();
case 3:
this.baseOneShap=new ShapThree();
case 4:
this.baseOneShap=new ShapFour();
case 5:
this.baseOneShap=new ShapFive();
}
this.positionArray=this.baseOneShap.getShap();
this.top=0;
this.left=3;
}
this.changeShap=function ()
{
this.positionArray=this.baseOneShap.getNextPositionShap();
}
this.moveLeft=function (map)
{
if(this.left<=0)
{
var k=0;
for(var i=0;i<this.getHeight();i++)
{
if(this.getPosition(i,0-this.left)==1)
{
break;
}
else
{
k++;
}
}
if(k==this.getHeight())
{
this.left --;
}
}
else
{
this.left --;
}
}
this.moveRight=function (map)
{
if((this.left+this.width)>=map.getWidth())
{
var k=0;
for(var i=0;i<this.getHeight();i++)
{
if(this.getPosition(i,map.getWidth()-this.left-1)==1)
{
break;
}
else
{
k++;
}
}
if(k==this.getHeight())
{
this.left ++;
}
}
else
{
this.left ++;
}
}
this.moveDown=function ()
{
this.top ++;
}
this.moveAtOnceDown=function (map)
{
for(var i=0;i<map.getHeight();i++)
{
if(this.isMoveToNextLine(map))
{
this.top ++;
}
else
{
break;
}
}
}
this.isMoveToNextLine=function (map)
{
if(this.getTop()+1>map.getHeight())
{
return false;
}
for(var i=0;i<this.getWidth();i++)
{
if(this.getPosition(this.getHeight()-1,i)==1 && map.getAPosition(this.getTop(),this.getLeft()+i)==1)
{
return false;
}
}
return true;
}
this.isFail=function (map)
{
for(var i=0;i<map.getWidth();i++)
{
if(map.getAPosition(0,i)==1)
{
return true;
}
}
return false;
}
this.showShap=function ()
{
var returnStr="";
for(var i=0;i<this.height;i++)
{
for(var j=0;j<this.width;j++)
{
returnStr +=this.positionArray[i][j]+",";
}
returnStr +="/n";
}
return returnStr;
}
this.getPosition=function (x,y)
{
return this.positionArray[x][y];
}
}
function MapClass()
{
this.mapArray=new Array();
this.height=30;
this.width=15;
this.score=0;
this.scorePreLine=100;
this.getAPosition=function (x,y)
{
return this.mapArray[x][y];
}
this.createMap=function()
{
for(var i=0;i<this.height;i++)
{
var tempArray=new Array();
for(var j=0;j<this.width;j++)
{
tempArray.push(0);
}
this.mapArray.push(tempArray);
}
}
this.showMap=function ()
{
var returnStr="";
for(var i=0;i<this.height;i++)
{
for(var j=0;j<this.width;j++)
{
returnStr +=this.mapArray[i][j]+",";
}
returnStr +="/n";
}
return returnStr;
}
this.cleanLine=function ()
{
for(var i=this.height-1;i>=0;i--)
{
var fullSum=0;
for(var j=0;j<this.width;j++)
{
if(this.mapArray[i][j]==0)
{
break;
}
else
{
fullSum ++;
}
}
if(fullSum==this.width)
{
for(var m=i;m>=0;m--)
{
if(m==0)
{
for(var n=0;n<this.width;n++)
{
this.mapArray[m][n]=0;
}
}
else
{
for(var n=0;n<this.width;n++)
{
this.mapArray[i][j]=this.mapArray[i-1][j];
}
}
}
this.score +=this.scorePreLine;
}
}
}
this.addShapToMap=function (shap)
{
var shapLeft=shap.getLeft();
var shapTop=shap.getTop();
for(var i=0;i<shap.getHeight();i++)
{
for(var j=0;j<shap.getWidth();j++)
{
if(shap.getPosition(i,j)==1)
{
this.mapArray[shapTop-shap.getHeight()+i][shapLeft+j]=1;
}
}
}
}
this.displayShapInMap=function (shap)
{
var tempArray=new Array();
for(var i=0;i<this.mapArray.length;i++)
{
var innerArray=new Array();
for(var j=0;j<this.mapArray[i].length;j++)
{
innerArray.push(this.mapArray[i][j]);
}
tempArray.push(innerArray);
}
var shapLeft=shap.getLeft();
var shapTop=shap.getTop();
for(var i=0;i<shap.getHeight();i++)
{
for(var j=0;j<shap.getWidth();j++)
{
if(shap.getPosition(i,j)==1)
{
var x=shapTop-shap.getHeight()+i;
var y=shapLeft+j;
if(x>=0 && y>=0)
{
tempArray[x][y]=1;
}
}
}
}
for(var i=0;i<tempArray.length;i++)
{
for(var j=0;j<tempArray[i].length;j++)
{
if(tempArray[i][j]==1)
{
$("#map"+(i*100+j)).attr("src","down.gif");
}
else
{
$("#map"+(i*100+j)).attr("src","up.gif");
}
}
}
$("#score").html(this.score);
tempArray=null;
}
this.getHeight=function ()
{
return this.height;
}
this.getWidth=function ()
{
return this.width;
}
}
function createTable(map)
{
var width=map.getWidth();
var height=map.getHeight();
var writeStr='<table style="border:solid 1px black;">';
writeStr +='<th><td colspan="'+map.getWidth()+'">得分:<span id="score" style="color:red">--</span></td></th>';
for(var i=0;i<height;i++)
{
writeStr +='<tr>';
for(var j=0;j<width;j++)
{
writeStr +='<td>';
writeStr +='<img src="up.gif" id="map'+(i*100+j)+'">';
writeStr +='</td>';
}
writeStr +='</tr>';
}
writeStr +='</table>';
$("body").append($(writeStr));
}
function ShapOne()
{
this.pisitionArrayArray=new Array(
new Array(
new Array(0,0,0,0),
new Array(0,0,0,0),
new Array(0,1,0,0),
new Array(1,1,1,0)
),
new Array(
new Array(0,0,0,0),
new Array(0,1,0,0),
new Array(0,1,1,0),
new Array(0,1,0,0)
),
new Array(
new Array(0,0,0,0),
new Array(0,0,0,0),
new Array(1,1,1,0),
new Array(0,1,0,0)
),
new Array(
new Array(0,0,0,0),
new Array(0,1,0,0),
new Array(1,1,0,0),
new Array(0,1,0,0)
)
);
this.step=0;
this.getShap=function ()
{
this.step=Math.floor(Math.random()*5);
return this.pisitionArrayArray[this.step];
}
this.getNextPositionShap=function ()
{
this.step ++;
if(this.step >=4)
{
this.step=0;
}
return this.pisitionArrayArray[this.step];
}
this.getStep=function ()
{
return this.step;
}
}
function ShapTwo()
{
this.pisitionArrayArray=new Array(
new Array(
new Array(0,0,0,0),
new Array(0,0,0,0),
new Array(0,0,0,0),
new Array(1,1,1,1)
),
new Array(
new Array(0,1,0,0),
new Array(0,1,0,0),
new Array(0,1,0,0),
new Array(0,1,0,0)
),
new Array(
new Array(0,0,0,0),
new Array(0,0,0,0),
new Array(0,0,0,0),
new Array(1,1,1,1)
),
new Array(
new Array(0,1,0,0),
new Array(0,1,0,0),
new Array(0,1,0,0),
new Array(0,1,0,0)
)
);
this.step=0;
this.getShap=function ()
{
this.step=Math.floor(Math.random()*5);
return this.pisitionArrayArray[this.step];
}
this.getNextPositionShap=function ()
{
this.step ++;
if(this.step >=4)
{
this.step=0;
}
return this.pisitionArrayArray[this.step];
}
this.getStep=function ()
{
return this.step;
}
}
function ShapThree()
{
this.pisitionArrayArray=new Array(
new Array(
new Array(0,0,0,0),
new Array(0,0,0,0),
new Array(0,1,1,0),
new Array(0,1,1,0)
),
new Array(
new Array(0,0,0,0),
new Array(0,0,0,0),
new Array(0,1,1,0),
new Array(0,1,1,0)
),
new Array(
new Array(0,0,0,0),
new Array(0,0,0,0),
new Array(0,1,1,0),
new Array(0,1,1,0)
),
new Array(
new Array(0,0,0,0),
new Array(0,0,0,0),
new Array(0,1,1,0),
new Array(0,1,1,0)
)
);
this.step=0;
this.getShap=function ()
{
this.step=Math.floor(Math.random()*5);
return this.pisitionArrayArray[this.step];
}
this.getNextPositionShap=function ()
{
this.step ++;
if(this.step >=4)
{
this.step=0;
}
return this.pisitionArrayArray[this.step];
}
this.getStep=function ()
{
return this.step;
}
}
function ShapFour()
{
this.pisitionArrayArray=new Array(
new Array(
new Array(0,0,0,0),
new Array(0,1,0,0),
new Array(0,1,0,0),
new Array(0,1,1,0)
),
new Array(
new Array(0,0,0,0),
new Array(0,0,0,0),
new Array(0,1,1,1),
new Array(0,1,0,0)
),
new Array(
new Array(0,0,0,0),
new Array(0,1,1,0),
new Array(0,0,1,0),
new Array(0,0,1,0)
),
new Array(
new Array(0,0,0,0),
new Array(0,0,0,0),
new Array(0,0,1,0),
new Array(1,1,1,0)
)
);
this.step=0;
this.getShap=function ()
{
this.step=Math.floor(Math.random()*5);
return this.pisitionArrayArray[this.step];
}
this.getNextPositionShap=function ()
{
this.step ++;
if(this.step >=4)
{
this.step=0;
}
return this.pisitionArrayArray[this.step];
}
this.getStep=function ()
{
return this.step;
}
}
function ShapFive()
{
this.pisitionArrayArray=new Array(
new Array(
new Array(0,0,0,0),
new Array(0,1,0,0),
new Array(0,1,0,0),
new Array(1,1,0,0)
),
new Array(
new Array(0,0,0,0),
new Array(0,0,0,0),
new Array(0,1,0,0),
new Array(0,1,1,1)
),
new Array(
new Array(0,0,0,0),
new Array(0,1,1,0),
new Array(0,1,0,0),
new Array(0,1,0,0)
),
new Array(
new Array(0,0,0,0),
new Array(0,0,0,0),
new Array(1,1,1,0),
new Array(0,0,1,0)
)
);
this.step=0;
this.getShap=function ()
{
this.step=Math.floor(Math.random()*5);
return this.pisitionArrayArray[this.step];
}
this.getNextPositionShap=function ()
{
this.step ++;
if(this.step >=4)
{
this.step=0;
}
return this.pisitionArrayArray[this.step];
}
this.getStep=function ()
{
return this.step;
}
}
- 纯js实现俄罗斯方块[效率待优化]
- 纯js实现俄罗斯方块详解与源码
- 纯JS俄罗斯方块
- 纯js写俄罗斯方块
- HTML5+JS实现俄罗斯方块
- Html5+js实现俄罗斯方块
- js实现俄罗斯方块
- js实现俄罗斯方块
- [前端 3]纯Js制作俄罗斯方块游戏
- console下纯字符实现的俄罗斯方块
- (难度:40%)纯js的俄罗斯方块游戏(含源码)
- 纯js实现五子棋
- 纯js实现倒计时
- 纯js实现轮播图
- 纯js实现分页
- 纯JS实现模态框
- 纯js实现轮播图
- 纯JS实现分页
- glib库简介
- 驱动ALIDEVICE.SYS
- .net 操作page类进行全站title统一
- TreeView CheckBox 部分显示
- 窘~~
- 纯js实现俄罗斯方块[效率待优化]
- 我的第一个WinCE驱动--流式驱动【转】
- 在Struts中创建 Session request repone 对像
- S60 3版程序移植到5版的注意事项
- dsoframer 注册控件后在某些机器调用部分函数时提示"没有注册接口"的解决办法
- Javascript常用脚本归类
- JavaScript页面刷新与弹出窗口问题解决方法
- adr adrl ldr伪指令的使用
- 107个Javascript基础.txt