<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<style>
#table {float:left}
#table tr{height:20px;}
#table tr td{width:20px;}
#yl {float:left;margin-left:10px;}
#yl tr{height:20px;}
#yl tr td{width:20px;}
</style>
</head>
<body οnkeydοwn="zhuan()" style="text-align: center">
<div style="width:350px;margin:0 auto;">
<table border="1" id="table" cellspacing="0">
</table>
<table border="1" id="yl" cellspacing="0">
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</table>
<table border="1" id="f" cellspacing="0" style="float:left;margin-left:10px;">
<tr>
<td> </td>
</tr>
</table>
</div>
<script type="text/javascript" charset="utf-8">
var fangkuai=new Array();
var a=new Array();
var map=new Array();
var tempmap=new Array();
var zhong;
var xzhong;
var xdx=4;
var xdy=0;
var fkg=0;
var fkk=0;
var fenshu=0;
var dingshiqi;
els();
function els()
{
init();
kuai();
zaofangkuai();
kuaitotemp();
xianshi();
xianshiyl();
dingSi();
}
//构造表格
function init()
{
var newtable=getId("table");
var ntbody=document.createElement ("tbody");
newtable.appendChild (ntbody);
for(var i=0;i<15;i++){
var row=document.createElement ("tr");
for(var j=0;j<10;j++){
var cell=document.createElement ("td");
cell.appendChild(document.createTextNode (" "));
row.appendChild (cell);
}
ntbody.appendChild (row);
}
for(h=0;h<newtable.rows.length;h++)
{
map[h]=new Array();
for(l=0;l<10;l++)
{
map[h][l]=0;
}
}
tempmap=newtempmap();
fen();
}
//造方块
function zaofangkuai()
{
zhong=xzhong;
xzhong=Math.floor(Math.random()*a.length);
fangkuai=a[zhong];
xianshiyl();
getfkc(fangkuai);
}
//定义几种方块
function kuai()
{
a[0]=new Array([1,1,1,1],[0,0,0,0],[0,0,0,0],[0,0,0,0]);
a[1]=new Array([1,1,0,0],[1,1,0,0],[0,0,0,0],[0,0,0,0]);
a[2]=new Array([1,1,1,0],[0,1,0,0],[0,0,0,0],[0,0,0,0]);
a[3]=new Array([0,1,1,0],[1,1,0,0],[0,0,0,0],[0,0,0,0]);
a[4]=new Array([1,0,0,0],[1,1,1,0],[0,0,0,0],[0,0,0,0]);
a[5]=new Array([0,0,1,0],[1,1,1,0],[0,0,0,0],[0,0,0,0]);
zhong=Math.floor(Math.random()*a.length);
xzhong=Math.floor(Math.random()*a.length);
}
//键盘事件
function zhuan()
{
document.documentElement.onkeydown = function(e){
e = e||window.event;var ec = e.which||e.keyCode;
switch(ec){
case 38:shang();break;
case 37:zuo();break;
case 39:you();break;
case 40:xia();break;
default:break
}
};
}
//键盘上
function shang()
{
tempkuai=new Array([0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]);
tempkuai=fangkuai;
tempkuai=xuanzhuan(tempkuai);
tempkuai=dingwei(tempkuai);
getfkc(tempkuai);
if(chujie(tempkuai) && chongdie(xdx,xdy,tempkuai))
{
fangkuai=tempkuai;
}
getfkc(fangkuai);
kuaitotemp();
xianshi();
}
function zuo()
{
if(xdx-1>=0 && chongdie(xdx-1,xdy,fangkuai))
{xdx--;}
kuaitotemp();
xianshi();
}
function you()
{
if(xdx+fkk<10 && chongdie(xdx+1,xdy,fangkuai))
{xdx++;}
kuaitotemp();
xianshi();
}
function xia()
{
gaodu=getId("table");
if(xdy+fkg<gaodu.rows.length && chongdie(xdx,xdy+1,fangkuai))
{
xdy++;
}else{
luoshi();
xiaokuai();
xdy=0;
xdx=4;
zaofangkuai();
}
kuaitotemp();
xianshi();
}
//旋转函数
function xuanzhuan(kuais)
{
tempkuai=new Array([0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]);
for(h=0;h<4;h++)
{
for(l=0;l<4;l++)
{
tempkuai[h][l]=kuais[3-l][h];
}
}
return tempkuai;
}
//旋转重定位
function dingwei(kuai)
{
tempkuai=new Array([0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]);
minx=4;
miny=4;
for(h=0;h<4;h++)
{
for(l=0;l<4;l++)
{
if(kuai[h][l]==1)
{
if(h<miny)
{
miny=h;
}
if(l<minx)
{
minx=l;
}
}
}
}
for(h=0;h<4;h++)
{
for(l=0;l<4;l++)
{
if(kuai[h+miny][l+minx]=="")
{
tempkuai[h][l]=0;
}else
{
tempkuai[h][l]=kuai[h+miny][l+minx];
}
}
}
return tempkuai;
}
//将生成的块装入移动数组
function kuaitotemp()
{
tempmap=newtempmap();
for(h=0;h<4;h++)
{
for(l=0;l<4;l++)
{
if(fangkuai[h][l]==1)
{
tempmap[h+xdy][l+xdx]=fangkuai[h][l];
}
}
}
}
//落实
function luoshi()
{
for(h=0;h<4;h++)
{
for(l=0;l<4;l++)
{
if(fangkuai[h][l]==1)
{
map[h+xdy][l+xdx]=fangkuai[h][l];
}
}
}
}
//出界判断
function chujie(s)
{
for(h=0;h<4;h++)
{
for(l=0;l<4;l++)
{
if(s[h][l]==1)
{
if(l+xdx>=10)
{return false;}
}
}
}
return true;
}
//重叠判断
function chongdie(x,y,f)
{
for(h=0;h<4;h++)
{
for(l=0;l<4;l++)
{
if(f[h][l]==1 && map[h+y][l+x]==1)
return false;
}
}
return true;
}
//定时移动
function yidong()
{
gaodu=getId("table");
tempmap=newtempmap();
if(xdy+fkg<gaodu.rows.length && chongdie(xdx,xdy+1,fangkuai))
{
xdy++;
}else{
luoshi();
xiaokuai();
xdy=0;
xdx=4;
zaofangkuai();
}
for(h=0;h<4;h++)
{
for(l=0;l<4;l++)
{
if(fangkuai[h][l]==1)
tempmap[h+xdy][l+xdx]=fangkuai[h][l];
}
}
xianshi();
}
//消除方块
function xiaokuai()
{
gaodu=getId("table");
h=gaodu.rows.length-1;
while(h>=0)
{
zongshu=0;
for(l=0;l<10;l++)
{
if(map[h][l]==1)
{
zongshu++;
}
}
if(zongshu==10)
{
fenshu=fenshu+10;
fen()
for(xh=h;xh>=0;xh--)
{
if(xh==0)
{
for(l=0;l<10;l++)
{
map[xh][l]=0;
}
}else{
for(l=0;l<10;l++)
{
map[xh][l]=map[xh-1][l];
}
}
}
h=gaodu.rows.length-1;
}else{
h--;
}
}
}
//得到方块长度.宽度
function getfkc(f)
{
fkg=0;
fkk=0;
for(h=0;h<4;h++)
{
for(l=0;l<4;l++)
{
if(f[h][l]==1)
{
if(h>=fkg)
{
fkg=h+1;
}
if(l>=fkk)
{
fkk=l+1;
}
}
}
}
}
//刷新移动数组
function newtempmap()
{
newmap=new Array();
gaodu=getId("table");
for(h=0;h<gaodu.rows.length;h++)
{
newmap[h]=new Array()
for(l=0;l<10;l++)
{
newmap[h][l]=0;
}
}
return newmap;
}
//显示预览框方块
function xianshiyl()
{
gaodu=getId("yl");
for(h=0;h<4;h++)
{
for(l=0;l<4;l++)
{
if(a[xzhong][h][l]==1)
{
gaodu.rows[h].cells[l].style.background="#FF0000";
}else
{
gaodu.rows[h].cells[l].style.background="#FFFFFF";
}
}
}
}
//显示游戏
function xianshi()
{
gaodu=getId("table");
for(h=0;h<gaodu.rows.length;h++)
{
for(l=0;l<10;l++)
{
if(map[h][l]==1 || tempmap[h][l]==1)
{
gaodu.rows[h].cells[l].style.background="#FF0000";
}else
{
gaodu.rows[h].cells[l].style.background="#FFFFFF";
}
}
}
}
//判断游戏失败
function shibai()
{
for(h=0;h<4;h++)
{
for(l=0;l<4;l++)
{
if(fangkuai[h][l]==1 && map[h+xdy][l+xdx]==1)
return true;
}
}
return false;
}
//显示分数
function fen()
{
s=getId("f");
s.rows[0].cells[0].innerHTML="得分:"+fenshu;
}
function getId(a)
{
return document.getElementById(a);
}
function dingSi()
{
dingshiqi=setInterval("yidong()",700);
}
</script>
</body>
</html>
js 俄罗斯方块
最新推荐文章于 2021-06-02 19:40:52 发布