当前位置:主页>FLASH AS 编程>AS游戏篇>文章内容
  • 类似俄罗斯式拼方块游戏
  • 来源:羽柴|鬼铃-blog 作者:羽柴|鬼铃 2007-10-29 【
FlashAs作品发表>>我要投稿 | FlashAs讨论区>>AS论坛
Flash Media Server学习站>>www.FMScn.com


基本原理和俄罗斯方块一样简单,维持2维数组碰撞。成形后消除。

拼成如下图形后消失。


============核心源码===========
function 变量初始() {
    场景高格数 = 12;
    场景宽格数 = 12;
    单元格高宽 = 30;
    移动时间 = 1000;
    游戏结束 = false;
    场景数组 = new Array();
    //生成2维数组
    for (var i = 0; i<场景高格数; i++) {
        场景数组[i] = new Array();
        for (var j = 0; j<场景宽格数; j++) {
            场景数组[i][j] = 0;
            //动态生出测试文本
            this.createTextField("文本"+i+"@"+j, (i*场景高格数+j)*10, i*单元格高宽, j*单元格高宽, 100, 100);
            this["文本"+i+"@"+j].color = 0xff0000;
            this["文本"+i+"@"+j].text = 0;
        }
    }
    //
    出现位置 = [5, 0];
    方块数量 = 0;
    生成方块();
    //在原点上的增加位 2维增加位 [1,1],[0,0]  前2位为第1行,后2位为第2行
    形状数组 = [["无需数据"], [1, 0], [0, 1], [-1, 0], [0, -1]];
}
变量初始();
//
function 生成方块() {
    trace("ss");
    if (!游戏结束) {
        方块数量++;
        attachMovie("方块", "方块"+方块数量, 方块数量);
        形状 = int(random(4))+1;
        当前操作方块 = this["方块"+方块数量];
        this["方块"+方块数量].形状 = 形状;
        this["方块"+方块数量].gotoAndStop(形状);
        this["方块"+方块数量].运行 = 运行;
        this["方块"+方块数量].移动 = 移动;
        this["方块"+方块数量].计算可否移动 = 计算可否移动;
        this["方块"+方块数量]._x = 出现位置[0]*单元格高宽+15;
        this["方块"+方块数量]._y = 出现位置[1]*单元格高宽+15;
        this["方块"+方块数量].运行(移动时间, 形状);
    }
}
function 运行(移动时间, 形状) {
    this.移动间隔 = setInterval(移动, 移动时间, this);
}
function 移动($this) {
    //trace("移动 "+$this);
    $this._y += 30;
    //计算可否移动
    计算可否移动($this, $this._x, $this._y, $this.形状);
}
function 横向移动($this, 方向) {
    var 二维坐标 = [int($this._x/单元格高宽), int($this._y/单元格高宽)];
    //trace(二维坐标)
    if (方向 == "左" && (场景数组[二维坐标[0]-1][二维坐标[1]] == 0 && 场景数组[二维坐标[0]+形状数组[$this.形状][0]-1][二维坐标[1]+形状数组[$this.形状][1]] == 0)) {
        $this._x -= 30;
    } else if (方向 == "右" && (场景数组[二维坐标[0]+1][二维坐标[1]] == 0 && 场景数组[二维坐标[0]+形状数组[$this.形状][0]+1][二维坐标[1]+形状数组[$this.形状][1]] == 0)) {
        $this._x += 30;
    }
}
function 计算可否移动($this, X, Y, 形状) {
    当前2维坐标 = [int(X/单元格高宽), int(Y/单元格高宽)];
    //trace(当前2维坐标)
    //trace(场景数组[当前2维坐标[0]][当前2维坐标[1]+1]);
    //判断游戏是否结束
    //判断是否碰到阻碍
    if (场景数组[当前2维坐标[0]][当前2维坐标[1]+1] != 0 || 场景数组[当前2维坐标[0]+形状数组[$this.形状][0]][当前2维坐标[1]+形状数组[$this.形状][1]+1] != 0) {
        //trace("形状="+$this.形状);
        //trace("形状数组="+形状数组[$this.形状]);
        场景数组[当前2维坐标[0]][当前2维坐标[1]] = 1;
        this["文本"+当前2维坐标[0]+"@"+当前2维坐标[1]].text = 1;
        场景数组[当前2维坐标[0]+形状数组[形状][0]][当前2维坐标[1]+形状数组[$this.形状][1]] = 2;
        this["文本"+(当前2维坐标[0]+形状数组[$this.形状][0])+"@"+(当前2维坐标[1]+形状数组[$this.形状][1])].text = 1;
        //trace(this["文本"+(当前2维坐标[0]+形状数组[$this.形状][0])+"@"+(当前2维坐标[1]+形状数组[$this.形状][1])]);
        var temp形状 = $this.形状;
        clearInterval($this.移动间隔);
        $this.removeMovieClip();
        //trace(this["方块"+当前2维坐标[0]+"@"+当前2维坐标[1]]);
        //
        是否清除 = 判断可否消除();
        if (是否清除) {
        } else {
            //同位置生成
            attachMovie("方块", "方块"+当前2维坐标[0]+"@"+当前2维坐标[1], 方块数量);
            this["方块"+当前2维坐标[0]+"@"+当前2维坐标[1]]._x = 当前2维坐标[0]*单元格高宽+15;
            this["方块"+当前2维坐标[0]+"@"+当前2维坐标[1]]._y = 当前2维坐标[1]*单元格高宽+15;
            this["方块"+当前2维坐标[0]+"@"+当前2维坐标[1]].形状 = temp形状;
            this["方块"+当前2维坐标[0]+"@"+当前2维坐标[1]].gotoAndStop(temp形状);
        }
        if (当前2维坐标[1]-1 == 0 || (当前2维坐标[1]+形状数组[$this.形状][1])-1 == 0) {
            trace("======游戏结束=======");
            游戏结束 = true;
            return;
        } else {
            生成方块();
        }
    }
}
function 判断可否消除() {
    for (var i = 1; i<场景高格数-1; i++) {
        for (var j = 1; j<场景宽格数-1; j++) {
            if (场景数组[i-1][j-1] == 1 && 场景数组[i][j-1] == 2 && 场景数组[i+1][j-1] == 1 && 场景数组[i-1][j] == 2 && 场景数组[i+1][j] ==2 && 场景数组[i-1][j+1] == 1 && 场景数组[i][j+1] == 2 && 场景数组[i+1][j+1] == 1) {
                trace("拼成一个 "+i+"@"+j);
                this["文本"+(i-1)+"@"+(j-1)].text = 0;
                this["文本"+(i)+"@"+(j-1)].text = 0;
                this["文本"+(i+1)+"@"+(j-1)].text = 0;
                this["文本"+(i-1)+"@"+(j)].text = 0;
                this["文本"+(i+1)+"@"+(j)].text = 0;
                this["文本"+(i-1)+"@"+(j+1)].text = 0;
                this["文本"+(i)+"@"+(j+1)].text = 0;
                this["文本"+(i+1)+"@"+(j+1)].text = 0;
                //                
                this["方块"+(i-1)+"@"+(j-1)].removeMovieClip();
                this["方块"+(i+1)+"@"+(j-1)].removeMovieClip();
                this["方块"+(i-1)+"@"+(j+1)].removeMovieClip();
                this["方块"+(i+1)+"@"+(j+1)].removeMovieClip();
                //
                场景数组[i-1][j-1] = 0;
                场景数组[i][j-1] = 0;
                场景数组[i+1][j-1] = 0;
                场景数组[i-1][j] = 0;
                场景数组[i+1][j] = 0;
                场景数组[i-1][j+1] = 0;
                场景数组[i][j+1] = 0;
                场景数组[i+1][j+1] = 0;
                return true;
            }
        }
    }
    return false;
}
//热键
var keyListener:Object = new Object();
keyListener.onKeyDown = function() {
    //trace(Key.getCode());
    //空格键
    if (Key.isDown(32) || Key.isDown(38)) {
        当前操作方块.形状++;
        if (当前操作方块.形状>4) {
            当前操作方块.形状 = 1;
        }
        当前操作方块.gotoAndStop(当前操作方块.形状);
    }
    //向下键                                      
    if (Key.isDown(40)) {
        if (!游戏结束) {
            移动(当前操作方块);
        }
    }
    if (Key.isDown(37)) {
        横向移动(当前操作方块, "左");
    }
    if (Key.isDown(39)) {
        横向移动(当前操作方块, "右");
    }
};
Key.addListener(keyListener);




源码下载:
下载文件 点击下载此文件




上一篇:利用递归空间聚合来检测碰撞   下一篇:FLASH AS 游戏:A*寻路(初级)
  • 用户名:新注册) 密码: 匿名评论
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规)

Copyright © 2006-2008 flashas.net All Rights Reserved.
网站内容咨询: admin#flashas.net (#为@) 联系QQ:40777822 浙ICP备06033001号
(本网站最佳浏览解析度为1024*768, 建议使用IE 6.0或以上版本浏览器。)