import win.ui;
/*DSG{{*/
mainForm = win.form(text="迷宫";right=561;bottom=588;bgcolor=32960;border="dialog frame")
mainForm.add(
map={cls="plus";left=13;top=13;right=549;bottom=549;bgcolor=33023;clipBk=false;edge=1;notify=1;z=1};
qzmap={cls="plus";left=24;top=17;right=560;bottom=553;clipBk=false;edge=1;notify=1;transparent=1;z=2};
start={cls="button";text="开 始";left=64;top=552;right=146;bottom=579;border=1;db=1;dl=1;z=3};
sts={cls="static";text="状态栏";left=180;top=556;right=506;bottom=577;align="center";border=1;db=1;dl=1;dr=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 mixpath={};//无重复路
var crs=26;//线 列数
var rs=26;//线 行数
var step=20;//步长
var boxin=crs*rs;//入口 id
var boxout=1;//出口 id
var curid=boxin;//当前位置
setwh=function(){//设置画图区域
mainForm.map.top=10;
mainForm.map.left=10;
mainForm.map.width=mainForm.width-35;
mainForm.map.height=mainForm.height-90;
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(mp){//初始化
map={};
slm={};
hids={};
waypath={};
var mid=1;
step=20;
if(mp){
crs=math.floor((mp.width-10)/step);
rs=math.floor((mp.height-15)/step);
boxin=crs*rs;
boxout=1;
}
for(i=1;rs*step;step){// row*step-step+1
for(j=1;crs*step;step){// col*step-step+1
tadd(map,{7+j,7+i,step-4,step-4,mid});//left,top,width,height,id
mid++;
}
}
for(i=1;crs;1){//四边墙
if(!table.find(map,i)){tadd(slm,i);}//第一行
if(!table.find(map,crs*(rs-1)+i)){tadd(slm,crs*(rs-1)+i);}//最后一行
}
for(i=1;rs;1){
if(!table.find(map,crs*i)){tadd(slm,crs*i);}//最后的一列
if(!table.find(map,crs*(i-1)+1)){tadd(slm,crs*(i-1)+1);}//第一列
}
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(#map-crs+1,#map-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*(rs-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,cid,rrgbs=0xFA0000FF){//选中 没用到这个函数
drawrect(mp,map[cid],rrgbs);
tadd(waypath,d);
}//点选
showmap=function(mp){//显示所有棋位框,测试时用,实际用不上
//import console;
for(i=1;#slm;1){
//console.log(""++rs*crs++" "++#map++" "++slm[i])
//if(slm[i]>#map){table.removeByValue(slm,slm[i]);}
//else
{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.orange);
drawtext(mp,map[boxout],"出",12,argb.white);
drawcur(mp);//
}
}
drawcur=function(mp){//画头位置 并判断是否已到终点
drawcircle(mp,{map[curid][1]+3,map[curid][2]+3},8,argb.orange);
if(curid==boxout){winner(mp,"你");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();
}
drawrect=function(mp,cid,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,cid[1],cid[2],cid[3],cid[4]);
if(flag){
var brush = gdip.solidBrush(trgbs);
graphics.fillRectangle( brush, cid[1],cid[2],cid[3],cid[4]);
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,w,h},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[4]);//设置文字区域
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;
var wx=(mp.width-390)/2;
var wy=(mp.height-100)/2;
drawrect(mp,{wx,wy,390,100},argb.blue);
drawtext(mp,{wx+10,wy,380,100},"恭喜 "+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;
var irs=rs*step+1;
gh.drawRectangle(pen,4,4,icrs+3,irs+3);
for(i=1;icrs;step){
gh.drawLine(pen,i+5,5,i+5,irs+5);
}
for(i=1;irs;step){
gh.drawLine(pen,5,i+5,icrs+5,i+5);
}
pen.delete();
}//画背景线
startgame=function(){
setwh();
initmap(mainForm.map);
dflag=true;
mainForm.map.redraw();//画线
showmap(mainForm.map);
if(!testpath(boxin,boxout)){mainForm.sts.text="无法到达终点,请重新生成地图";waypath={};dflag=false;}
else {waypath={};mainForm.sts.text="请开始你的冒险之旅吧";}
}
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){
setwh();
initline(graphics);
}
}//前景刷新
mainForm.start.oncommand = function(id,event){// 开始
startgame();
}
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);
drawrect(mainForm.qzmap,map[curid],argb.white);
curid=curid-crs;
drawcur(mainForm.qzmap);
//redraw(mainForm.qzmap);
}
};
};
{
//ctrl = true;
vkey =0x28/*_VK_DOWN*/ or's' or 'S'#;
oncommand = function(){
//mainForm.sts.text="下";
if(!testlei(curid+crs) && (curid+crs)<(crs*rs)){
tadd(waypath,curid);
drawrect(mainForm.qzmap,map[curid],argb.white);
curid=curid+crs;
drawcur(mainForm.qzmap);
//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);
drawrect(mainForm.qzmap,map[curid],argb.white);
curid--;
drawcur(mainForm.qzmap);
//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);
drawrect(mainForm.qzmap,map[curid],argb.white);
curid++;
drawcur(mainForm.qzmap);
//redraw(mainForm.qzmap);
}
};
};
},mainForm );
mainForm.show();
startgame();
return win.loopMessage();