栏目搜索
 
 
 
 
你的位置:首页 > JavaScript > js写的俄罗斯方块 >
 

js写的俄罗斯方块

发布者:[本站编辑] | 来源:[]

js写的俄罗斯方块_电脑维修资料库

<html>
<head>
<title>俄罗斯方块</title>
<style>
<!--
body
{
}
.mb
{
    background-color: firebrick;
    cursor: default;
    height: 22px;
    width: 22px
}
.sb
{
    background-color: slategray;
    cursor: default;
    height: 22px;
    width: 22px
}
.bk
{
    background-color: white;
    cursor: default;
    height: 22px;
    width: 22px
}
.gt
{
    border-bottom: deepskyblue thin solid;
    border-left: deepskyblue thin solid;
    border-right: deepskyblue thin solid;
    border-top: deepskyblue thin solid;
    cursor: default
}
-->
</style>
<script>
<!--
var bx=new array(4);
var by=new array(4);
var px=new array(4);
var py=new array(4);
var mtimer
var firstview

function begingame()
{
    gamestate=0;
    speed=1;
    outtime=1100-speed*100;
    score=0;
    if(gamestate!=0)return;
    firstview=true;
    for(j=0;j<16;j++)
        for(i=0;i<10;i++)
            setclass(i,j,bk);
    randbar();
    gamestate=1;
    play.disabled=true;
    window.clearinterval(mtimer);
    mtimer=window.setinterval(movebar(),outtime);
}

function keycontrol()
{
    if(gamestate!=1)return;
    switch(event.keycode){
        case 37:{    //left
            for(i=0;i<4;i++)if(bx==0)return;
            for(i=0;i<4;i++)if(getclass(bx-1,by)==sb)return;
            for(i=0;i<4;i++)setclass(bx,by,bk);
            for(i=0;i<4;i++)bx=bx-1;
            for(i=0;i<4;i++)setclass(bx,by,mb);
            break;}
        case 38:{    //up
            var prembarx=new array(4);
            var prembary=new array(4);
            var cx=math.round((bx<0>+bx<1>+bx<2>+bx<3>)/4);
            var cy=math.round((by<0>+by<1>+by<2>+by<3>)/4);
            for(i=0;i<4;i++){
                prembarx=math.round(cx-cy+by);
                prembary=math.round(cx+cy-bx);
                if(prembarx<0 ¦¦ prembarx>9 ¦¦ prembary<0 ¦¦ prembary>15)return;
                if(getclass(prembarx,prembary)==sb)return;
            }
            for(i=0;i<4;i++)setclass(bx,by,bk);
            for(i=0;i<4;i++){
                bx=prembarx;
                by=prembary;
            }
            for(i=0;i<4;i++)setclass(bx,by,mb);
            break;}
        case 39:{    //right
            for(i=0;i<4;i++)if(bx==9)return;
            for(i=0;i<4;i++)if(getclass(bx+1,by)==sb)return;
            for(i=0;i<4;i++)setclass(bx,by,bk);
            for(i=0;i<4;i++)bx=bx+1;
            for(i=0;i<4;i++)setclass(bx,by,mb);
            break;}
        case 40:{    //down
            movebar();
            break;}
    }
}

function delline()
{
    for(i=0;i<4;i++)setclass(bx,by,sb);
    for(j=0;j<16;j++){
        dline=true;
        for(i=0;i<9;i++){
            if(getclass(i,j)!=sb){
                dline=false;
                break;
            }
        }
        if(dline){
            score=score+100;
            for(k=j;k>0;k--)
                for(l=0;l<10;l++)
                    setclass(l,k,getclass(l,k-1));
            for(l=0;l<10;l++)setclass(l,0,bk);
        }
    }
    randbar();
    speed=math.floor(score/3000)+1;
    outtime=1100-speed*100;
    scorebar.innerhtml=score : + score;
    speedbar.innerhtml=speed : + speed;
    window.clearinterval(mtimer);
    mtimer=window.setinterval(movebar(),outtime);
}

function getclass(x,y){return gamebar.children.children.classname;}
function setclass(x,y,cname){gamebar.children.children.classname=cname;}

function movebar()
{
    if(gamestate!=1)return;
    dropline=true;
    for(i=0;i<4;i++)if(by==15)dropline=false;
    if(dropline)for(i=0;i<4;i++)if(getclass(bx,by+1)==sb)dropline=false;
    if(!dropline){
        window.clearinterval(mtimer);
        delline();
        return;
    }
    for(i=0;i<4;i++)setclass(bx,by,bk);
    for(i=0;i<4;i++)by=by+1;
    for(i=0;i<4;i++)setclass(bx,by,mb);
}

function pausegame()
{
    if(gamestate==0)return;
    if(event.srcelement.value==pause){
        gamestate=2;
        event.srcelement.value=continue;
        window.clearinterval(mtimer);
    }
    else{
        gamestate=1;
        event.srcelement.value=pause;
        mtimer=window.setinterval(movebar(),outtime);
    }
}

function fmnu(){return false;}
document.oncontextmenu=fmnu;

function preview()
{
    if(previewbar.style.display!=none)
        previewbar.style.display=none;
    else
        previewbar.style.display=block;
}

function replaygame()
{
    if(gamestate!=1)return;
    if(!confirm(你真的要重新开始么?))return;
    gamestate=0;
    window.clearinterval(mtimer);
    begingame();
}
function randbar()
{
    randnum=math.floor(math.random()*20)+1;
    if(!firstview)
        for(i=0;i<4;i++){
            bx=px;
            by=py;
        }
    switch(randnum){
        case 1:{
            px<0>=4;
            py<0>=0;
            px<1>=4;
            py<1>=1;
            px<2>=5;
            py<2>=1;
            px<3>=6;
            py<3>=1;
            break;}
        case 2:{
            px<0>=4;
            py<0>=0;
            px<1>=5;
            py<1>=0;
            px<2>=4;
            py<2>=1;
            px<3>=4;
            py<3>=2;
            break;}
        case 3:{
            px<0>=4;
            py<0>=0;
            px<1>=5;
            py<1>=0;
            px<2>=6;
            py<2>=0;
            px<3>=6;
            py<3>=1;
            break;}
        case 4:{
            px<0>=5;
            py<0>=0;
            px<1>=5;
            py<1>=1;
            px<2>=5;
            py<2>=2;
            px<3>=4;
            py<3>=2;
            break;}
        case 5:{
            px<0>=6;
            py<0>=0;
            px<1>=6;
            py<1>=1;
            px<2>=4;
            py<2>=1;
            px<3>=5;
            py<3>=1;
            break;}
        case 6:{
            px<0>=4;
            py<0>=0;
            px<1>=4;
            py<1>=1;
            px<2>=4;
            py<2>=2;
            px<3>=5;
            py<3>=2;
            break;}
        case 7:{
            px<0>=4;
            py<0>=0;
            px<1>=4;
            py<1>=1;
            px<2>=5;
            py<2>=0;
            px<3>=6;
            py<3>=0;
            break;}
        case 8:{
            px<0>=4;
            py<0>=0;
            px<1>=5;
            py<1>=0;
            px<2>=5;
            py<2>=1;
            px<3>=5;
            py<3>=2;
            break;}
        case 9:{
            px<0>=4;
            py<0>=0;
            px<1>=5;
            py<1>=0;
            px<2>=5;
            py<2>=1;
            px<3>=6;
            py<3>=1;
            break;}
        case 10:{
            px<0>=5;
            py<0>=0;
            px<1>=5;
            py<1>=1;
            px<2>=4;
            py<2>=1;
            px<3>=4;
            py<3>=2;
            break;}
        case 11:{
            px<0>=4;
            py<0>=1;
            px<1>=5;
            py<1>=1;
            px<2>=5;
            py<2>=0;
            px<3>=6;
            py<3>=0;
            break;}
        case 12:{
            px<0>=4;
            py<0>=0;
            px<1>=4;
            py<1>=1;
            px<2>=5;
            py<2>=1;
            px<3>=5;
            py<3>=2;
            break;}
        case 13:{
            px<0>=4;
            py<0>=0;
            px<1>=5;
            py<1>=0;
            px<2>=6;
            py<2>=0;
            px<3>=5;
            py<3>=1;
            break;}
        case 14:{
            px<0>=4;
            py<0>=0;
            px<1>=4;
            py<1>=1;
            px<2>=4;
            py<2>=2;
            px<3>=5;
            py<3>=1;
            break;}
        case 15:{
            px<0>=5;
            py<0>=0;
            px<1>=5;
            py<1>=1;
            px<2>=4;
            py<2>=1;
            px<3>=6;
            py<3>=1;
            break;}
        case 16:{
            px<0>=5;
            py<0>=0;
            px<1>=5;
            py<1>=1;
            px<2>=5;
            py<2>=2;
            px<3>=4;
            py<3>=1;
            break;}
        case 17:{
            px<0>=4;
            py<0>=0;
            px<1>=5;
            py<1>=0;
            px<2>=4;
            py<2>=1;
            px<3>=5;
            py<3>=1;
            break;}
        case 18:{
            px<0>=4;
            py<0>=0;
            px<1>=5;
            py<1>=0;
            px<2>=4;
            py<2>=1;
            px<3>=5;
            py<3>=1;
            break;}
        case 19:{
            px<0>=3;
            py<0>=0;
            px<1>=4;
            py<1>=0;
            px<2>=5;
            py<2>=0;
            px<3>=6;
            py<3>=0;
            break;}
        case 20:{
            px<0>=5;
            py<0>=0;
            px<1>=5;
            py<1>=1;
            px<2>=5;
            py<2>=2;
            px<3>=5;
            py<3>=3;
            break;}
    }
    if(firstview){
        firstview=false;
        randbar();
        return;
    }
    for(i=0;i<4;i++){
        for(j=0;j<4;j++){
            previewbar.children.children.classname=bk;
        }
    }
    for(i=0;i<4;i++)previewbar.children>.children-3>.classname=mb;
    for(i=0;i<4;i++){
        if(getclass(bx,by)!=bk){
            alert(game over!);
            window.clearinterval(mtimer);
            play.disabled=false;
            gamestate=0;
            return;
        }
    }
    for(i=0;i<4;i++)setclass(bx,by,mb);
}
// -->
</script>
</head>
<body bgcolor=black onkeydown=return keycontrol();>
<center>
<p><b><font color=red>俄罗斯方块</font></b></p>
<table cellspacing=0 cellpadding=0 class=gt border=1 bordercolor=lightyellow style=position:absolute;left:220px;top:53px;>
<tbody id=gamebar>
<tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr>
</tbody>
</table>
</center>
<table style=position:absolute;top=100px;left:50px>
    <tr><td id=scorebar style=color:white>score : 0</td></tr>
    <tr><td id=speedbar style=color:white>speed : 1</td></tr>
</table>

<table cellspacing=0 cellpadding=0 class=gt border=1 bordercolor=lightyellow style=position:absolute;left:570px;top:53px;>
<tbody id=previewbar>
<tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr><tr><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td><td class=bk>&nbsp;</td></tr>
</tbody>
</table>
<table style=position:absolute;left:570px;top:180px;>
    <tr><td><input type=button id=play style=width:100px value=play onclick=return begingame();></td></tr>
    <tr><td><input type=button id=pause style=width:100px value=pause onclick=return pausegame();></td></tr>
    <tr><td><input type=button id=preview style=width:100px value=preview onclick=preview();></td></tr>
    <tr><td><input type=button id=replay style=width:100px value=replay onclick=replaygame();></td></tr>
    <tr><td><input type=button id=quit style=width:100px value=quit onclick=window.close();></td></tr>
</table>
<div style=position:absolute;left:590px;top:390px;color:white;font-size:9pt>如有问题请与我<a href=mailto:sor@263.net><font color=white>联系</font></a></div>
</body>
</html>
<script>
function unsel()
{
    document.execcommand(unselect);
    window.settimeout(unsel(),10);
}
unsel();

window.onunload=rel;
function rel()
{
    location.reload();
    return false;
}

</script> </t

 
返回列表 返回JavaScript