//迷宫 单机
import win.ui;
/*DSG{{*/
mainForm = win.form(text="迷宫";right=561;bottom=588;bgcolor=32960;max=false)
mainForm.add(
map={cls="plus";left=15;top=13;right=551;bottom=547;bgcolor=33023;clipBk=false;edge=1;notify=1;z=1};
qzmap={cls="plus";left=13;top=8;right=549;bottom=542;clipBk=false;edge=1;notify=1;transparent=1;z=2};
start={cls="button";text="开 始";left=63;top=551;right=145;bottom=578;border=1;z=3};
sts={cls="static";text="状态栏";left=179;top=553;right=505;bottom=574;align="center";border=1;font=LOGFONT(weight=700);transparent=1;z=4}
)
/*}}*/
import math;
import win.ui.accelerator;
var dflag=false;//画图标志位
var map={};//初始图表
var slm={};//已落子图表id
var waypath={};//已走过的
var crs=26;//线 行列数 可放棋子为 25*25
var step=20;//步长
var boxin=crs*crs;//入口 id
var boxout=1;//出口 id
var curid=boxin;//当前位置
mainForm.qzmap.top=mainForm.map.top;
mainForm.qzmap.left=mainForm.map.left;
mainForm.qzmap.width=mainForm.map.width;
mainForm.qzmap.height=mainForm.map.height;
var argb={//0xAARRGGBB
red=0xFFFF0000;
green=0xFF00FF00;
blue=0xFF0000FF;
yellow=0xFFFFFF00;
orange=0xFF800000;
black=0xFF000000;
white=0xFFFFFFFF;
};//定义几个常用 色
tadd=function(t,id,f=false){//f 默认过滤重复值
if(type(t)=="table"){
if(f){table.push(t,id);}
else{if(!table.find(t,id)){table.push(t,id);}}
}
}
initmap=function(){
map={};
slm={};
hids={};
waypath={};
var mid=1;
step=20;
for(i=1;crs*step;step){// row*step-step+1
for(j=1;crs*step;step){// col*step-step+1
tadd(map,{7+j,7+i,j+step+3,i+step+3,mid});//left,top,right,bottom,id
mid++;
}
}
for(i=1;crs;1){//四边墙
tadd(slm,i);
tadd(slm,crs*i);
tadd(slm,crs*(i-1)+1);
tadd(slm,crs*(crs-1)+i);
}
for(i=1;#map/2;1){//内部墙
tadd(slm,math.floor(math.random(1,#map)));
}
while(table.find(slm,boxout+crs)){
boxout=math.floor(math.random(2,crs-1));//出口
}
table.removeByValue(slm,boxout);
while(table.find(slm,boxin-crs)){
boxin=math.floor(math.random(crs*(crs-1)+2,crs*crs-1));//入口
}
table.removeByValue(slm,boxin);
curid=boxin;
} //初始化地图
testlei=function(cid){//测试当前是否为雷
if(table.find(slm,cid)){return true;};//当前是雷
return(false);
}
testpath=function(cid,tid){//测试可行路径 cid 起点,tid 终点
if(testlei(cid)){return false;}//此路不通,换吧
else {if(table.find(waypath,cid)){return false;} tadd(waypath,cid);}
var lb={cid-crs,//上面 1
cid-1,cid+1,//左右 2 3
cid+crs//下面4
};
if(cid<=crs && cid!=tid){lb[1]=0;}//第一行
//if(cid%crs==1){lb[2]=0;}//第一列
//if(cid%crs==0){lb[3]=0;}//最后一列
if(cid>(crs*(crs-1))){lb[4]=0;}//最后一行
if(lb[1]>0){if(testpath(lb[1],tid)){return true;}}
if(lb[2]>0){if(testpath(lb[2],tid)){return true;}}
if(lb[3]>0){if(testpath(lb[3],tid)){return true;}}
if(lb[4]>0){if(testpath(lb[4],tid)){return true;}}
if(table.find(waypath,tid)){return true;}
return false;
}
selmap=function(mp,d,rrgbs=0xFA0000FF){//选中
drawrect(mp,map[d],rrgbs);
tadd(waypath,d);
}//点选
showmap=function(mp){//显示所有棋位框,测试时用,实际用不上
for(i=1;#slm;1){
drawrect(mp,map[slm[i]],argb.orange);
}
drawtext(mp,map[boxin],"入",12,argb.white);
drawtext(mp,map[boxout],"出",12,argb.white);
drawcircle(mp,{map[curid][1]+3,map[curid][2]+3},8,argb.orange);
}
redraw=function(mp){//刷新
if(dflag){
for(i=1;#waypath;1){//已走过的路
drawrect(mp,map[waypath[i]],argb.white);
}
for(i=1;#slm;1){//墙 无法走的路
drawrect(mp,map[slm[i]],argb.orange);
}
drawtext(mp,map[boxin],"入",12,argb.white);
drawtext(mp,map[boxout],"出",12,argb.white);
drawcircle(mp,{map[curid][1]+3,map[curid][2]+3},8,argb.orange);
if(curid==boxout){winner(mainForm.qzmap,"你");dflag=false;}
}
}
drawcircle=function(mp,cid,r=8,color=0xFF000000,qz=true){//默认半径为8
var x=cid[1]+5;
var y=cid[2]+5;
var brush = gdip.solidBrush(color);
var graphics=gdip.graphics(mp);
graphics.smoothingMode = 4/*_GdipSmoothingModeAntiAlias*/ ; //为了圆形画的平滑自然,加上抗锯齿功能
graphics.fillEllipse( brush, x-r,y-r,r*2,r*2);//画圆形、或椭圆
if(qz){//眼睛和嘴,哈哈哈。。。
if(color!=argb.white){brush=gdip.solidBrush(argb.white);}
else{brush=gdip.solidBrush(argb.black);}
graphics.fillEllipse(brush,x-r/4,y+r/3,r/2,r/5);
graphics.fillEllipse(brush,x-r/2,y-r/3,r/3,r/6);
graphics.fillEllipse(brush,x+r/3,y-r/3,r/3,r/6);
}
brush.delete();
}
drawup=function(mp,md,cl=0xFF0000FF){//左 上 影
drawrect(mp,{md[1]-2,md[2]-2,md[3],md[4]},cl);
}
drawdown=function(mp,md,cl=0xFF000000){// 右 下 影
drawrect(mp,{md[1],md[2],md[3]+2,md[4]+2},cl);
}
drawrect=function(mp,xx,trgbs=0xFAFF0000,flag=true){//画矩形,xx为数组={x,y,w,h} ,mp为窗体或控件,fl:true空心或false实心
import gdip;
var graphics = gdip.graphics(mp)
var penrect = gdip.pen( trgbs, 1, 2/*_GdipUnitPixel*/ );//笔
graphics.drawRectangle( penrect, xx[1],xx[2],xx[3]-xx[1],xx[4]-xx[2]);
if(flag){
var brush = gdip.solidBrush(trgbs);
graphics.fillRectangle( brush, xx[1]+1,xx[2]+1,xx[3]-xx[1]-1,xx[4]-xx[2]-1);
brush.delete();
}
penrect.delete();
} //画矩形
drawout=function(mp,cid,color=0xFF222222){//棋位提示框,有点闪,所以没开启
var graphics=gdip.graphics(mp);
var pen = gdip.pen( color,1,2/*_GdipUnitPixel*/ );
//创建一个文字路径
pen.dashCap = _GdipLineCap;
path = gdip.path();
path.addRectangle(map[cid][1]-3,map[cid][2]-3,step-4,step-4);
graphics.drawPath( pen, path);
pen.delete();
path.delete();
}
drawtext=function(mp,xx,tt,ss,rgbs=0xFF000000,align=1,valign=1){//xx文字区域{x,y,dx,dy},tt文字,ss大小
import gdip;
var graphics = gdip.graphics(mp)//图形对象graphics(可以看作是画板)
graphics.smoothingMode = 4/*_GdipSmoothingModeAntiAlias*/ ; //加上抗锯齿功能
var pentxt = gdip.pen( rgbs, 1,2/*_GdipUnitPixel*/ );//创建画笔,画笔pen只能画一个轮廓(画线描边)
var brushtxt = gdip.solidBrush(rgbs);//创建刷子,画刷可以对一个东西进行填充(刷子)
family = gdip.family( "Verdana" ); 创建FontFamily字体
strformat = gdip.stringformat ( );//创建stringFormat
strformat.align =align;//1/*_StringAlignmentCenter*/; //设置样式 水平居中
strformat.lineAlign =valign;// 1/*_StringAlignmentCenter*/ ; //设置样式 垂直居中
rclayout = ..gdip.RECTF(xx[1],xx[2],xx[3]-xx[1]-1,xx[4]-xx[2]-1);//设置文字区域
path = gdip.path(); //创建一个文字路径
path.startFigure();
path.addstring( tt, family, 1/*_GdipFontStyleBold*/, ss, rclayout, strformat);
graphics.fillPath( brushtxt, path)//fillPath填充路径
//graphics.drawPath( pen, path)//drawPath描边
//删除所有GDI+对象
brushtxt.delete();
pentxt.delete() ;
strformat.delete();
family.delete();
path.delete();
} //写字
testxy=function(mx,my){//取当前位 map id
var cid=0;
for(i=1;#map;1){
if(mx>=map[i][1]&&mx<=map[i][3]&&my>=map[i][2]&&my<=map[i][4]){
cid=tonumber(map[i][5]);
}
}
return(cid);
} //测试 ID
winner=function(mp,txt=""){//胜利 提示
dflag=false;
drawrect(mp,{70,90,440,170},argb.blue);
drawtext(mp,{80,110,440,150},txt+" 胜了!",45,argb.yellow);
}
initline=function(graphics,ps=2,pm=2){//画背景线和标位点
var gh=graphics;
var pen=gdip.pen( argb.black,ps/* 2*/, pm /* 2=_GdipUnitPixel*/ );
var icrs=crs*step+1;
gh.drawRectangle(pen,4,4,icrs+3,icrs+3);
for(i=1;icrs;step){
gh.drawLine(pen,i+5,5,i+5,icrs+5);
gh.drawLine(pen,5,i+5,icrs+5,i+5);
}
pen.delete();
}//画背景线
mainForm.onActivate = function(state,hwndOther,minimized){
if(state && dflag){//防止画布变成空白
mainForm.qzmap.redraw();
redraw(mainForm.qzmap);
}
}//窗口恢复显示
mainForm.map.onDrawContent = function(graphics,rc,txtColor,rcContent,foreColor){
if(dflag){
initline(graphics);
}
}//前景刷新
mainForm.start.oncommand = function(id,event){// 开始
initmap();
dflag=true;
mainForm.map.redraw();//画线
showmap(mainForm.map);
if(!testpath(boxin,boxout)){mainForm.sts.text="无法到达终点,请重新生成地图";waypath={};dflag=false;}
else {waypath={};mainForm.sts.text="请开始你的冒险之旅吧";}
}
var winhotkey = win.ui.accelerator({//测试按键,控制走动方向。
{
//ctrl = true;
vkey =0x26/*_VK_UP*/ or 'w' or 'W'#;
oncommand = function(){
//mainForm.sts.text="上";
if(!testlei(curid-crs)){tadd(waypath,curid);curid=curid-crs;redraw(mainForm.qzmap);}
};
};
{
//ctrl = true;
vkey =0x28/*_VK_DOWN*/ or's' or 'S'#;
oncommand = function(){
//mainForm.sts.text="下";
if(!testlei(curid+crs)){tadd(waypath,curid);curid=curid+crs;redraw(mainForm.qzmap);}
};
};
{
//ctrl = true;
vkey = 0x25/*_VK_LEFT*/ or 'a' or 'A'#;
oncommand = function(){
//mainForm.sts.text="左";
if(!testlei(curid-1)){tadd(waypath,curid);curid--;redraw(mainForm.qzmap);}
};
};
{
//ctrl = true;
vkey =0x27/*_VK_RIGHT*/ or 'd' or 'D'#;
oncommand = function(){
//mainForm.sts.text="右";
if(!testlei(curid+1)){tadd(waypath,curid);curid++;redraw(mainForm.qzmap);}
};
};
},mainForm );
mainForm.show();
dflag=true;//窗体完全载入并显示后,设置为true,然后重画窗体内容
mainForm.map.redraw();
initmap();
showmap(mainForm.map);
if(!testpath(boxin,boxout)){mainForm.sts.text="无法到达终点,请重新生成地图";waypath={};dflag=false;}
else {waypath={};mainForm.sts.text="请开始你的冒险之旅吧";}
return win.loopMessage();