11RIA 闪客社区 - 最赞 Animate Flash 论坛

搜索
查看: 1941|回复: 0
上一主题 下一主题

[高级教程] 【9RIA—沐枫】—【基础小知识】第29节(扫雷)

[复制链接] TA的其它主题
发表于 2018-2-6 21:10:29 | 显示全部楼层 |阅读模式

【游客模式】——注册会员,加入11RIA 闪客社区吧!一起见证Flash的再次辉煌……

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
本帖最后由 TKCB 于 2018-2-6 21:12 编辑

转载:9RIA游戏开发者社区(天地会)
作者:沐枫(原天地会大神)


【基础小知识】系列教程(总目录)——沐枫大神出品


其实我是不喜欢把代码都写在一个类上的,不过为了方便理解,我还是写在了一个类上,免得大家到时不好看

效果演示在最下面哦

[Actionscript3] 纯文本查看 复制代码
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;

public class Minesweeper extends Sprite
{
private var shuchu_txt:TextField;
private var kongzhi_txt:TextField;
private var xianshi_txt:TextField;

private const WIDTH:uint=9;//9列
private const HEIGHT:uint=9;//9行
private const MINES:uint=12;//地雷数
// 对的,这三个参数,想怎么设就怎么设,不影响游戏运行,不过为了省事,就不设置娜么多按钮调数了,直接常量,各位没意见吧。
private var arr:Array=[];
private var map_arr:Array=[];//存放小格子
private var num:uint=0;//存放红旗下数组的值
private var leinum:uint=0;//点了几个旗子
private var bool:Boolean;//是否可点
private var boolen:Boolean;

public function Minesweeper()
{
kongzhi_txt=new TextField();
kongzhi_txt.text="重新开始";
kongzhi_txt.x=300;
kongzhi_txt.y=300;
kongzhi_txt.background=true;
kongzhi_txt.backgroundColor=0x00ff00;
kongzhi_txt.border=true;
kongzhi_txt.selectable=false;
kongzhi_txt.width=200;
kongzhi_txt.height=70;
var newformat:TextFormat = new TextFormat();
newformat.size = 48;
kongzhi_txt.setTextFormat(newformat);
addChild(kongzhi_txt);
kongzhi_txt.addEventListener(MouseEvent.CLICK,onKongZhiHandler);

shuchu_txt=new TextField();
shuchu_txt.text="shift左键猜雷";
shuchu_txt.y=300;
shuchu_txt.background=true;
shuchu_txt.backgroundColor=0x0000ff;
shuchu_txt.border=true;
shuchu_txt.selectable=false;
shuchu_txt.width=280;
shuchu_txt.height=70;
var format:TextFormat = new TextFormat();
format.size = 48;
shuchu_txt.setTextFormat(format);
addChild(shuchu_txt);

var txt:TextField=new TextField();
txt.text="提示";
txt.x=380;
txt.background=true;
txt.backgroundColor=0xffff00;
txt.border=true;
txt.autoSize=TextFieldAutoSize.CENTER;
txt.selectable=false;
addChild(txt);
var newFormat:TextFormat = new TextFormat();
newFormat.size = 18;
txt.setTextFormat(newFormat);
txt.addEventListener(MouseEvent.CLICK,onTxtClickHandler);
dataArr();
dataMap();
}

protected function onKongZhiHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
// 重新开始
var len:uint=map_arr.length;
for(var i:uint=0;i<len;i++)
{
map_arr.gotoAndStop(1);
map_arr.addEventListener(MouseEvent.CLICK,onMapClickHandler);
}
shuchu_txt.text="shift左键猜雷";
var format:TextFormat = new TextFormat();
format.size = 48;
shuchu_txt.setTextFormat(format);
addChild(shuchu_txt);
if(boolen)
{
xianshi_txt.visible=false;
}
arr=[];
dataArr();
bool=false;
}

private function dataMap():void
{
// TODO Auto Generated method stub
// 初始化地图
for(var i:uint=0;i<HEIGHT;i++)
{
for(var j:uint=0;j<WIDTH;j++)
{
var map:Map=new Map();
map.x=map.width*j+20;
map.y=map.height*i+20;
map.buttonMode=true;
map.name=j+"_"+i;//给每个小方格起个名字,下面递归时会用到
addChild(map);
map_arr.push(map);
// if(arr[j]==0)
// {
// map.gotoAndStop(2);
// map.txt.text="";
// }
// else if(arr[j]==9)
// {
// map.gotoAndStop(3);
// }
// else
// {
// map.gotoAndStop(2);
// map.txt.text=arr[j].toString();
// }
// 因为这个挂实在是太牛逼了,所以不得不把它注了
map.mouseChildren=false;//子级不支持点击事件
map.addEventListener(MouseEvent.CLICK,onMapClickHandler);//至于这一行是干嘛的,我想就没必要浪费口水了
}
}
}

protected function onMapClickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
var map:Map=event.target as Map;//弱转换,把当前点击的目标命名为map
var wid:uint=Math.floor(mouseX/map.width-0.2);//得出点击的列(索引)
var hit:uint=Math.floor(mouseY/map.height-0.2);//得出点击的行(索引),呃,这里的0。2 是因为我的影片剪辑带边框,所以不太精确,我手动调的
// trace("第"+(hit+1).toString()+"行","第"+(wid+1).toString()+"列");
if(bool==false)
{
if(event.shiftKey)//是否按住shift键
{
if(map.currentFrame==1)
{
map.gotoAndStop(4);
num+=arr[hit][wid];
leinum++;
}
else if(map.currentFrame==4)
{
map.gotoAndStop(1);
num-=arr[hit][wid];
leinum--;
}
// trace(num);
if(num==9*MINES&&leinum==MINES)//全部猜中雷时
{
shuchu_txt.text="好吧,你赢了";
bool=true;
}
}
else
{
if(arr[hit][wid]==9)
{
map.gotoAndStop(3);
bool=true;
shuchu_txt.text="呃,怎么输了";
}
else if(arr[hit][wid]==0)
{
forZoom(wid,hit);//递归
}
else
{
map.gotoAndStop(2);
map.txt.text=arr[hit][wid].toString();
}
}
// trace(leinum);
var format:TextFormat = new TextFormat();
format.size = 46;
shuchu_txt.setTextFormat(format);
addChild(shuchu_txt);
}
}

protected function onTxtClickHandler(event:MouseEvent):void
{
// TODO Auto-generated method stub
xianshi_txt=new TextField();
for(var i:uint=0;i<HEIGHT;i++)
{
xianshi_txt.appendText(arr+"\n");
}
xianshi_txt.x=380;
xianshi_txt.y=50;
xianshi_txt.background=true;
xianshi_txt.backgroundColor=0xaaff00;
xianshi_txt.border=true;
xianshi_txt.autoSize=TextFieldAutoSize.CENTER;
addChild(xianshi_txt);
var newFormat:TextFormat = new TextFormat();
newFormat.size = 20;
xianshi_txt.setTextFormat(newFormat);
xianshi_txt.visible=true;
boolen=true;
}

private function forZoom(wid:int,hit:int):void//传入行数和列数值
{
var map:Map=this.getChildByName(wid+"_"+hit) as Map;
if(map.currentFrame==1)
{
map.gotoAndStop(2);
map.removeEventListener(MouseEvent.CLICK,onMapClickHandler);
if(arr[hit][wid]>0)
{
map.txt.text=arr[hit][wid].toString();
}
else
{
map.txt.text="";
}
if(arr[hit][wid]==0)
{
for(var h:int=-1;h<=1;h++)//遍历小格子所在的九宫格
{
for(var w:int=-1;w<=1;w++)
{
if(wid+w!=-1&&wid+w!=WIDTH&&hit+h!=-1&&hit+h!=HEIGHT)
{
if(h!=0||w!=0)//不等于自己
{
if(arr[hit+h][wid+w]!=9)
{
forZoom((wid+w),(hit+h));
}
}
}
}
}
}
}
}

private function dataArr():void
{
// TODO Auto Generated method stub
// 生成全部元素为0的二维数组
// 即:
// 0,0,0,0,0,0,0,0,0
// 0,0,0,0,0,0,0,0,0
// 0,0,0,0,0,0,0,0,0
// 0,0,0,0,0,0,0,0,0
// 0,0,0,0,0,0,0,0,0
// 0,0,0,0,0,0,0,0,0
// 0,0,0,0,0,0,0,0,0
// 0,0,0,0,0,0,0,0,0
// 0,0,0,0,0,0,0,0,0
for(var i:uint=0;i<HEIGHT;i++)
{
arr.push(new Array);//每行添加一个新的空数组
for(var j:uint=0;j<WIDTH;j++)
{
// 为每行新的空数组添加9个为0的元素
arr.push(0);
}
// trace(arr);
}
// OK,已生成生成全部元素为0的9*9二维数组
// 下面,随机添加9个地雷
i=0;
while(i<MINES)
{
var wid:uint=Math.floor(Math.random()*WIDTH);//随机列0~8
var hit:uint=Math.floor(Math.random()*HEIGHT);//随机行0~8
// 下面写个if判断,避免随机数有重复的就不好了
if(arr[hit][wid]!=9)
{
arr[hit][wid]=9;//有雷的位置 值为9
i++;
}
}
// for(i=0;i<HEIGHT;i++)
// {
// trace(arr);
// }
// 下面来修改二维数组添加提示数据,都知道什么是提示数据吧。什么?不知道。好吧,提示数据就是当自身不是地雷时,它会扫描自身所在的九宫格里是否存在地雷,若存在则显示成存在的地雷数。OK。
// 遍历整个数组,寻找九宫格内雷数。
for(i=0;i<HEIGHT;i++)
{
for(j=0;j<WIDTH;j++)
{
if(arr[j]!=9)
{
for(var h:int=-1;h<=1;h++)
{
for(var w:int=-1;w<=1;w++)
{
if(i+h!=-1&&i+h!=HEIGHT&&j+w!=-1&&j+w!=WIDTH)//当然,不能出现arr[-1]或者arr[9]的情况
{
if(arr[i+h][j+w]==9)
{
arr[j]++;
}
}
}
}
}
}
// 好吧,我承认我表达能力不够强,这个就靠个人理解能力了
// trace(arr);
}
}
}
}




下载:
扫雷.zip (28.38 KB, 下载次数: 2)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐 上一条 /1 下一条

感谢所有支持论坛的朋友:下面展示最新的5位赞助和充值的朋友……更多赞助和充值朋友的信息,请查看:永远的感谢名单

SGlW(66139)、 anghuo(841)、 whdsyes(255)、 longxia(60904)、 囫囵吞澡(58054)

下面展示总排行榜的前3名(T1-T3)和今年排行榜的前3名的朋友(C1-C3)……更多信息,请查看:总排行榜今年排行榜

T1. fhqu1462(969)、 T2. lwlpluto(14232)、 T3. 1367926921(962)  |  C1. anghuo(147)、 C2. fdisker(27945)、 C3. 囫囵吞澡(58054)



快速回复 返回顶部 返回列表