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=22;top=15;right=558;bottom=551;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;//窗体快捷键库
//import win.timer;//定时器库
math.randomize();//随机种子
var dflag=false;//画图标志位
var map={};//地图表id
var slm={};//墙图表id
var wayline={};//蛇身id
var food={id=30,n=3};//食物{id,n};
var crs=26;//线 列数
var rs=26;//线 行数
var step=20;//地图步长
var curid=1;//头 id
var ss=1;//向右走
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;
targb=0x00000000;//透明色
};//定义几个常用 色
buildfood=function(){//随机食物
var cid=math.random(1,crs*rs);
while(table.find(slm,cid) or table.find(wayline,cid)){
cid=math.random(1,crs*rs);
}
food.id=cid;
food.n=math.random(2,5);
}
showfood=function(mp){
buildfood();
drawcircle(mp,{map[food.id][1]+3,map[food.id][2]+3},8,argb.blue,false);
drawtext(mp,map[food.id],food.n-1,9,argb.yellow);
}
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={};
wayline={};
var mid=1;
step=20;
if(mp){
crs=math.floor((mp.width-10)/step);
rs=math.floor((mp.height-15)/step);
}
for(i=1;rs*step;step){//初始化地图
for(j=1;crs*step;step){
tadd(map,{7+j,7+i,step-4,step-4,mid,0});//left,top,width,height,id,stay
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);}//第一列
}
while(table.find(slm,curid)){//蛇头
curid=math.floor(math.random(crs+1,#map-crs-1));
}
map[curid][6]=1;//可向右走
table.push(wayline,curid);
} //初始化地图
testlei=function(cid){//测试当前是否为雷
if(table.find(slm,cid) || table.find(wayline,cid)){return true;};//当前是墙
return(false);
}
showmap=function(mp){//画出围墙
for(i=1;#slm;1){
drawrect(mp,map[slm[i]],argb.orange);
}
}
redraw=function(mp){//刷新
if(dflag){
for(i=1;#slm;1){//墙
drawrect(mp,map[slm[i]],argb.orange);
}
drawcur(mp);//画蛇身
}
}
drawcur=function(mp){//画蛇身
var tt;
if(!testlei(curid+ss)){
map[curid][6]=ss;
curid=curid+ss;
drawcircle(mp,{map[curid][1]+3,map[curid][2]+3},8,argb.orange);
for(i=1;#wayline+1;1){//比身体多一节,用于清除尾部痕迹
tt=tonumber(wayline[i]);
if(testlei(tt)){
drawcircle(mp,{map[tt][1]+3,map[tt][2]+3},8,argb.orange+i*0xf0f/3,false);
//drawtext(mp,map[tt],#wayline-i,9,argb.yellow)
if(!testlei(wayline[i]+map[tt][6])){
wayline[i]=wayline[i]+map[tt][6];
}
if(tt==(wayline[#wayline]-map[tt][6])){//清除尾烟儿
hidecur(mp,tt);
map[tt][6]=0;
}
}
}
}
}
hidecur=function(mp,cid=0){//遮盖尾部
/**
var graphics=gdip.graphics(mp);
var x=map[curid][1]-0.5;
var y=map[curid][2]-0.5;
//graphics.compositingMode = 1/*_CompositingModeSourceCopy*/;//切换为替换绘图模式
graphics.smoothingMode = 4/*_GdipSmoothingModeAntiAlias*/ ; //为了圆形画的平滑自然,加上抗锯齿功能
var brush = gdip.solidBrush(); //创建一个透明刷子
var hpen=gdip.pen(0xffff800,1,2);
graphics.drawRectangle(hpen, x,y,17.5,17.5);
graphics.fillRectangle(brush, x,y,17.5,17.5);
//graphics.fillEllipse(brush, x,y,17,17);//用透明刷子擦除中心圆形
brush.delete();
hpen.delete();
//graphics.compositingMode = 0/*_CompositingModeSourceOver*///恢复为叠加绘图模式
**/
if(cid!=0){
drawrect(mp,map[cid],0xffff8000);
}
}
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();
} //画矩形
drawtext=function(mp,xx,tt,tss,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*/, tss, rclayout, strformat);
graphics.fillPath( brushtxt, path)//fillPath填充路径
//graphics.drawPath( pen, path)//drawPath描边
//删除所有GDI+对象
brushtxt.delete();
pentxt.delete() ;
strformat.delete();
family.delete();
path.delete();
} //写字
gameover=function(mp){//胜利 提示
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},"天呢 你 撞墙了",45,argb.yellow);
}
startgame=function(){//游戏开始
setwh();
initmap(mainForm.map);
dflag=true;//窗体完全载入并显示后,设置为true,然后重画窗体内容
ss=-ss;//反方向走,防止一直提示游戏结束
mainForm.map.redraw();//画线
showmap(mainForm.map);
showfood(mainForm.qzmap);
}
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();
}//画背景线
addfood=function(t,cid,n){//吃
var x={};
for(i=1;n;1){
table.push(x,cid);
}
for(i=1;#t;1){
tadd(x,t[i]);
}
t=x;
return(t);
}
var goxy=function(){//移动
if(!testlei(curid+ss)){
drawcur(mainForm.qzmap);
//redraw(mainForm.qzmap);
} else {
if(table.find(wayline,curid+ss) && curid+ss==wayline[2]){
ss=-ss;
} else {
gameover(mainForm.qzmap);
}
}//撞墙了或咬到自己了。。。
if(curid==food.id){//吃到食物
wayline=addfood(wayline,curid,food.n);
buildfood();
showfood(mainForm.qzmap);
}
}
var winhotkey = win.ui.accelerator({//测试按键,控制走动方向。
{
vkey =0x26/*_VK_UP*/ ;
oncommand = function(){//"上";
ss=-crs;
};
};
{
vkey =0x28/*_VK_DOWN*/;
oncommand = function(){//"下";
ss=crs
};
};
{
vkey = 0x25/*_VK_LEFT*/;
oncommand = function(){//"左";
ss=-1
};
};
{
vkey =0x27/*_VK_RIGHT*/;
oncommand = function(){//"右";
ss=1
};
};
//想斜着跑就按下 Ctrl键
{
ctrl = true;
vkey =0x26/*_VK_UP*/ ;
oncommand = function(){//左上
ss=-crs-1;
};
};
{
ctrl = true;
vkey =0x28/*_VK_DOWN*/;
oncommand = function(){//右下
ss=crs+1
};
};
{
ctrl = true;
vkey = 0x25/*_VK_LEFT*/;
oncommand = function(){//左下
ss=crs-1;
};
};
{
ctrl = true;
vkey =0x27/*_VK_RIGHT*/;
oncommand = function(){//右上
ss=-crs+1;
};
};
},mainForm );//
mainForm.onActivate = function(state,hwndOther,minimized){
if(state && dflag){//防止画布变成空白
mainForm.qzmap.redraw();
redraw(mainForm.qzmap);
}
}//窗口恢复显示
mainForm.onPosChanged = function(flags,hwndInsertAfter,x,y,cx,cy){
setwh();
mainForm.map.redraw();
showmap(mainForm.qzmap);
}
mainForm.setInterval(//计时器
function(){
if(dflag){goxy();}
},200
)
mainForm.map.onDrawContent = function(graphics,rc,txtColor,rcContent,foreColor){
if(dflag){
setwh();
initline(graphics);
}
}//前景刷新
mainForm.start.oncommand = function(id,event){// 开始
startgame();
}
mainForm.sts.text="点击 上 下 左 右键来控制方向";
mainForm.show();
startgame();
return win.loopMessage();