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

cpp/app/whack.cpp · 8315 字节 · 下载原文

▶ 在浏览器里试玩

// 打地鼠 —— 用 **C++** 写的手机小游戏
// Whack-a-mole — a small phone game written in **C++**
//
// 玩法(与 `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/cpp/app/snake.cpp` 文件头)
// ## Style constraints (same as the header of `Examples/cpp/app/snake.cpp`)
//
//   · 颜色写**负数十进制**(本前端不认 `0x`);数组维度只写**字面量宏**(`(N * N)` 这种表达式宏实测越界);
//   · colours are **negative decimals** (no `0x` here); array dimensions use **literal macros only**
//     (an expression macro like `(N * N)` was measured to overrun);
//   · 整数转字符串走本文件的 `numstr()`;
//   · integers to strings go through `numstr()` here;
//   · 循环更新写 `i = i + 1`。
//   · loop updates are written `i = i + 1`.
//
// 跑法 —— 手机:`vml run examples/cpp/whack.cpp`
// How to run — on the phone: `vml run examples/cpp/whack.cpp`
// 跑法 —— 桌面:`vmlcli Examples/cpp/whack.cpp --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/cpp/whack.cpp --screen 480x640 --frames /tmp/fr`

#include <vml_ui.h>

#define MAX_FRAMES 6000
#define GRID 3
#define SHOW_TICKS 14              // 一只地鼠活 14 拍 ≈ 0.7 秒 / a mole lives 14 ticks ~= 0.7 s

#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

static int LANG = 0;
static int SW = 360;
static int SH = 620;
static int CELL = 120;
static int OX = 0;
static int OY = 130;

static int moleAt = -1;
static int moleTicks = 0;
static int waitTicks = 6;
static int score = 0;
static int lives = 3;
static int over = 0;
static int hitFlash = 0;
static int seed = 20267;
static int maxPrims = 0;

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

char* numstr(int v)
{
    int n = 0;
    int m = 0;
    char tmp[12];
    if (v < 0) { g_num[0] = '-'; v = -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)
{
    seed = (seed * 1103 + 12345) % 32768;
    return (seed / 256) % n;
}

void relayout()
{
    SW = ui_scr_get_w();
    SH = ui_scr_get_h();
    if (SW <= 0) { SW = 360; }
    if (SH <= 0) { SH = 620; }
    int side = SW - 24;
    if (SW >= SH - 150) { side = SH - 180; }
    CELL = side / GRID;
    if (CELL < 8) { CELL = 8; }
    OX = (SW - GRID * CELL) / 2;
    OY = 130;
}

void pickHole()
{
    moleAt = rnd(GRID * GRID);
    moleTicks = SHOW_TICKS;
}

void newGame()
{
    score = 0;
    lives = 3;
    over = 0;
    hitFlash = 0;
    moleAt = -1;
    waitTicks = 6;
}

void draw()
{
    ui_2d_clear(C_BG);

    int i = 0;
    while (i < GRID * GRID) {
        int cx = OX + (i % GRID) * CELL + CELL / 2;
        int cy = OY + (i / GRID) * CELL + CELL / 2;
        int r = CELL / 3;
        ui_2d_ellipse(cx, cy + r / 3, r + CELL / 12, r / 2 + CELL / 20, C_HOLE, 1, 0);
        if (i == moleAt) {
            int mc = C_MOLE;
            if (hitFlash == 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(score), C_TEXT, 18, VML_ANCHOR_LEFT);
    if (LANG == 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(SW - 10, 24, numstr(lives), C_MISS, 18, VML_ANCHOR_RIGHT);
    if (LANG == 0) { ui_2d_text(SW - 10, 48, "命", C_DIM, 10, VML_ANCHOR_RIGHT); }
    else { ui_2d_text(SW - 10, 48, "lives", C_DIM, 10, VML_ANCHOR_RIGHT); }

    if (over == 1) {
        if (LANG == 0) { ui_2d_text(SW / 2, OY + GRID * CELL + 30, "结束,点一下再来", C_TEXT, 16, VML_ANCHOR_CENTER); }
        else { ui_2d_text(SW / 2, OY + GRID * CELL + 30, "Over - tap to retry", C_TEXT, 16, VML_ANCHOR_CENTER); }
    } else {
        if (LANG == 0) { ui_2d_text(SW / 2, OY + GRID * CELL + 30, "冒头就点它", C_DIM, 12, VML_ANCHOR_CENTER); }
        else { ui_2d_text(SW / 2, OY + GRID * CELL + 30, "Tap the mole when it pops", C_DIM, 12, VML_ANCHOR_CENTER); }
    }

    int n = ui_res_get_count(0);
    if (n > maxPrims) { maxPrims = n; }
    ui_2d_present();
}

void tap(int tx, int ty)
{
    if (over == 1) {
        newGame();
        return;
    }
    int x = (tx - OX) / CELL;
    int y = (ty - OY) / CELL;
    if (x < 0 || y < 0 || x >= GRID || y >= GRID) { return; }
    int idx = y * GRID + x;
    if (idx == moleAt) {
        score = score + 1;
        moleAt = -1;
        waitTicks = 4 + rnd(4);
        hitFlash = 1;
    }
}

int main()
{
    LANG = ui_scr_get_language();

    relayout();
    if (LANG == 0) { ui_win_open("打地鼠", 0, 0); }
    else { ui_win_open("Whack", 0, 0); }
    relayout();
    newGame();
    draw();

    int tid = ui_timer_set(50, 0);
    int frames = 0;
    int done = 0;
    while (done == 0 && ui_win_is_closed() == 0) {
        int t = ui_msg_fetch_wait(0);
        if (t == 10) { break; }                  // MSG_WINDOWCLOSE

        if (t == 6 || t == 4) {                  // MSG_TOUCHDOWN / MSG_MOUSEDOWN
            tap(ui_msg_get_a(), ui_msg_get_b());
            draw();
            continue;
        }

        if (t == 1) {                            // MSG_KEYDOWN
            int 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 (over == 0) {
                hitFlash = 0;
                if (moleAt >= 0) {
                    moleTicks = moleTicks - 1;
                    if (moleTicks <= 0) {
                        // 没点中:扣命 / missed: lose a life
                        moleAt = -1;
                        lives = lives - 1;
                        waitTicks = 8;
                        if (lives <= 0) {
                            over = 1;
                            done = 1;
                        }
                    }
                } else {
                    waitTicks = waitTicks - 1;
                    if (waitTicks <= 0) { pickHole(); }
                }
            }
            draw();
            continue;
        }
    }

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