利用aardio制作简单的打地鼠游戏

Mr_MAO 1天前 51

import win.ui;
/*DSG{{*/
var winform = win.form(text="打地鼠游戏 (By:Mr_MAO)";right=400;bottom=450;border="dialog frame";max=false)
winform.add(
btnStart={cls="button";text="开始游戏";left=140;top=380;right=260;bottom=410;z=1};
lblScore={cls="static";text="得分: 0";left=20;top=385;right=120;bottom=410;font=LOGFONT(h=-16);transparent=1;z=2};
lblTime={cls="static";text="倒计时: 30";left=280;top=385;right=380;bottom=410;font=LOGFONT(h=-16);transparent=1;z=3}
)
/*}}*/

// 游戏全局变量
var score = 0;
var gameDuration = 30;    // 游戏时长(秒)
var timeLeft = gameDuration;
var moleTimerId = null;   // 地鼠刷新的定时器ID
var currentMoleIndex = 0; // 当前地鼠所在的位置索引
var isPlaying = false;

// 创建 3x3 的按钮矩阵 (地鼠洞)
var moles = {};
var btnSize = 100;
var gap = 10;
var startX = 40;
var startY = 40;

for(row=1;3){
    for(col=1;3){
        var idx = (row-1)*3 + col;
        
        // 动态创建按钮
        var btn = winform.add({
            cls="button";
            text=""; 
            left=startX + (col-1)*(btnSize+gap);
            top=startY + (row-1)*(btnSize+gap);
            right=startX + (col-1)*(btnSize+gap) + btnSize;
            bottom=startY + (row-1)*(btnSize+gap) + btnSize;
            font=LOGFONT(h=-40); // 大字体显示地鼠图标
            z=10
        });
        
        // 绑定点击事件
        btn.oncommand = function(id,event){
            if(!isPlaying) return;
            
            if(currentMoleIndex == idx){
                // 打中了!
                score++;
                winform.lblScore.text = "得分: " + score;
                
                // 打中后立即清除当前地鼠,并稍微加快刷新(可选)
                btn.text = "💥"; // 打中特效
                btn.disabled = true; // 防止重复点击
                win.delay(100);
                btn.disabled = false;
                btn.text = "";
                currentMoleIndex = 0; 

                refreshMole();
            }
        }
        
        moles[idx] = btn;
    }
}

// 刷新地鼠的逻辑
function refreshMole(){
    if(!isPlaying) return;

    // 清除旧的地鼠
    if(currentMoleIndex > 0){
        moles[currentMoleIndex].text = "";
        moles[currentMoleIndex].bgcolor = null; 
    }

    // 随机选择一个新的位置,尽量不与上次重复(除非运气不好)
    var newIndex = math.random(1, 9);
    
    // 更新状态
    currentMoleIndex = newIndex;
    moles[newIndex].text = "🐹"; 
}

// 游戏结束逻辑
function stopGame(){
    isPlaying = false;
    winform.clearInterval(moleTimerId);
    
    // 清理界面
    if(currentMoleIndex > 0) moles[currentMoleIndex].text = "";
    currentMoleIndex = 0;
    
    winform.btnStart.disabled = false;
    winform.msgbox("游戏结束!您的最终得分是: " + score);
}

// 游戏主倒计时逻辑
function gameTick(){
    timeLeft--;
    winform.lblTime.text = "倒计时: " + timeLeft;
    
    if(timeLeft <= 0){
        stopGame();
    }
}

// 开始按钮事件
winform.btnStart.oncommand = function(id,event){
    // 初始化变量
    score = 0;
    timeLeft = gameDuration;
    isPlaying = true;
    currentMoleIndex = 0;
    
    // 更新UI
    winform.lblScore.text = "得分: 0";
    winform.lblTime.text = "倒计时: " + timeLeft;
    winform.btnStart.disabled = true;

    math.randomize(); // 随机数种子初始化
    
    // 设置地鼠刷新定时器 (每700毫秒移动一次地鼠)
    moleTimerId = winform.setInterval( 
        700, 
        function(){
            refreshMole();
        } 
    );
    
    // 设置游戏倒计时定时器 (每1秒减少时间)
    winform.setInterval(
        1000,
        function(){
            if(isPlaying) gameTick();
            else return 0; // 返回0清除定时器
        }
    )
    
    refreshMole(); // 立即显示第一个
}

winform.show();
win.loopMessage();


最新回复 (1)
  • netfox 1天前
    0 2

返回