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

typescript/app/pong.ts · 7271 字节 · 下载原文

▶ 在浏览器里试玩

// pong.ts —— 对打(VML 的 TypeScript 前端)
// pong.ts -- Pong (VML's TypeScript frontend)
//
// 与 `Examples/c/app/pong.c` / `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.ts` 文件头):
// ⚠ Two measured rules of this frontend (see the header of `memory.ts`):
//   · `/` 是浮点除法 ⇒ 全用 `~~(a / b)`;
//   · `/` is floating division => `~~(a / b)` everywhere;
//   · `VML_MSG_*` 常量不会被自动引入 ⇒ 就地声明成 `const`。
//   · `VML_MSG_*` constants are not auto-imported => declare them here as `const`.
//
// 跑法 —— 手机:`vml run examples/typescript/pong.ts`
// How to run — on the phone: `vml run examples/typescript/pong.ts`
// 跑法 —— 桌面:`vmlcli Examples/typescript/pong.ts --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/typescript/pong.ts --screen 480x640 --frames /tmp/fr`

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

const MAX_FRAMES: number = 6000;
const WIN_SCORE: number = 5;

const C_BG: number = 0xFF101018;
const C_LINE: number = 0xFF2A2A36;
const C_LEFT: number = 0xFF51E86E;
const C_RIGHT: number = 0xFF4A90D9;
const C_BALL: number = 0xFFE8E8F0;
const C_TEXT: number = 0xFFE8E8F0;
const C_DIM: number = 0xFF9AA0B0;

// S slots:
//   0 w 1 h 2 padW 3 padH 4 ballR 5 leftY 6 rightY 7 ballX 8 ballY
//   9 vx 10 vy 11 scoreL 12 scoreR 13 ended 14 frames 15 seed 16 serveDir 17 lang 18 touchY
let S: number[] = [360, 620, 12, 70, 6, 310, 310, 180, 310, -5, 3, 0, 0, 0, 0, 20262, 0, 0, 0];

function pgRnd(n: number): number {
    S[15] = (S[15] * 1103 + 12345) % 32768;
    return ~~(S[15] / 256) % n;
}

function pgLayout(): void {
    S[0] = ui_scr_get_w();
    S[1] = ui_scr_get_h();
    if (S[0] <= 0) { S[0] = 360; }
    if (S[1] <= 0) { S[1] = 620; }
    S[2] = ~~(S[0] / 28);
    if (S[2] < 8) { S[2] = 8; }
    S[3] = ~~(S[1] / 6);
    if (S[3] < 40) { S[3] = 40; }
    S[4] = ~~(S[0] / 40);
    if (S[4] < 4) { S[4] = 4; }
}

function pgServe(): void {
    S[7] = ~~(S[0] / 2);
    S[8] = ~~(S[1] / 2);
    if (S[16] === 0) { S[9] = -5; S[16] = 1; }
    else { S[9] = 5; S[16] = 0; }
    if (pgRnd(2) === 0) { S[10] = -3; }
    else { S[10] = 3; }
}

function pgNewGame(): void {
    S[5] = ~~(S[1] / 2);
    S[6] = ~~(S[1] / 2);
    S[11] = 0;
    S[12] = 0;
    S[13] = 0;
    S[14] = 0;
    S[16] = 0;
    pgServe();
}

function pgStep(): void {
    S[7] = S[7] + S[9];
    S[8] = S[8] + S[10];

    if (S[8] < S[4]) { S[8] = S[4]; S[10] = 0 - S[10]; }
    if (S[8] > S[1] - S[4]) { S[8] = S[1] - S[4]; S[10] = 0 - S[10]; }

    if (S[9] < 0 && S[7] <= 10 + S[2] + S[4] && S[7] >= 6) {
        let top: number = S[5] - ~~(S[3] / 2);
        if (S[8] >= top && S[8] <= top + S[3]) {
            S[7] = 10 + S[2] + S[4];
            S[9] = 0 - S[9] - 1;
            if (S[9] < -14) { S[9] = -14; }
        }
    }

    if (S[9] > 0 && S[7] >= S[0] - 10 - S[2] - S[4] && S[7] <= S[0] - 6) {
        let top2: number = S[6] - ~~(S[3] / 2);
        if (S[8] >= top2 && S[8] <= top2 + S[3]) {
            S[7] = S[0] - 10 - S[2] - S[4];
            S[9] = 0 - S[9] + 1;
            if (S[9] > 14) { S[9] = 14; }
        }
    }

    if (S[7] < 0 - S[4]) {
        S[12] = S[12] + 1;
        if (S[12] >= WIN_SCORE) { S[13] = 1; }
        pgServe();
    }
    if (S[7] > S[0] + S[4]) {
        S[11] = S[11] + 1;
        if (S[11] >= WIN_SCORE) { S[13] = 1; }
        pgServe();
    }

    if (S[6] < S[8]) { S[6] = S[6] + 4; }
    if (S[6] > S[8]) { S[6] = S[6] - 4; }

    if (S[5] < ~~(S[3] / 2)) { S[5] = ~~(S[3] / 2); }
    if (S[5] > S[1] - ~~(S[3] / 2)) { S[5] = S[1] - ~~(S[3] / 2); }
    if (S[6] < ~~(S[3] / 2)) { S[6] = ~~(S[3] / 2); }
    if (S[6] > S[1] - ~~(S[3] / 2)) { S[6] = S[1] - ~~(S[3] / 2); }
}

function pgDraw(): void {
    ui_2d_clear(C_BG);

    let y: number = 10;
    while (y + 16 <= S[1] - 10) {
        ui_2d_rect(~~(S[0] / 2) - 2, y, 4, 16, C_LINE, 1, 0, 0);
        y = y + 32;
    }

    ui_2d_rect(10, S[5] - ~~(S[3] / 2), S[2], S[3], C_LEFT, 1, 0, 4);
    ui_2d_rect(S[0] - 10 - S[2], S[6] - ~~(S[3] / 2), S[2], S[3], C_RIGHT, 1, 0, 4);
    ui_2d_circle(S[7], S[8], S[4], C_BALL, 1, 0);

    ui_2d_text(~~(S[0] / 2) - 70, 28, "" + S[11], C_TEXT, 26, 1);
    ui_2d_text(~~(S[0] / 2) + 70, 28, "" + S[12], C_TEXT, 26, 1);

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

function main(): void {
    S[17] = ui_scr_get_language();
    if (S[17] === 0) { ui_win_open("对打", 0, 0); }
    else { ui_win_open("Pong", 0, 0); }
    ui_scr_set_keep_on(1);

    pgLayout();
    pgNewGame();
    pgDraw();

    let tid: number = ui_timer_set(33, 1);
    let done: number = 0;
    while (done === 0 && ui_win_is_closed() === 0) {
        let t: number = ui_msg_fetch_wait(33);
        if (t === MSG_CLOSE) { break; }

        if (t === MSG_TIMER) {
            S[14] = S[14] + 1;
            if (S[14] >= MAX_FRAMES) { done = 1; }
            if (S[13] === 0) { pgStep(); }
            pgDraw();
            continue;
        }

        if (t === MSG_TOUCHDOWN || t === MSG_MOUSEDOWN) {
            S[18] = ui_msg_get_b();
            if (S[13] !== 0) { pgNewGame(); }
            else { S[5] = S[18]; }
            pgDraw();
            continue;
        }

        if (t === 7) {
            S[18] = ui_msg_get_b();
            if (S[13] === 0) { S[5] = S[18]; }
            pgDraw();
            continue;
        }

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

        if (t === MSG_KEYDOWN) {
            let k: number = ui_msg_get_a();
            if (k === KEY_ESC) { done = 1; }
            if (k === 13 || k === 82) { pgNewGame(); pgDraw(); }
            continue;
        }
    }

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

main();