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="狀態(tài)欄";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;//畫圖標(biāo)志位
var map={};//初始圖表
var slm={};//已落子圖表id
var waypath={};//已走過的
//var mixpath={};//無重復(fù)路
var crs=26;//線 列數(shù)
var rs=26;//線 行數(shù)
var step=20;//步長
var boxin=crs*rs;//入口 id
var boxout=1;//出口 id
var curid=boxin;//當(dāng)前位置
setwh=function(){//設(shè)置畫圖區(qū)域
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 默認(rèn)過濾重復(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){//內(nèi)部墻
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){//測試當(dāng)前是否為雷
if(table.find(slm,cid)){return true;};//當(dāng)前是雷
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){//選中 沒用到這個函數(shù)
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){//默認(rèn)半徑為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為數(shù)組={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*/ );
//創(chuàng)建一個文字路徑
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文字區(qū)域{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*/ );//創(chuàng)建畫筆,畫筆pen只能畫一個輪廓(畫線描邊)
var brushtxt = gdip.solidBrush(rgbs);//創(chuàng)建刷子,畫刷可以對一個東西進(jìn)行填充(刷子)
family = gdip.family( "Verdana"? ); ////創(chuàng)建FontFamily字體
strformat = gdip.stringformat();//創(chuàng)建stringFormat
strformat.align =align;//1/*_StringAlignmentCenter*/; //設(shè)置樣式 水平居中
strformat.lineAlign =valign;// 1/*_StringAlignmentCenter*/ ; //設(shè)置樣式 垂直居中
rclayout = ..gdip.RECTF(xx[1],xx[2],xx[3],xx[4]);//設(shè)置文字區(qū)域
path = gdip.path(); //創(chuàng)建一個文字路徑
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){//取當(dāng)前位 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){//畫背景線和標(biāo)位點
? ? 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="無法到達(dá)終點,請重新生成地圖";waypath={};dflag=false;}
else {waypath={};mainForm.sts.text="請開始你的冒險之旅吧";}
}
mainForm.onActivate = function(state,hwndOther,minimized){
if(state && dflag){//防止畫布變成空白
mainForm.qzmap.redraw();
redraw(mainForm.qzmap);
}
}//窗口恢復(fù)顯示
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();
?