← 都来码 / Dolaima← 例程索引d/app/whack.d▶ 在浏览器里试玩 原文

d/app/whack.d · 5974 字节 · 下载原文

▶ 在浏览器里试玩

// 打地鼠 —— 用 **D** 写的手机小游戏
// Whack-a-mole -- a small phone game written in **D**
//
// 玩法:3×3 的洞里随机冒地鼠,30 秒内点得越多分越高;点空扣分。
//
// 跑法 —— 手机:vml run examples/d/whack.d
// 跑法 —— 桌面:vmlcli Examples/d/whack.d --screen 480x640 --frames /tmp/fr

#include <vml_ui.d>

char[] digit(int d) {
    if (d == 0) { return "0"; }
    if (d == 1) { return "1"; }
    if (d == 2) { return "2"; }
    if (d == 3) { return "3"; }
    if (d == 4) { return "4"; }
    if (d == 5) { return "5"; }
    if (d == 6) { return "6"; }
    if (d == 7) { return "7"; }
    if (d == 8) { return "8"; }
    return "9";
}

void drawNum(int x, int y, int v, int col, int sz) {
    int p1000 = v / 1000;
    int p100 = (v / 100) % 10;
    int p10 = (v / 10) % 10;
    int p1 = v % 10;
    int cx = x;
    if (p1000 > 0) {
        ui_2d_text(cx, y, digit(p1000), col, sz, 0);
        cx = cx + sz / 2;
        ui_2d_text(cx, y, digit(p100), col, sz, 0);
        cx = cx + sz / 2;
    } else if (p100 > 0) {
        ui_2d_text(cx, y, digit(p100), col, sz, 0);
        cx = cx + sz / 2;
    }
    if (p1000 > 0 || p100 > 0 || p10 > 0) {
        ui_2d_text(cx, y, digit(p10), col, sz, 0);
        cx = cx + sz / 2;
    }
    ui_2d_text(cx, y, digit(p1), col, sz, 0);
}

void main() {
    int k = ui_win_open("打地鼠", 0, 0);
    k = ui_scr_set_keep_on(1);
    int w = ui_scr_get_w();
    int h = ui_scr_get_h();
    if (w <= 0) { w = 360; }
    if (h <= 0) { h = 620; }

    int small = w;
    if (h < small) { small = h; }

    // 版面按画布现算:顶部计分 + 中间 3×3 洞位 + 底部提示。
    int stat_fs = small / 16;
    if (stat_fs < 16) { stat_fs = 16; }
    if (stat_fs > 40) { stat_fs = 40; }
    int label_fs = small / 30;
    if (label_fs < 9) { label_fs = 9; }
    if (label_fs > 16) { label_fs = 16; }
    int hint_fs = small / 26;
    if (hint_fs < 9) { hint_fs = 9; }
    if (hint_fs > 16) { hint_fs = 16; }

    int stat_y = stat_fs + 4;
    int label_y = stat_y + label_fs + 4;
    int hint_y = label_y + hint_fs + 8;
    int top = hint_y + 8;
    int pad = w / 40;
    if (pad < 8) { pad = 8; }
    int bottom_y = h - 8;
    int bottom_top = h - (hint_fs + 12);
    int avail_h = bottom_top - top;
    if (avail_h < 30) { avail_h = 30; }
    int cellw = w - 2 * pad;
    if (avail_h < cellw) { cellw = avail_h; }
    cellw = cellw / 3;
    if (cellw < 8) { cellw = 8; }
    int ox = (w - cellw * 3) / 2;
    int oy = top + (avail_h - cellw * 3) / 2;
    if (oy < top) { oy = top; }
    int inset = cellw / 12;
    if (inset < 4) { inset = 4; }
    if (inset > 20) { inset = 20; }
    int score = 0;
    int ticks = 0;
    int move = 0;
    int over = 0;
    int frames = 0;

    int[9] hole;
    int i = 0;
    while (i < 9) { hole[i] = 0; i = i + 1; }
    hole[ui_math_rand(9)] = 1;

    int tid = ui_timer_set(100, 0);

    while (frames < 30000) {
        k = ui_2d_clear(-15724520);
        i = 0;
        while (i < 9) {
            int r = i / 3;
            int c = i - r * 3;
            int tx = ox + c * cellw + inset;
            int ty = oy + r * cellw + inset;
            int hs = cellw - inset * 2;
            if (hole[i] == 1) {
                k = ui_2d_rect(tx, ty, hs, hs, -13730510, 1, 0, 10);
                k = ui_2d_circle(tx + hs / 2, ty + hs / 2, hs / 4, -2516442, 1, 0);
            } else {
                k = ui_2d_rect(tx, ty, hs, hs, -14408660, 1, 0, 10);
            }
            i = i + 1;
        }
        drawNum(pad, stat_y, score, -1513232, stat_fs);
        k = ui_2d_text(pad, label_y, "得分", -6643536, label_fs, 0);
        int rem = 300 - ticks;
        if (rem < 0) { rem = 0; }
        int remv = rem / 10;
        int rem_w = stat_fs / 2;
        if (remv >= 10) { rem_w = stat_fs; }
        drawNum(w - pad - rem_w, stat_y, remv, -10044566, stat_fs);
        k = ui_2d_text(w - pad, label_y, "剩余秒", -6643536, label_fs, 2);
        if (over == 1) {
            k = ui_2d_text(w / 2, hint_y, "时间到!ESC 再来", -2073552, hint_fs, 1);
        } else {
            k = ui_2d_text(w / 2, hint_y, "点冒头的绿色地鼠", -6643536, hint_fs, 1);
        }
        k = ui_2d_text(w / 2, bottom_y, "30 秒计分 · ESC 重开", -6643536, hint_fs, 1);
        k = ui_2d_present();

        int msg = ui_msg_fetch_wait(0);
        if (msg == 10) { frames = 30000; }
        if (msg == 6) {
            int ax = ui_msg_get_a();
            int ay = ui_msg_get_b();
            if (over == 0 && ax >= ox && ay >= oy) {
                int c2 = (ax - ox) / cellw;
                int r2 = (ay - oy) / cellw;
                if (c2 < 3 && r2 < 3) {
                    int j = r2 * 3 + c2;
                    if (hole[j] == 1) {
                        score = score + 10;
                        int ii = 0;
                        while (ii < 9) { hole[ii] = 0; ii = ii + 1; }
                        hole[ui_math_rand(9)] = 1;
                    } else {
                        score = score - 5;
                    }
                }
            }
        }
        if (msg == 1) {
            int key = ui_msg_get_a();
            if (key == 27) {
                score = 0; ticks = 0; over = 0; move = 0;
                int ii = 0;
                while (ii < 9) { hole[ii] = 0; ii = ii + 1; }
                hole[ui_math_rand(9)] = 1;
            }
        }
        if (msg == 9) {
            frames = frames + 1;
            if (over == 0) {
                ticks = ticks + 1;
                move = move + 1;
                if (move >= 6) {
                    move = 0;
                    int ii = 0;
                    while (ii < 9) { hole[ii] = 0; ii = ii + 1; }
                    hole[ui_math_rand(9)] = 1;
                }
                if (ticks >= 300) { over = 1; }
            }
        }
    }

    k = ui_timer_kill(tid);
    k = ui_scr_set_keep_on(0);
    k = ui_win_close();
}