|
/upimg/soft/1_061225125319.fla |
老大让弄个扫雷游戏出来,俺以前也很想做,只是总感觉没有时间,不知道在忙什么呢?呵呵... 闲话不多说了,整理了下,象这种游戏个人感觉都可以用数组来生成游戏的地图,扫雷主要算法的一个是地图的生成与空方块的清除。
function createRandom(total:Number, selNum:Number):Array
{
myRandom = [];
var myTemp:Array = [];
for (var i = 1; i < total; i++) {
myTemp.push(i);
}
for (var i = 1; i <= selNum; i++) {
mytemp = Math.floor(Math.random() * myTemp.length);
myRandom.push(myTemp[mytemp]);
myTemp.splice(mytemp, 1);
}
trace(myRandom);
return myRandom;
}
function createArray(tileNum_h, tileNum_w)
{
for (var i = 0; i < tileNum_h; i++) {
mainTileArray = [];
for (var j = 0; j < tileNum_w; j++) {
mainTileArray[j] = 0;
}
}
//初始化数组
for (var i = 0; i < totalBombNum; i++) {
countShowTile(myRandom);
//布雷
}
}
function countShowTile(inNum:Number)
{
var out_B:Number = inNum % tileNum_w;
out_A = int(inNum / tileNum_w);
mainTileArray[out_A][out_B] = 9;
mainTileArray[out_A][out_B] == 9 ? fullTileMcArray.push(out_A, out_B) : null;
//布雷
countShowMap(out_A, out_B);
//调用countShowMap显示雷周围数字
}
function countShowMap(A:Number, B:Number)
{
for (var i = -1; i <= 1; i++) {
for (var j = -1; j <= 1; j++) {
//显示雷周围数字
i == j && i == 0 ? null : (mainTileArray[A - i][B - j] != undefined && mainTileArray[A - i][B - j] != 9 ? mainTileArray[A - i][B - j]++ : null);
}
}
}
function copyTile()
{
for (var i = 0; i < tileNum_h; i++) {
for (var j = 0; j < tileNum_w; j++) {
_root.tile.duplicateMovieClip("myTile" + "_" + i + "_" + j, this.getNextHighestDepth());
tileMC = _root["myTile" + "_" + i + "_" + j];
tileMC._x = tileStart_x + tile_w * j;
tileMC._y = tileStart_y + tile_h * i;
userClick(tileMC);
tileMC.useHandCursor = false;
}
}
}
//复制方块到舞台
function userClick(tileMC)
{
tileMC.onRelease = function()
{
if (Click == true) {
myStr = this._name;
myStr = myStr.split("_");
myCurrentFrame = this._currentframe;
if (Key.isDown(Key.CONTROL)) {
myCurrentFrame != 10 && myCurrentFrame == 1 ? this.gotoAndStop("banner") : showTile(this, myStr[1], myStr[2]);
}
else {
myCurrentFrame != 10 ? showTile(this, myStr[1], myStr[2]) : null;
}
}
};
}
//接收用户操作
function showTile(mcName, array_1, array_2)
{
myStr[1] = array_1;
myStr[2] = array_2;
mcName.enabled = false;
if (mainTileArray[myStr[1]][myStr[2]] == 9) {
lose();
mcName.gotoAndStop("bomb");
}
else if (mainTileArray[myStr[1]][myStr[2]] == 0) {
clearEmptyTile(myStr[1], myStr[2]);
while (clearTileArray_A.length != 0)
{
tempA = clearTileArray_A.splice(0, 1);
tempB = clearTileArray_B.splice(0, 1);
clearEmptyTile(tempA[0], tempB[0]);
}
}
else {
mainOpenNUM++;
Result(mainOpenNUM);
mcName.gotoAndStop(mainTileArray[myStr[1]][myStr[2]] + 1);
}
}
//处理用户点击后的方块显示
function clearEmptyTile(A:Number, B:Number)
{
for (var i:Number = -1; i <= 1; i++) {
for (var j:Number = -1; j <= 1; j++) {
if (mainTileArray[A - i][B - j] == 0) {
clearTileArray_A.push(A - i);
clearTileArray_B.push(B - j);
mainTileArray[A - i][B - j] = "10";
_root["myTile" + "_" + (A - i) + "_" + (B - j)].gotoAndStop("empty");
mainOpenNUM++;
Result(mainOpenNUM);
trace("mainNUM: " + mainOpenNUM);
}
else if (mainTileArray[A - i][B - j] > 0 && mainTileArray[A - i][B - j] < 9) {
_root["myTile" + "_" + (A - i) + "_" + (B - j)].gotoAndStop(mainTileArray[A - i][B - j] + 1);
_root["myTile" + "_" + (A - i) + "_" + (B - j)].enabled = false;
race(_root["myTile" + "_" + (A - i) + "_" + (B - j)]);
mainTileArray[A - i][B - j] = "11";
mainOpenNUM++;
Result(mainOpenNUM);
trace("mainNUM: " + mainOpenNUM);
}
}
}
}
//清除空方块
function showBomb(NAME:String)
{
for (var i = 0; i < fullTileMcArray.length; i += 2) {
_root["myTile" + "_" + fullTileMcArray + "_" + fullTileMcArray[i + 1]].gotoAndStop(NAME);
trace("____" + _root["myTile" + "_" + fullTileMcArray + "_" + fullTileMcArray[i + 1]]);
}
Click = false;
}
function lose()
{
_root.radio.text = "lose";
showBomb("bomb");
trace("lose");
}
//失败
function Result(openNum:Number)
{
if (openNum == needOpenTileTotal) {
_root.radio.text = "success";
showBomb("banner");
trace("success*");
}
}
//-------------------------------------------------init------------------------------------------------------------
var Click:Boolean = true;
//是否接受用户事件
var tile_w:Number = 30;
//方格宽
var tile_h:Number = 30;
//方格高
var mainOpenNUM:Number = 0;
//记录打开方块数
var clearTileArray_A:Array = [];
//存放清除方块x位置数组
var clearTileArray_B:Array = [];
//存放清除方块y位置数组
var tileStart_x:Number = 86;
//初始化方块的首位置X坐标
var tileStart_y:Number = 100;
//初始化方块的首位置Y坐标
var mainTileArray:Array = [];
//地图数组
var fullTileMcArray:Array = [];
//存放雷的MC名称
var tileNum_w:Number = 20;
//横雷数
var tileNum_h:Number = 10;
//纵雷数
var totalBombNum:Number = 5;
//雷总数
needOpenTileTotal = tileNum_w * tileNum_h - totalBombNum;
createRandom(tileNum_w * tileNum_h, totalBombNum);
//返回随机数,随机雷的位置
createArray(tileNum_h, tileNum_w);
//初始化数组
copyTile();
stop();
for (var i = 0; i < tileNum_h; i++) {
trace(mainTileArray);
//显示地图
}