分享拼图游戏源码
By
admin
at 2020-01-11 • 0人收藏 • 1276人看过
感谢 aardio培训群: 巴克队长 分享源代码
import fonts.fontAwesome import fsys import math import win.ui; /*DSG{{*/ var winform = win.form(text="快乐拼图";right=1182;bottom=808) winform.add( bk={cls="plus";left=6;top=203;right=606;bottom=803;bgcolor=15780518;dl=1;dt=1;z=1}; btFromLocal={cls="plus";text="从图库加载";left=957;top=5;right=1064;bottom=32;bgcolor=12639424;dl=1;dt=1;iconStyle={align="left";font=LOGFONT(name='FontAwesome');padding={left=5}};iconText='\uF26C';notify=1;z=6}; btFromWeb={cls="plus";text="从网络获取";left=1070;top=5;right=1177;bottom=32;bgcolor=12639424;dl=1;dt=1;iconStyle={align="left";font=LOGFONT(name='FontAwesome');padding={left=5}};iconText='\uF26B';notify=1;z=7}; btLastOne={cls="plus";left=6;top=2;right=206;bottom=202;bgcolor=12639424;border={color=-4144960;width=1};dl=1;dt=1;notify=1;z=2}; btStart={cls="plus";text="开始拼图";left=379;top=146;right=606;bottom=202;bgcolor=12639424;dl=1;dt=1;iconStyle={align="left";font=LOGFONT(name='FontAwesome')};notify=1;z=3}; cbbLevel={cls="combobox";left=266;top=177;right=373;bottom=200;dl=1;dt=1;edge=1;items={"简单";"一般";"困难"};mode="dropdownlist";z=10}; etHistory={cls="plus";left=214;top=2;right=606;bottom=144;align="left";bgcolor=10789024;border={color=-6250332;width=1};dl=1;dt=1;editable=1;forecolor=15793151;iconColor=65535;iconStyle={align="left";font=LOGFONT(h=-16;name='FontAwesome')};iconText='\uF091战绩';multiline=1;paddingLeft=50;valign="top";z=11}; etPath={cls="plus";text="D:\Users\Desktop\照片";left=627;top=5;right=949;bottom=30;align="left";bgcolor=10789024;dl=1;dt=1;editable=1;forecolor=15793151;iconColor=65535;iconStyle={align="left";font=LOGFONT(h=-16;name='FontAwesome');padding={left=2}};iconText='\uF07B 路径';paddingBottom=1;paddingLeft=60;paddingRight=1;paddingTop=1;textPadding={left=2;top=3};z=5}; etSteps={cls="plus";text="0";left=214;top=146;right=375;bottom=173;bgcolor=10789024;border={color=-6250332;width=1};dl=1;dt=1;forecolor=15793151;iconColor=65535;iconStyle={align="left";font=LOGFONT(h=-16;name='FontAwesome');padding={left=2}};iconText='\uF29D步数';paddingLeft=50;z=8}; static1={cls="plus";left=214;top=175;right=375;bottom=202;bgcolor=10789024;border={color=-6250332;width=1};dl=1;dt=1;forecolor=15793151;iconColor=65535;iconStyle={align="left";font=LOGFONT(h=-16;name='FontAwesome')};iconText='\uF11B难度';paddingLeft=50;z=9}; stlc={cls="static";text="local";left=626;top=40;right=1172;bottom=803;dl=1;dt=1;notify=1;transparent=1;z=4} ) /*}}*/ math.randomize() winform.cbbLevel.selIndex=1; import web.form var wb = web.form(winform.stlc) //只要是web窗体external内的成员,都可以从网页上调用 wb.external = { showImg = function (path){ path = string.replace(path,"^<@file:///@>","") winform.bk.background=path winform.msgbox("图像以更新,要立即开始新游戏,请点击开始") } } var _html =/* <!doctype html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <style type="text/css"> html,body{ height:100%; margin:0; } img {width:200px;height:200px;margin=125px;} </style> </head> <body> */ //从本地加载 winform.btFromLocal.oncommand = function(id,event){ var path = io.exist(winform.etPath.text) if(!path or !fsys.isDir(path)){ //不存在路径或者不是目录,则返回 winform.msgboxErr("设定的目录不存在,或者不是目录","路径错误") return ; } var html = _html fsys.enum( path, {"*.png";"*.jpg";"*.bmp";"*.jpeg"}, function(dir,filename,fullpath,findData){ if(filename) html = string.concat(html,`<img border="2" src="`,fullpath,`" onclick='external.showImg(this.src)' >`) } ,false/*如果此参数为false则忽略子目录*/ ); wb.html = string.concat(html,`</body></html>`) } //从网络加载图片 import web.rest.client var client = web.rest.client() var getBingPic=client.api("https://bing.ioliu.cn/?p={page}","GET",lambda(r) string.map(r,`\<img.+?src=\"(\S+?)\"\>`) ) winform.btFromWeb.oncommand = function(id,event){ var page = math.random(1,100) var list = getBingPic[page]() var html = _html for(i=1;#list;1) html = string.concat(html,`<img border="2" src="`,list[i],`" onclick='external.showImg(this.src)' >`) wb.html = string.concat(html,`</body></html>`) } //难度等级对应的plus尺寸 var size //3,4,5 var level ={ 简单=3; //3*3=9块, 一般=4; //4*4 困难=5; //5*5 } //计算相邻块的位置,返回{左右上下} var getNearPos=function(){ var left = winform.pic1.unFixId -1 var right = winform.pic1.unFixId +1 var top = winform.pic1.unFixId-size var bottom = winform.pic1.unFixId+size var max = size*size return table.map({left;right;top;bottom},function(v,k,result){ //仅保留有效的位置 if(v>0 and v<= max) table.push(result,v) }) ; } //与空白块pic1交换位置 var swap=function(picClicked){ var nearPos = getNearPos() var picCount = size*size var countStep=true if(!picClicked){//打乱顺序时是没有这个参数的,故此要从相邻位置中随机一个随机选取一个 var ind = math.random(1,picCount) picClicked = winform["pic"++ ind] countStep = false } if(countStep) winform.etSteps.text += 1 //修改步数,打乱时不记录 if( table.find(nearPos, picClicked.unFixId ) ){ //交换编号 var temid = picClicked.unFixId picClicked.unFixId=winform.pic1.unFixId winform.pic1.unFixId = temid var rc1=winform.pic1.getRect() var rc2=picClicked.getRect() winform.pic1.setRect(rc2) picClicked.setRect(rc1) } } //打乱顺序 var randomSwitch=function(picList){ //先把左上角第一个块设置为空块 winform.btLastOne.background = picList[1] winform.pic1.background = null var picCount = size*size for(i=1;picCount*10;1) swap() //依次重复打乱10次 winform.msgbox("顺序已打乱,可以开始了") } //按难度生成plus控件,并随机分配小图片 var creatPlus=function(picList){ var rect = winform.bk.getRect(true) //获取背景图片的位置 var w,h=rect.width()/size,rect.height()/size winform.bk.show(false) //隐藏背景 winform.btLastOne.setRect({left=6;right=6+w;top=203-h;bottom=203})//调整保留块的大小 //调整下 winform.btLastOne的尺寸 var t={} //临时序号 for(row=1;size;1){//总的图片数量 for(col=1;size;1){ var index = (row-1)*size + col var frdName = "pic"++index winform.add([frdName]={fixId=index;cls="plus";left=1;top=1;right=2;bottom=2;border={color=-4144960;width=2};db=0.85;dr=0.9;dt=0.03;notify=1;z=2};) var rc = table.clone(rect) rc.left += (col-1) * w rc.right = rc.left + w rc.top += (row-1) * h rc.bottom = rc.top + h winform[frdName].setRect(rc,true) winform[frdName].unFixId = index winform[frdName].background = picList[index] winform[frdName].oncommand = function(id,event){ swap(owner) //点击时交换 } } } } //复位 var reset=function(){ winform.etSteps.text=0 //删除所有已经存在的控件 for(name,ctrl in winform.eachControl("plus") ) if(string.startWith(name,"pic")) winform[name].close() winform.bk.show(true) winform.btLastOne.background=null } //开始运行 winform.btStart.oncommand = function(id,event){ var bitmap = winform.bk.getBackground() //返回背景图像,返回值为gdip.bitmap对象,背景色返回null if(!bitmap){ winform.msgboxErr("尚未设定图片") return ; } reset() size=level[[winform.cbbLevel.text]] : 4 //默认普通难度 var pics = bitmap.split(size,size) //获取图片并分割 creatPlus(pics) //按难度生成plus控件,并随机分配小图片 randomSwitch(pics) //打乱顺序 } //最后一块,检测是否拼图成功 winform.btLastOne.oncommand = function(id,event){ if(winform.pic1.unFixId==1){ //第一块为空白时才动作,否则不动 //检查是否已经完成 var isok=true for(name,ctrl in winform.eachControl("plus") ){ if(string.startWith(name,"pic")){ isok = isok and (winform[name].fixId ==winform[name].unFixId) //所有的序号都对上了就证明完成了 if(!isok) return ;//不对的话,不再继续判断 } } //成功,则空白块与当前块获取当前块的图片 winform.pic1.setBackground(owner.getBackground().saveToBuffer()) winform.etHistory.editBox.print(tostring(time(,"%Y-%m-%d %H:%M:%S")),winform.cbbLevel.text,winform.etSteps.text) reset() winform.msgbox("恭喜成功") } } winform.show(); win.loopMessage();
登录后方可回帖