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

javascript/app/pong.js · 8411 字节 · 下载原文

▶ 在浏览器里试玩

// pong.js —— 对打(VML 的 JavaScript 前端)
// pong.js -- Pong (VML's JavaScript frontend)
//
// 与 `Examples/c/app/pong.c` / `Examples/python/app/pong.py` **同一套玩法**:
// Same rules as the C / Python versions:
//   左板跟手指(点/拖左半屏),右板是 AI 追球,先到 5 分;
//   the left paddle follows the finger (tap/drag on the left half), the right one is an AI, first to 5 wins;
//   球碰上下边界反弹,碰板反弹并提速;出界对方得分;结束后点屏幕 / ESC / 回车重开。
//   the ball bounces off the top/bottom and off a paddle (speed up); going out scores for the opponent;
//   tap / ESC / Enter restarts when the match is over.
//
// ⚠ 本前端硬约束(见 `memory.js` 文件头):
// ⚠ Hard constraints of this frontend (see the header of `memory.js`):
//   · **没有整除**:`/` 返回浮点、`| 0` 被忽略 ⇒ 一律走 `pgDiv`;
//   · **no integer division**: `/` yields a float and `| 0` is ignored => always go through `pgDiv`;
//   · **函数里的 `var` 是全局标签** ⇒ 本文件局部变量带 `pg` 前缀;
//   · **a `var` inside a function is a global label** => locals here carry a `pg` prefix;
//   · **`push` 不扩容**(本文件全是定长数组)。
//   · **`push` never grows** (everything here is a fixed array).
//
// 跑法 —— 手机:`vml run examples/javascript/pong.js`
// How to run — on the phone: `vml run examples/javascript/pong.js`
// 跑法 —— 桌面:`vmlcli Examples/javascript/pong.js --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/javascript/pong.js --screen 480x640 --frames /tmp/fr`

var MSG_KEYDOWN = 1;
var MSG_MOUSEDOWN = 4;
var MSG_TOUCHDOWN = 6;
var MSG_TIMER = 9;
var MSG_CLOSE = 10;
var MSG_RESIZE = 11;
var KEY_ESC = 27;

var MAX_FRAMES = 6000;
var WIN_SCORE = 5;

var C_BG = -15724520;
var C_LINE = -14013898;
var C_LEFT = -11409298;
var C_RIGHT = -11890471;
var C_BALL = -1513232;
var C_TEXT = -1513232;
var C_DIM = -6643536;

var G_SW = 360;
var G_SH = 620;
var G_PAD_W = 12;
var G_PAD_H = 70;
var G_BALL_R = 6;
var G_LEFT_Y = 310;
var G_RIGHT_Y = 310;
var G_BALL_X = 180;
var G_BALL_Y = 310;
var G_BVX = -5;
var G_BVY = 3;
var G_SCORE_L = 0;
var G_SCORE_R = 0;
var G_ENDED = 0;
var G_FRAMES = 0;
var G_SEED = 20262;
var G_SERVE_DIR = 0;
var G_LANG = 0;

function pgDiv(pa, pb) {
    if (pb <= 0) { return 0; }
    var pq = 0;
    var pr = pa;
    if (pr < 0) { pr = 0 - pr; }
    while (pr >= pb) {
        pr = pr - pb;
        pq = pq + 1;
    }
    if (pa < 0) { return 0 - pq; }
    return pq;
}

function pgDigitStr(pd) {
    if (pd == 0) { return "0"; }
    if (pd == 1) { return "1"; }
    if (pd == 2) { return "2"; }
    if (pd == 3) { return "3"; }
    if (pd == 4) { return "4"; }
    if (pd == 5) { return "5"; }
    if (pd == 6) { return "6"; }
    if (pd == 7) { return "7"; }
    if (pd == 8) { return "8"; }
    return "9";
}

function pgRnd(pn) {
    G_SEED = (G_SEED * 1103 + 12345) % 32768;
    return pgDiv(G_SEED, 256) % pn;
}

function pgLayout() {
    G_SW = ui_scr_get_w();
    G_SH = ui_scr_get_h();
    if (G_SW <= 0) { G_SW = 360; }
    if (G_SH <= 0) { G_SH = 620; }
    G_PAD_W = pgDiv(G_SW, 28);
    if (G_PAD_W < 8) { G_PAD_W = 8; }
    G_PAD_H = pgDiv(G_SH, 6);
    if (G_PAD_H < 40) { G_PAD_H = 40; }
    G_BALL_R = pgDiv(G_SW, 40);
    if (G_BALL_R < 4) { G_BALL_R = 4; }
}

function pgServe() {
    G_BALL_X = pgDiv(G_SW, 2);
    G_BALL_Y = pgDiv(G_SH, 2);
    if (G_SERVE_DIR == 0) { G_BVX = -5; G_SERVE_DIR = 1; }
    else { G_BVX = 5; G_SERVE_DIR = 0; }
    if (pgRnd(2) == 0) { G_BVY = -3; }
    else { G_BVY = 3; }
}

function pgNewGame() {
    G_LEFT_Y = pgDiv(G_SH, 2);
    G_RIGHT_Y = pgDiv(G_SH, 2);
    G_SCORE_L = 0;
    G_SCORE_R = 0;
    G_ENDED = 0;
    G_FRAMES = 0;
    G_SERVE_DIR = 0;
    pgServe();
}

function pgStep() {
    G_BALL_X = G_BALL_X + G_BVX;
    G_BALL_Y = G_BALL_Y + G_BVY;

    if (G_BALL_Y < G_BALL_R) { G_BALL_Y = G_BALL_R; G_BVY = 0 - G_BVY; }
    if (G_BALL_Y > G_SH - G_BALL_R) { G_BALL_Y = G_SH - G_BALL_R; G_BVY = 0 - G_BVY; }

    if (G_BVX < 0 && G_BALL_X <= 10 + G_PAD_W + G_BALL_R && G_BALL_X >= 6) {
        var pgTop = G_LEFT_Y - pgDiv(G_PAD_H, 2);
        if (G_BALL_Y >= pgTop && G_BALL_Y <= pgTop + G_PAD_H) {
            G_BALL_X = 10 + G_PAD_W + G_BALL_R;
            G_BVX = 0 - G_BVX - 1;
            if (G_BVX < -14) { G_BVX = -14; }
        }
    }

    if (G_BVX > 0 && G_BALL_X >= G_SW - 10 - G_PAD_W - G_BALL_R && G_BALL_X <= G_SW - 6) {
        var pgTop2 = G_RIGHT_Y - pgDiv(G_PAD_H, 2);
        if (G_BALL_Y >= pgTop2 && G_BALL_Y <= pgTop2 + G_PAD_H) {
            G_BALL_X = G_SW - 10 - G_PAD_W - G_BALL_R;
            G_BVX = 0 - G_BVX + 1;
            if (G_BVX > 14) { G_BVX = 14; }
        }
    }

    if (G_BALL_X < 0 - G_BALL_R) {
        G_SCORE_R = G_SCORE_R + 1;
        if (G_SCORE_R >= WIN_SCORE) { G_ENDED = 1; }
        pgServe();
    }
    if (G_BALL_X > G_SW + G_BALL_R) {
        G_SCORE_L = G_SCORE_L + 1;
        if (G_SCORE_L >= WIN_SCORE) { G_ENDED = 1; }
        pgServe();
    }

    if (G_RIGHT_Y < G_BALL_Y) { G_RIGHT_Y = G_RIGHT_Y + 4; }
    if (G_RIGHT_Y > G_BALL_Y) { G_RIGHT_Y = G_RIGHT_Y - 4; }

    if (G_LEFT_Y < pgDiv(G_PAD_H, 2)) { G_LEFT_Y = pgDiv(G_PAD_H, 2); }
    if (G_LEFT_Y > G_SH - pgDiv(G_PAD_H, 2)) { G_LEFT_Y = G_SH - pgDiv(G_PAD_H, 2); }
    if (G_RIGHT_Y < pgDiv(G_PAD_H, 2)) { G_RIGHT_Y = pgDiv(G_PAD_H, 2); }
    if (G_RIGHT_Y > G_SH - pgDiv(G_PAD_H, 2)) { G_RIGHT_Y = G_SH - pgDiv(G_PAD_H, 2); }
}

function pgDraw() {
    ui_2d_clear(C_BG);

    var pgY = 10;
    while (pgY + 16 <= G_SH - 10) {
        ui_2d_rect(pgDiv(G_SW, 2) - 2, pgY, 4, 16, C_LINE, 1, 0, 0);
        pgY = pgY + 32;
    }

    ui_2d_rect(10, G_LEFT_Y - pgDiv(G_PAD_H, 2), G_PAD_W, G_PAD_H, C_LEFT, 1, 0, 4);
    ui_2d_rect(G_SW - 10 - G_PAD_W, G_RIGHT_Y - pgDiv(G_PAD_H, 2), G_PAD_W, G_PAD_H, C_RIGHT, 1, 0, 4);
    ui_2d_circle(G_BALL_X, G_BALL_Y, G_BALL_R, C_BALL, 1, 0);

    ui_2d_text(pgDiv(G_SW, 2) - 70, 28, pgDigitStr(G_SCORE_L), C_TEXT, 26, 1);
    ui_2d_text(pgDiv(G_SW, 2) + 70, 28, pgDigitStr(G_SCORE_R), C_TEXT, 26, 1);

    if (G_ENDED != 0) {
        if (G_LANG == 0) {
            if (G_SCORE_L >= WIN_SCORE) { ui_2d_text(pgDiv(G_SW, 2), pgDiv(G_SH, 2) - 40, "你赢了!点一下再来", C_LEFT, 20, 1); }
            else { ui_2d_text(pgDiv(G_SW, 2), pgDiv(G_SH, 2) - 40, "电脑赢了,点一下再来", -2069424, 20, 1); }
        } else {
            if (G_SCORE_L >= WIN_SCORE) { ui_2d_text(pgDiv(G_SW, 2), pgDiv(G_SH, 2) - 40, "You win! Tap to retry", C_LEFT, 20, 1); }
            else { ui_2d_text(pgDiv(G_SW, 2), pgDiv(G_SH, 2) - 40, "AI wins - tap to retry", -2069424, 20, 1); }
        }
    } else {
        if (G_LANG == 0) { ui_2d_text(pgDiv(G_SW, 2), 58, "左半屏滑动控制球板,先到 5 分", C_DIM, 11, 1); }
        else { ui_2d_text(pgDiv(G_SW, 2), 58, "Drag on the left half; first to 5", C_DIM, 11, 1); }
    }
    ui_2d_present();
}

function main() {
    G_LANG = ui_scr_get_language();
    if (G_LANG == 0) { ui_win_open("对打", 0, 0); }
    else { ui_win_open("Pong", 0, 0); }
    ui_scr_set_keep_on(1);

    pgLayout();
    G_SEED = 20262;
    pgNewGame();
    pgDraw();

    var pgTid = ui_timer_set(33, 1);
    var pgDone = 0;
    while (pgDone == 0 && ui_win_is_closed() == 0) {
        var pgT = ui_msg_fetch_wait(33);
        if (pgT == MSG_CLOSE) { break; }

        if (pgT == MSG_TIMER) {
            G_FRAMES = G_FRAMES + 1;
            if (G_FRAMES >= MAX_FRAMES) { pgDone = 1; }
            if (G_ENDED == 0) { pgStep(); }
            pgDraw();
            continue;
        }

        if (pgT == MSG_TOUCHDOWN || pgT == MSG_MOUSEDOWN) {
            if (G_ENDED != 0) { pgNewGame(); }
            else { G_LEFT_Y = ui_msg_get_b(); }
            pgDraw();
            continue;
        }

        if (pgT == 7) {
            if (G_ENDED == 0) { G_LEFT_Y = ui_msg_get_b(); }
            pgDraw();
            continue;
        }

        if (pgT == MSG_RESIZE) { pgLayout(); pgDraw(); continue; }

        if (pgT == MSG_KEYDOWN) {
            var pgK = ui_msg_get_a();
            if (pgK == KEY_ESC) { pgDone = 1; }
            if (pgK == 13 || pgK == 82) { pgNewGame(); pgDraw(); }
            continue;
        }
    }

    ui_timer_kill(pgTid);
    ui_scr_set_keep_on(0);
    ui_win_close();
}

main();