欢迎光临
我们一直在努力

未完成的JS作品之二:应用面向对象思想写的俄罗斯方块

建站超值云服务器,限时71元/月

练手的时候写的,后面写得好乱,8(

比较满意的是方块的储存和旋转,是我见过同类游戏中最简洁的

<html>
<head>
<title>俄罗斯方块</title>
<script>
/********************************
俄罗斯方块 version 1.0beta
2001年10月19日 by 黄砾(stone) (bbs.online.jx.cn)

这段脚本可以免费使用于任何非商业用途。引用时请保留本段说明。

tetris version 1.0beta
by li huang, october 19th 2001
*******************************/

//存储各方块的形状
var shapes = new array (
    new array (2,1,0,0,1,0,2,0,1,1),
    new array (2,1,0,0,1,0,1,1,2,1),
    new array (2,1,1,0,2,0,0,1,1,1),
    new array (1,1,0,0,1,0,0,1,1,1),
    new array (2,1,0,0,1,0,2,0,0,1),
    new array (2,1,0,0,1,0,2,0,2,1),
    new array (3,2,0,1,1,1,2,1,3,1));

function tetris(name,width,height) {
    this.width = width;
    this.height = height;
    document.write(<table name= + name + id= + name + border=1 cellspacing=0 bgcolor=#c0c0c0 bordercolor=#c0c0c0 style=\"font-size:12pt;line-height:12pt;\">);
    for (var y=0;y<this.height;y++) {
        document.write(<tr>);
        for (var x=0;x<this.width;x++)
            document.write(<td width=12 height=12>&nbsp;</td>);
        document.write(</tr>);
    }
    document.write(</table>);
    this.box = eval(name);

    this.checkdot = function(x,y) {
        if (x<0 || y<0 || x>=this.width || y>=this.height)
            return false;
        else if (this.box.rows(y).cells(x).bordercolordark == #ffffff)
            return false;
        else
            return true;
    }
    this.dot = function(x,y) {
        if (this.checkdot(x,y))
            with (this.box.rows(y).cells(x)) {
                bordercolordark = #ffffff;
                bordercolorlight = #808080;
            }
    }
    this.cleardot = function(x,y) {
        with (this.box.rows(y).cells(x)) {
            bordercolordark = #c0c0c0;
            bordercolorlight = #c0c0c0;
        }
    }
    this.eraseline = function() {
        var line = 0, ny=this.height-1;
        for (var y=this.height-1;y>=0;y–) {
            var erase=true;
            for (var x=0;x<this.width;x++)
                if (this.checkdot(x,y)) {
                    this.cleardot(x,ny);
                    erase=false;
                }
                else
                    this.dot(x,ny);
            if (!erase)
                ny–;
            else
                line++;
        }
        return line;
    }
    this.clear=function() {
        for (var y=0;y<this.height;y++)
            for (var x=0;x<this.width;x++)
                this.cleardot(x,y);
    }
}
function pos(x,y) {
    this.x = x;
    this.y = y;
    return this;
}
function block(obj,shape) {
    this.x = 0;
    this.y = 1;
    this.obj = obj;
    this.shape = new array();
    for (var i=0;i<shape.length;i+=2)
        this.shape[i>>1] = new pos(shape[i],shape[i+1]);
    this.setobj = function(obj) {
        this.obj = obj;
    }
    this.turn = function() {
        var i,tmp;
          with (this) {
            for (i=1;i<shape.length;i++) {
                tmp = shape[i].y;
                shape[i].y = shape[i].x;
                shape[i].x = shape[0].y – tmp;
            }
            tmp=shape[0].x; shape[0].x=shape[0].y; shape[0].y=tmp;
            if (!check()) {
                for (i=1;i<shape.length;i++) {
                    tmp = shape[i].x;
                    shape[i].x=shape[i].y;
                    shape[i].y=shape[0].x-tmp;
                }
                tmp=shape[0].x; shape[0].x=shape[0].y; shape[0].y=tmp;
            }
        }
    }
    this.check = function() {
        with (this)
            for (var i=1;i<shape.length;i++)
                if (!obj.checkdot(shape[i].x+x,shape[i].y+y))
                    return false;
        return true;
    }
    this.left = function() {
        with (this) {
            x–;
            if (!check())
                x++;
        }
    }
    this.right = function() {
        with (this) {
            x++;
            if (!check())
                x–;
        }
    }
    this.down = function() {
        with (this) {
            y++;
            if (!check()) {
                y–;
                return false;
            }
        }
        return true;
    }
    this.clear = function() {
        with (this)
            for (var i=1;i<shape.length;i++)
                obj.cleardot(shape[i].x+x,shape[i].y+y);
    }
    this.draw = function() {
        with (this)
            for (var i=1;i<shape.length;i++)
                obj.dot(shape[i].x+x,shape[i].y+y);
    }
}

var hiscore=0, score=0, line=0, level=1, speed=1500, handle=nan;
    
function keyplay() {
    block.clear();
    switch (window.event.keycode) {
    case 73:
    case 105:block.turn(); break;
    case 74:
    case 106:block.left(); break;
    case 75:
    case 107:if (!isnan(handle))
            window.cleartimeout(handle);
        down();
        break;
    case 76:
    case 108:block.right();
    }
    block.draw();
}
function keywait() {
    if (window.event.keycode==115 || window.event.keycode==83)
        gamestart();
}
function gamestart() {
    message.style.visibility = "hidden";
    score=0; line=0; level=1; speed=1500;

    tetris.clear();
    preview.clear();
    show();

    block = new block(tetris,shapes[math.round(math.random()*6)]);
    nextblock = new block(preview,shapes[math.round(math.random()*6)]);
    block.draw();
    nextblock.draw();
    
    handle = window.settimeout(down(),speed);
    document.body.onkeypress = keyplay;
}
function gameover() {
    handle = nan;
    document.body.onkeypress = keywait;
    message.innerhtml = 俄罗斯方块 v1.0beta<br><br><font size=5>游戏结束!</font><br><br>按 s 键重新开始,<br>j,k,l键控制方块移动,<br>按 i 键控制方块旋转<br><br>2001-10-19 by 黄砾(stone)<br>http://bbs.online.jx.cn;
    message.style.visibility = visible;
}
function show() {
    show.innerhtml = <p><b>hiscore</b><br> + hiscore + </p><p><b>score</b><br> + score + </p><p><b>level</b><br> + level + </p><p><b>line</b><br> + line + </p><p> </p><p> </p><p> </p>;
}
function down() {
    block.clear();
    if (!block.down()) {
        block.draw();
        var eraseline = tetris.eraseline();
        line += eraseline;
        score+= 50*((1<<eraseline)-1);
        if (score>hiscore)
            hiscore=score;
        if ((score>>9)>level) {
            level++;
            speed=math.round(speed/1.3);
            if (speed<80)
                speed=80;
        }
        
        nextblock.clear();
        block = nextblock;
        block.setobj(tetris);
        
        show();
        
        if (!block.check()) {
            gameover();
            return;
        }
        nextblock = new block(preview,shapes[math.round(math.random()*6)]);
        nextblock.draw();
    }
    block.draw();
    handle = window.settimeout("down()",speed);
}
</script>
</head>
<body bgcolor=#c0c0c0>
<div id=message align=center style="position:absolute; left:30; top:100; width:200; z-index:2; border:2px double white; background-color: #c0c0c0; layer-background-color: #c0c0c0; visibility: hidden"></div>
<table border=2 cellspacing=2 bgcolor=#c0c0c0 bordercolor=#c0c0c0>
<tr>
<td bordercolor=#ffffff rowspan=2><script>tetris = new tetris(box,10,20);</script></td>
<td bordercolor=#ffffff align=center><b>next</b><br><script>preview = new tetris(next,4,3);</script></td>
</tr>
<tr>
<td name=show id=show bordercolor=#ffffff align=center valign=top>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</td>
</tr>
</table>
<script>
gameover();
</script>
</body>
</html>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 未完成的JS作品之二:应用面向对象思想写的俄罗斯方块
分享到: 更多 (0)

相关推荐

  • 暂无文章