Cheer 发表于 2007-4-28 11:07:35

游戏类俄罗斯方块

把下列代码加到网页的<body></body>标签之间 从google下载我的照片管理软件
<!--网页特效代码由驿站http://js.jojoo.net提供!-->

<html>
<head>
<meta name="title" content="jscript simple tetris">

<title>simple tetris</title>
<style>
body {margin:0;background:black;}
td {height:20;width:20;}
#block_div {position:absolute;z-index:1;width:80;}
#table_div {position:absolute;z-index:0;width:320;}
#nblock_div {position:absolute;z-index:2;font:48 system;color:red;}
#title_div {position:absolute;left:326;top:7;font-size:22px;color:white}
#infobar_div {position:absolute;left:396;top:416;}
#infobar2_div {position:absolute;left:396;top:480;font-size:14px;color:white}
#infobar2_div a{color:99ccff;font:system;text-decoration:none}
</style>

<script id=clienteventhandlersjs language=javascript>
<!--


if (document.all){
var n_width = 800;
var n_height = 600;
var n_left = math.round( screen.width/2 ) - n_width/2;
var n_top = math.round( screen.height/2 ) - n_height/2;
var n_incstep = 20;
var curblcok,nextblock;
var arr_curblock = new array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
var curx,cury;
var speed=1;
var maxspeed=9;
var clr_per_line=18;
var pause=0;
var gameover=0;
var colors = new array("#999999","#0000ff","#80ffff","#80ff80","#ffff00","#ff8000","#ff00ff","#ff0000"
);
var cid;
var ncid;
var blocks = new array("tt_o1","tt_t2","tt_z1","tt_s1","tt_l1","tt_j1","tt_i2");
var bid;
var killedlines=0;

var tt_o1 = new array(0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0);
var tt_o2 = new array(0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0);
var tt_o3 = new array(0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0);
var tt_o4 = new array(0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0);

var tt_t1 = new array(0,0,0,0,0,1,0,0,1,1,0,0,0,1,0,0);
var tt_t2 = new array(0,0,0,0,0,0,0,0,1,1,1,0,0,1,0,0);
var tt_t3 = new array(0,0,0,0,0,1,0,0,0,1,1,0,0,1,0,0);
var tt_t4 = new array(0,0,0,0,0,1,0,0,1,1,1,0,0,0,0,0);

var tt_z1 = new array(0,0,0,0,1,1,0,0,0,1,1,0,0,0,0,0);
var tt_z2 = new array(0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0);
var tt_z3 = new array(0,0,0,0,0,1,1,0,0,0,1,1,0,0,0,0);
var tt_z4 = new array(0,0,1,0,0,1,1,0,0,1,0,0,0,0,0,0);

var tt_s1 = new array(0,0,0,0,0,1,1,0,1,1,0,0,0,0,0,0);
var tt_s2 = new array(0,0,0,0,0,1,0,0,0,1,1,0,0,0,1,0);
var tt_s3 = new array(0,0,0,0,0,0,1,1,0,1,1,0,0,0,0,0);
var tt_s4 = new array(0,1,0,0,0,1,1,0,0,0,1,0,0,0,0,0);

var tt_l1 = new array(0,1,0,0,0,1,0,0,0,1,1,0,0,0,0,0);
var tt_l2 = new array(0,0,0,0,0,0,1,0,1,1,1,0,0,0,0,0);
var tt_l3 = new array(0,0,0,0,0,1,1,0,0,0,1,0,0,0,1,0);
var tt_l4 = new array(0,0,0,0,0,1,1,1,0,1,0,0,0,0,0,0);

var tt_j1 = new array(0,0,1,0,0,0,1,0,0,1,1,0,0,0,0,0);
var tt_j2 = new array(0,0,0,0,1,1,1,0,0,0,1,0,0,0,0,0);
var tt_j3 = new array(0,0,0,0,0,1,1,0,0,1,0,0,0,1,0,0);
var tt_j4 = new array(0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0);

var tt_i1 = new array(0,1,0,0,0,1,0,0,0,1,0,0,0,1,0,0);
var tt_i2 = new array(0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1);
var tt_i3 = new array(0,0,1,0,0,0,1,0,0,0,1,0,0,0,1,0);
var tt_i4 = new array(0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1);

var table = new array(
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
      1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,
                1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1)

}
else
alert("you need ie4+ to play tetris!")

function dimension2(row,col,num){
    var i = row * num + col;
    return (this);
}
array.prototype.getd = dimension2;

function showblock(x,y,block_type,color){
    for (var i=0;i<block_tbl.rows.length;i++){
      for (var j=0;j<block_tbl.rows(i).cells.length;j++){      
            var d2 = i * 4 + j;
            if (block_type==1){
                block_tbl.rows(i).cells(j).style.background = color;
            }
            else{
                block_tbl.rows(i).cells(j).style.background = "";
            }
      }
    }
    block_div.style.pixelleft=x;
    block_div.style.pixeltop=y;
}

function showblock1(x,y,block_type,color,obj_tabid,obj_divid){
    for (var i=0;i<obj_tabid.rows.length;i++){
      for (var j=0;j<obj_tabid.rows(i).cells.length;j++){      
            var d2 = i * 4 + j;
            if (block_type==1){
                obj_tabid.rows(i).cells(j).style.background = color;
            }
            else{
                obj_tabid.rows(i).cells(j).style.background = "";
            }
      }
    }
    obj_divid.style.pixelleft=x;
    obj_divid.style.pixeltop=y;
}

function change(inc){
    var type = curblock.substr(0,4);
    var num = curblock.substr(curblock.length-1);
    num = parseint(num) + inc;
    if (num>4||num<1) num -= 4*inc;
    type += num;
    eval("arr_tmp = " + type + ";");
    if (canmove(curx,cury,arr_tmp)){
      curblock = type;
      eval("arr_curblock = " + curblock + ";");
      eval("showblock(block_div.style.pixelleft,block_div.style.pixeltop," + curblock + ",colors[" + cid + "]);");
    }
}

function canmove(x,y,block){
    for (i=0;i<4;i++){
      for (j=0;j<4;j++){
            if (block.getd(i,j,4)&table.getd(y/n_incstep+i,x/n_incstep+j,16)) return false;
      }
    }
    return true;
}

function newblock(){
    curblock = nextblock;
    cid = ncid;
    bid = math.round(math.random()*(blocks.length-1));
    ncid = math.round(math.random()*(colors.length-1));
    nextblock = blocks;
    eval("arr_curblock = " + curblock + ";");
    eval("showblock(120,0," + curblock + ",colors[" + cid + "]);");
    eval("arr_curblock = " + curblock + ";");
    eval("showblock1(466,116," + nextblock + ",colors[" + ncid + "],nblock_tbl,nblock_div);");
}

function saveblock(){
    for (i=0;i<4;i++){
      for (j=0;j<4;j++){
            table[(cury/n_incstep+i)*16+curx/n_incstep+j]|=arr_curblock;
            if (arr_curblock==1)
                if ((cury/n_incstep+i<21)&&(curx/n_incstep+j>1)&&(curx/n_incstep+j<14))
                  table_tbl.rows(cury/n_incstep+i).cells(curx/n_incstep+j).style.background = colors;
            if (table[(cury/n_incstep+i)*16+curx/n_incstep+j]!=1)
                  table_tbl.rows(cury/n_incstep+i).cells(curx/n_incstep+j).style.background = "black";
      }
    }
}

function delline(line){
    for(i=line;i>0;i--){
      for(j=2;j<14;j++){
            table=table[(i-1)*16+j];
      }
    }
    table_tbl.deleterow(line);
    table_tbl.insertrow(0);
    for (i=0;i<16;i++){
      table_tbl.rows(0).insertcell();
      if (i<2||i>13) table_tbl.rows(0).cells(i).style.background="navy";      
    }
    killedlines++;
    cll.innertext=parseint(cll.innertext)+1;
}

function dellines(){
    var c,d,i,j;
    d=0;
    cury=block_div.style.pixeltop;
    for(i=(cury/20+3);i>cury/20-1;i--){
      c=0;
      for(j=2;j<14;j++){
            if (isnan(table)||i==21) break;
            c+=table;
      }
      if(c==12){
            delline(i);
            i++;
            d++;
      }
    }
    if (d>0)
      sco.innertext=parseint(sco.innertext)+d*d*36;
}

function lucifer(){
    for(var i=2;i<14;i++){
      if (table==1) return true;
    }
    return false;
}

function gameover(){
    gameover=1;
    clearinterval(gameinterval);
    block_div.innerhtml="";
    for (i=0;i<21;i++){
      for (j=2;j<14;j++){
            settimeout("table_tbl.rows(" + i + ").cells(" + j + ").style.background = colors;",16*i*j);
      }
    }
    nblock_div.innerhtml = "game over";
}



function document_onkeydown() {
    if (gameover==1) return;
    with (block_div.style){
    curx = pixelleft;
    cury = pixeltop;
    switch (event.keycode){
      case 37:
            if (canmove(curx-n_incstep,cury,arr_curblock))
                pixelleft-=n_incstep;
            break;
      case 38:
            change(1);
            break;
      case 39:
            if (canmove(curx+n_incstep,cury,arr_curblock))
                pixelleft+=n_incstep;
            break;
      case 40:
            if (canmove(curx,cury+n_incstep,arr_curblock)){
                pixeltop+=n_incstep;
            }
            else{
                saveblock();
                dellines();
                if (lucifer()){
                  gameover();
                  return;
                }
                sco.innertext=parseint(sco.innertext)+2;
                newblock();
            }
            break;
      case 32:
            if (pause==0){
                clearinterval(gameinterval);
                pause=1;
            }
            else{
                gameinterval=window.setinterval("handle_interval()",(maxspeed-speed+1)*60);
                pause=0;
            }
            break;
      case 90:      
            change(1);
            break;
      case 88:   
            change(-1);
            break;
      default:
    }
    }
}

function handle_interval(){
    curx = block_div.style.pixelleft;
    cury = block_div.style.pixeltop;
    if (canmove(curx,cury+n_incstep,arr_curblock)){
      block_div.style.pixeltop+=n_incstep;
    }
    else{
      saveblock();
      dellines();
      if (lucifer()){
            gameover();
            return;
      }
      sco.innertext=parseint(sco.innertext)+2
      newblock();
    }
    if (killedlines>=clr_per_line){
      killedlines-=clr_per_line;
      if (speed<maxspeed)
            speed++;
      else
            speed=maxspeed;
      spd.innertext=speed;
      clearinterval(gameinterval);
      gameinterval=window.setinterval("handle_interval()",(maxspeed-speed+1)*60);
    }
}
//-->
</script>

<script language=javascript for=document event=onkeydown>
<!--
if (document.all)
document_onkeydown()
//-->
</script>

</head>

<body language=javascript>

<div id=block_div style="left:60;top:0">
<table id=block_tbl border=0 cellspacing=0 cellpadding=0>
<script>
if (document.all){
for (var i=0;i<4;i++){
    document.write("<tr>");
    for (var j=0;j<4;j++){
      document.write("<td style=\"border:1 solid black;\"></td>");
    }
    document.write("</tr>");
}
}
</script>
</table>
</div>

<div id=nblock_div>
<table id=nblock_tbl border=0 cellspacing=0 cellpadding=0>
<script>
if (document.all){
for (var i=0;i<4;i++){
    document.write("<tr>");
    for (var j=0;j<4;j++){
      document.write("<td style=\"height:40;width:40;border:1 outset black;\"></td>");
    }
    document.write("</tr>");
}
}
</script>
</table>
</div>

<div id=table_div>
<table id=table_tbl border=0 cellspacing=0 cellpadding=0>
<script>
if (document.all){
for (var i=0;i<22;i++){
    document.write("<tr>");
    for (var j=0;j<16;j++){
      var d2 = i * 16 + j;
      if (table==1)
      document.write("<td bgcolor=navy></td>");
      else
      document.write("<td style=\"background:black;\"></td>");
    }
    document.write("</tr>");
}
}
</script>
</table>
</div>

<div id=title_div nowrap>please input speed (1-9) to begin: <input type="text" size=8 id="speedin"> <button onclick="begintet()" id="but">submit!</button></div>


<div id=infobar_div>
<table border=1 bordercolor=navy cellspacing=0 cellpadding=0>
<tr align=center>
<td style="color:99ccff;font:12 system;width:56;">speed:</td>
<td style="color:red;font:12 system;" id=spd>1</td>
<td style="color:99ccff;font:12 system;width:86;">total score:</td>
<td style="color:red;font:12 system;" id=sco>0</td>
<td style="color:99ccff;font:12 system;width:96;">cleared lines:</td>
<td style="color:red;font:12 system;" id=cll>0</td>
</tr>
</table>
</div>

<script id=mainsection language=javascript>
<!--
if (document.all){
ncid = math.round(math.random()*(colors.length-1));
bid = math.round(math.random()*(blocks.length-1));
nextblock = blocks;
newblock();
}
function begintet(){
document.all.speedin.disabled=true
document.all.but.disabled=true
speed=parseint(document.all.speedin.value);
if (isnan(speed)||speed==null||speed>maxspeed||speed<1) speed=1;
spd.innertext=speed;
gameinterval=window.setinterval("handle_interval()",(maxspeed-speed+1)*60);
}


//-->
</script>
</body>
</html>
页: [1]
查看完整版本: 游戏类俄罗斯方块