import win.ui;
import winex.desktop
/*DSG{{*/
var winform = ..win.form(text="aardio Form";right=303;bottom=109;mode="popup";title=false)
winform.add(
about={cls="static";left=20;top=30;right=264;bottom=80;color=255;notify=1;transparent=1;z=1}
)
/*}}*/
var scrWidth, scrHeight = win.getScreen();
var tmpWidth = scrWidth * 15;
var tmpHeight = scrHeight * 15;
var iconTop = 2;
var iconLeft = 21;
var iconSize = 75;
var depHeight = math.floor(tmpHeight / (15 * iconSize));
var depWidth = math.floor(tmpWidth / (15 * iconSize));
var count = winex.desktop.listview.count; //桌面图标个数
var snakeLen; //贪吃蛇当前长度
var head; //贪吃蛇的头索引
tab = {};
class iconObj{
int i; //当前引号
int n; //下一个索引号
int x; //坐标 X
int y; //坐标 Y
}
// 创建每个图标的对象
for(i=1; count; 1){
table.insert(tab, iconObj(), i);
}
// 移动细节
moveTo = function(iconIndex){
winex.desktop.listview.setItemPos(iconIndex,
tab[iconIndex].x * iconSize + iconLeft,
tab[iconIndex].y * iconSize + iconTop);
}
//随机食物
showGem = function(num){
math.randomize();
var finded = false ;
while( !finded ){
tab[num].x = math.floor(math.random() * depWidth);
tab[num].y = math.floor(math.random() * depHeight);
finded = true;
for(i=1; snakeLen; 1){
if(tab[i].x == tab[num].x and
tab[i].y == tab[num].y) {
finded = false;
}
}
}
moveTo(num);
}
//游戏初始化
initGame = function(){
winex.desktop.listview.setExtended(0x80000/*_LVS_EX_SNAPTOGRID*/,false);
winex.desktop.listview.modifyStyle(0x100/*_LVS_AUTOARRANGE*/);
if(count< 2) {
win.msgboxErr("图标少于2个");
}
for(i=1;count;1){
var moveToX = -100;
var moveToY = 0;
win.delay(10);
winex.desktop.listview.setItemPos(i, moveToX, moveToY);
}
//初始化蛇的长度
tab[1].n = 1;
tab[1].x = 0;
tab[1].y = depHeight - 1;
moveTo(1);
head = 1;
snakeLen = 1;
showGem(snakeLen + 1);
}
initGame();
// 定时器蛇的移动
var tmId = winform.setInterval(
200,
function(hwnd, msg, id, tick){
c = tab[head];
select(vbKey) {
case 1 {
c.x = c.x-1;
}
case 2 {
c.y =c.y-1;
}
case 3 {
c.x = c.x+1;
}
case 4 {
c.y = c.y+1;
}
}
//判定吃到到食物
if(c.x = tab[snakeLen + 1].x and
c.y = tab[snakeLen + 1].y) {
snakeLen = snakeLen + 1;
tab[snakeLen].n = tab[head].n;
tab[head].n = snakeLen;
head = snakeLen;
if(snakeLen >= count) {
winform.clearInterval(id) ;
hotket.close();
winform.msgbox("你赢了");
winform.close();
winex.desktop.listview.modifyStyle(, 0x100/*_LVS_AUTOARRANGE*/ );
winex.desktop.listview.setExtended(0x80000/*_LVS_EX_SNAPTOGRID*/, true);
}
else {
showGem(snakeLen + 1);
}
}
else {
//撞墙咬尾
if(c.x < 0 or c.x >= depWidth or
c.y < 0 or c.y >= depHeight) {
winform.clearInterval(id);
if(winform.msgbox("您撞墙了","失败")){
winex.desktop.listview.modifyStyle( , 0x100/*_LVS_AUTOARRANGE*/ );
winex.desktop.listview.setExtended(0x80000/*_LVS_EX_SNAPTOGRID*/, true);
hotket.close();
winform.close();
return ;
}
}
else {
for(i=1; snakeLen; 1){
if( i!= head and tab[i].x = tab[head].x and
tab[i].y = tab[head].y) {
winform.clearInterval(id);
if(winform.msgbox("您咬到自己的脚了","失败")){
winex.desktop.listview.modifyStyle( , 0x100/*_LVS_AUTOARRANGE*/ );
winex.desktop.listview.setExtended(0x80000/*_LVS_EX_SNAPTOGRID*/, true);
hotket.close();
winform.close();
}
}
}
}
head = tab[head].n;
tab[head].x = c.x;
tab[head].y = c.y;
moveTo (head);
}
}
)
// 以下是程序运行开始.
//注册热键
import key.hotkey
hotket = key.hotkey(winform);
// left
hotket.reg(
37,
function(hwnd, ...) {
vbKey = 1;
winform.show(false);
}
)
// up
hotket.reg(
38,
function(hwnd, ...) {
vbKey = 2;
winform.show(false);
}
)
// right
hotket.reg(
39,
function(hwnd,...) {
vbKey = 3;
winform.show(false);
}
)
// down
hotket.reg(
40,
function(hwnd, ...) {
vbKey = 4;
winform.show(false);
}
)
// ESC
hotket.reg(
27,
function(hwnd, ...){
winex.desktop.listview.modifyStyle(, 0x100/*_LVS_AUTOARRANGE*/ );
winex.desktop.listview.setExtended(0x80000/*_LVS_EX_SNAPTOGRID*/, true);
winform.close();
}
)
winform.about.text = "玩法:上下左右 按ESC退出,按方向键界面自动消失。"
winform.show();
win.loopMessage();