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

d/app/pong.d · 5678 字节 · 下载原文

▶ 在浏览器里试玩

// 乒乓 —— 用 **D** 写的手机小游戏
// Pong -- a small phone game written in **D**
//
// 玩法:左右两块挡板,球在中间弹;手指上下拖动控制你的挡板,AI 自动跟球;
// 球漏到你这一侧对方得分;先到 5 分结束,点屏幕重开。
//
// 跑法 —— 手机:vml run examples/d/pong.d
// 跑法 —— 桌面:vmlcli Examples/d/pong.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; }
    int score_fs = small / 14;
    if (score_fs < 28) { score_fs = 28; }
    if (score_fs > 56) { score_fs = 56; }
    int hint_fs = small / 26;
    if (hint_fs < 10) { hint_fs = 10; }
    if (hint_fs > 16) { hint_fs = 16; }
    int status_fs = small / 14;
    if (status_fs < 20) { status_fs = 20; }
    if (status_fs > 36) { status_fs = 36; }
    int score_y = score_fs + 8;

    int padh = h / 9;
    if (padh < 40) { padh = 40; }
    int padw = w / 36;
    if (padw < 6) { padw = 6; }
    int br = w / 60;
    if (br < 4) { br = 4; }

    int p1y = h / 2;
    int p2y = h / 2;
    int bx = w / 2;
    int by = h / 2;
    int bvx = 4;
    int bvy = 3;
    int s1 = 0;
    int s2 = 0;
    int over = 0;
    int frames = 0;
    int tid = ui_timer_set(16, 0);

    while (frames < 20000) {
        if (over == 0) {
            bx = bx + bvx;
            by = by + bvy;
            if (by < br) { by = br; bvy = 0 - bvy; }
            if (by > h - br) { by = h - br; bvy = 0 - bvy; }
            if (bvx < 0) {
                if (bx < padw + 16) {
                    if (by > p1y) {
                        if (by < p1y + padh) {
                            bx = padw + 16;
                            bvx = 0 - bvx;
                        }
                    }
                }
                if (bx < br) {
                    s2 = s2 + 1;
                    bx = w / 2;
                    by = h / 2;
                    bvx = 4;
                    bvy = 3;
                    if (s2 >= 5) { over = 1; }
                }
            } else {
                if (bx > w - padw - 16) {
                    if (by > p2y) {
                        if (by < p2y + padh) {
                            bx = w - padw - 16;
                            bvx = 0 - bvx;
                        }
                    }
                }
                if (bx > w - br) {
                    s1 = s1 + 1;
                    bx = w / 2;
                    by = h / 2;
                    bvx = 0 - 4;
                    bvy = 3;
                    if (s1 >= 5) { over = 1; }
                }
            }
            if (by > p2y + padh / 2) { p2y = p2y + 4; }
            if (by < p2y + padh / 2) { p2y = p2y - 4; }
            if (p2y < 0) { p2y = 0; }
            if (p2y > h - padh) { p2y = h - padh; }
        }

        k = ui_2d_clear(-15724520);
        k = ui_2d_rect(10, p1y, padw, padh, -10044566, 1, 0, 4);
        k = ui_2d_rect(w - padw - 10, p2y, padw, padh, -11890471, 1, 0, 4);
        k = ui_2d_circle(bx, by, br, -1513232, 1, 0);
        drawNum(w / 2 - score_fs * 3 / 2, score_y, s1, -10044566, score_fs);
        drawNum(w / 2 + score_fs / 2, score_y, s2, -11890471, score_fs);

        if (over == 1) {
            if (s1 >= 5) {
                k = ui_2d_text(w / 2, h / 2, "你赢了!", -10044566, status_fs, 1);
            } else {
                k = ui_2d_text(w / 2, h / 2, "AI 赢了", -2073552, status_fs, 1);
            }
            k = ui_2d_text(w / 2, h / 2 + status_fs + 8, "点屏幕重开", -6643536, hint_fs, 1);
        } else {
            k = ui_2d_text(w / 2, h - 8, "上下拖动控制左边挡板", -6643536, hint_fs, 1);
        }
        k = ui_2d_present();

        int msg = ui_msg_fetch_wait(0);
        if (msg == 10) { frames = 20000; }
        if (msg == 6 || msg == 7 || msg == 4) {
            int ax = ui_msg_get_a();
            int ay = ui_msg_get_b();
            if (over == 1) {
                s1 = 0; s2 = 0; over = 0;
                bx = w / 2; by = h / 2;
                bvx = 4; bvy = 3;
            } else {
                p1y = ay - padh / 2;
                if (p1y < 0) { p1y = 0; }
                if (p1y > h - padh) { p1y = h - padh; }
            }
        }
        if (msg == 1) {
            int key = ui_msg_get_a();
            if (key == 27) { frames = 20000; }
        }
        if (msg == 9) { frames = frames + 1; }
    }

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