一个简单的俄罗斯方块脚本[1]

[入库:2005年8月18日] [更新:2007年3月25日]

本文简介:选择自 bhw98 的 blog

这是一个简单的俄罗斯方块的源码,用javascript编写。它是我以前在网上偶尔发现的,觉得很有价值,于是收藏起来。遗憾的是,现在已经不知其出处了。
该脚本最大的特点(优点)是用数组存储所需数据,简化了编程,值得学习。虽没有注释,但看起来应该不难理解作者的思路。

<html>
<head>
<meta name="title" content="jscript simple tetris">
<meta content="text/html; charset=gb2312" http-equiv=content-type>
<title>俄罗斯方块</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:9pt;color:white}
#infobar_div {position:absolute;left:396;top:416;}
#infobar2_div {position:absolute;left:396;top:480;font-size:9pt;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[i]);
}
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[d2]==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;
  

本文关键:俄罗斯方块, JavaScript, 数组
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top