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

objc/app/whack.m · 8344 字节 · 下载原文

▶ 在浏览器里试玩

// 打地鼠 —— 用 **ObjC** 写的手机小游戏
// Whack-a-mole — a small phone game written in **ObjC**
//
// 玩法(与 `Examples/c/app/whack.c` / `Examples/python/app/whack.py` 同一套):
// Gameplay (same rules as the C / Python versions):
//   · 3×3 九个洞,每 SHOW_TICKS 拍有一个洞冒出地鼠;
//   · nine holes in a 3x3; every SHOW_TICKS ticks one hole pops a mole;
//   · 在它缩回去之前点中 +1 分,超时没点中扣一条命;三条命用完结束;
//   · tapping it before it ducks back scores +1; missing it costs a life; three lives ends the run;
//   · 结束后点屏幕 / ESC 重开。
//   · after the run ends, tap the screen / press ESC for a new one.
//
// ## "计时归谁管"是这一类游戏的关键
// ## **Who owns the clock** is the key thing in this genre
//
// 地鼠的存活时间记在**拍数**上(`moleTicks`),不是 `time()` —— 定时器就是心跳,
// "一只地鼠活多少拍"可计算、不随机器快慢漂移。
// A mole's lifetime is counted in **ticks** (`moleTicks`), not `time()` -- the timer is the
// heartbeat, so "how many ticks a mole stays" is computable and does not drift with the machine.
//
// ## 写法约束(见 `Examples/objc/app/snake.m` 文件头)
// ## Style constraints (see the header of `Examples/objc/app/snake.m`)
//
//   · 循环更新写 `i = i + 1`;颜色写**负数十进制**;
//   · loop updates are `i = i + 1`; colours are **negative decimals**;
//   · 状态放**一个**文件级数组 `A[]`,辅助函数一律无参(tap 的实参走 A[15]/A[16])。
//   · state lives in **one** file-level array `A[]`, and helper functions take no arguments
//     (tap arguments come in through A[15]/A[16]).
//
// 跑法 —— 手机:`vml run examples/objc/whack.m`
// How to run — on the phone: `vml run examples/objc/whack.m`
// 跑法 —— 桌面:`vmlcli Examples/objc/whack.m --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/objc/whack.m --screen 480x640 --frames /tmp/fr`

#include <vml_ui.h>
#import <vml_ui.h>

// 状态表(一个数组放全部):
//   0=moleAt 1=moleTicks 2=waitTicks 3=score 4=lives 5=over 6=hitFlash 7=seed
//   8=LANG 9=SW 10=SH 11=CELL 12=OX 13=OY 14=tid 15=tapX 16=tapY
int A[17];

#define MAX_FRAMES 6000
#define GRID 3
#define SHOW_TICKS 14          // 一只地鼠活 14 拍 ≈ 0.7 秒

#define C_BG      -15724520    // 0xFF101018
#define C_HOLE    -15066588    // 0xFF1A1A24
#define C_MOLE    -2509750     // 0xFFD9B44A
#define C_TEXT     -1513232    // 0xFFE8E8F0
#define C_DIM      -6643536    // 0xFF9AA0B0
#define C_MISS     -2069424    // 0xFFE06C50
#define C_HIT             -1   // 0xFFFFFFFF

// 整数 → 字符串(不引 std::string;`sprintf` 在各前端形态不一)
// Integer to string (no std::string; `sprintf` differs across front ends)
char g_num[12];

char* numstr(int v) {
    int n;
    int m;
    char tmp[12];
    n = 0;
    m = 0;
    if (v < 0) { g_num[0] = '-'; v = 0 - v; n = 1; }
    if (v == 0) { g_num[n] = '0'; g_num[n + 1] = 0; return g_num; }
    while (v > 0) { tmp[m] = (char)('0' + v % 10); v = v / 10; m = m + 1; }
    while (m > 0) { m = m - 1; g_num[n] = tmp[m]; n = n + 1; }
    g_num[n] = 0;
    return g_num;
}

// 确定性伪随机(验收要能复现同一局)
// Deterministic PRNG (acceptance needs to reproduce the same run)
int rnd(int n) {
    A[7] = (A[7] * 1103 + 12345) % 32768;
    return (A[7] / 256) % n;
}

void relayout() {
    A[9] = ui_scr_get_w();
    A[10] = ui_scr_get_h();
    if (A[9] <= 0) { A[9] = 360; }
    if (A[10] <= 0) { A[10] = 620; }
    A[11] = A[9] - 24;
    if (A[9] >= A[10] - 150) { A[11] = A[10] - 180; }
    A[11] = A[11] / GRID;
    if (A[11] < 8) { A[11] = 8; }
    A[12] = (A[9] - GRID * A[11]) / 2;
    A[13] = 130;
}

void pickHole() {
    A[0] = rnd(GRID * GRID);
    A[1] = SHOW_TICKS;
}

void newGame() {
    A[3] = 0;
    A[4] = 3;
    A[5] = 0;
    A[6] = 0;
    A[0] = -1;
    A[2] = 6;
}

void draw() {
    int i;
    int cx;
    int cy;
    int r;
    int mc;
    ui_2d_clear(C_BG);

    i = 0;
    while (i < GRID * GRID) {
        cx = A[12] + (i % GRID) * A[11] + A[11] / 2;
        cy = A[13] + (i / GRID) * A[11] + A[11] / 2;
        r = A[11] / 3;
        ui_2d_ellipse(cx, cy + r / 3, r + A[11] / 12, r / 2 + A[11] / 20, C_HOLE, 1, 0);
        if (i == A[0]) {
            mc = C_MOLE;
            if (A[6] == 1) { mc = C_HIT; }
            ui_2d_circle(cx, cy, r, mc, 1, 0);
            ui_2d_circle(cx - r / 3, cy - r / 4, r / 5, C_BG, 1, 0);
            ui_2d_circle(cx + r / 3, cy - r / 4, r / 5, C_BG, 1, 0);
        }
        i = i + 1;
    }

    ui_2d_text(10, 24, numstr(A[3]), C_TEXT, 18, VML_ANCHOR_LEFT);
    if (A[8] == 0) { ui_2d_text(10, 48, "得分", C_DIM, 10, VML_ANCHOR_LEFT); }
    else { ui_2d_text(10, 48, "score", C_DIM, 10, VML_ANCHOR_LEFT); }
    ui_2d_text(A[9] - 10, 24, numstr(A[4]), C_MISS, 18, VML_ANCHOR_RIGHT);
    if (A[8] == 0) { ui_2d_text(A[9] - 10, 48, "命", C_DIM, 10, VML_ANCHOR_RIGHT); }
    else { ui_2d_text(A[9] - 10, 48, "lives", C_DIM, 10, VML_ANCHOR_RIGHT); }

    if (A[5] == 1) {
        if (A[8] == 0) { ui_2d_text(A[9] / 2, A[13] + GRID * A[11] + 30, "结束,点一下再来", C_TEXT, 16, VML_ANCHOR_CENTER); }
        else { ui_2d_text(A[9] / 2, A[13] + GRID * A[11] + 30, "Over - tap to retry", C_TEXT, 16, VML_ANCHOR_CENTER); }
    } else {
        if (A[8] == 0) { ui_2d_text(A[9] / 2, A[13] + GRID * A[11] + 30, "冒头就点它", C_DIM, 12, VML_ANCHOR_CENTER); }
        else { ui_2d_text(A[9] / 2, A[13] + GRID * A[11] + 30, "Tap the mole when it pops", C_DIM, 12, VML_ANCHOR_CENTER); }
    }

    ui_2d_present();
}

// tap 的实参走 A[15]/A[16](辅助函数无参)
// tap arguments come in through A[15]/A[16] (helpers take no arguments)
void tap() {
    int x;
    int y;
    int idx;
    if (A[5] == 1) {
        newGame();
        return;
    }
    x = (A[15] - A[12]) / A[11];
    y = (A[16] - A[13]) / A[11];
    if (x < 0 || y < 0 || x >= GRID || y >= GRID) { return; }
    idx = y * GRID + x;
    if (idx == A[0]) {
        A[3] = A[3] + 1;
        A[0] = -1;
        A[2] = 4 + rnd(4);
        A[6] = 1;
    }
}

int main() {
    int t;
    int k;
    int frames;
    int done;

    A[8] = ui_scr_get_language();

    relayout();
    if (A[8] == 0) { ui_win_open("打地鼠", 0, 0); }
    else { ui_win_open("Whack", 0, 0); }
    relayout();
    A[7] = 20267;                // 与其它语言版同一个种子 / same seed as the other language versions
    newGame();
    draw();

    A[14] = ui_timer_set(50, 0);
    frames = 0;
    done = 0;
    while (done == 0 && ui_win_is_closed() == 0) {
        t = ui_msg_fetch_wait(0);
        if (t == 0) { continue; }
        if (t == 10) { break; }                  // MSG_WINDOWCLOSE

        if (t == 6 || t == 4) {                  // MSG_TOUCHDOWN / MSG_MOUSEDOWN
            A[15] = ui_msg_get_a();
            A[16] = ui_msg_get_b();
            tap();
            draw();
            continue;
        }

        if (t == 1) {                            // MSG_KEYDOWN
            k = ui_msg_get_a();
            if (k == 27 || k == 82) { newGame(); draw(); }
            continue;
        }

        if (t == 11) { relayout(); draw(); continue; }   // MSG_WINDOWRESIZE
        if (t == 12) { continue; }                       // MSG_WINDOWORIENT

        if (t == 9) {                            // MSG_TIMER
            frames = frames + 1;
            if (frames >= MAX_FRAMES) { done = 1; }
            if (A[5] == 0) {
                A[6] = 0;
                if (A[0] >= 0) {
                    A[1] = A[1] - 1;
                    if (A[1] <= 0) {
                        // 没点中:扣命 / missed: lose a life
                        A[0] = -1;
                        A[4] = A[4] - 1;
                        A[2] = 8;
                        if (A[4] <= 0) {
                            A[5] = 1;
                            done = 1;
                        }
                    }
                } else {
                    A[2] = A[2] - 1;
                    if (A[2] <= 0) { pickHole(); }
                }
            }
            draw();
            continue;
        }
    }

    ui_timer_kill(A[14]);
    ui_win_close();
    return 0;
}