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

rust/app/memory.rs · 11418 字节 · 下载原文

▶ 在浏览器里试玩

// 记忆翻牌 —— 用 **Rust** 写的手机小游戏
// Memory matching — a small phone game written in **Rust**
//
// 玩法(与 `Examples/c/app/memory.c` / `Examples/python/app/memory.py` 同一套):
// Gameplay (same rules as the C / Python versions):
//   · 4×4 共 8 对;点一张翻开,再点第二张;
//   · 4x4, eight pairs; tap one card to flip it, then tap a second;
//   · 一样就留在明面,不一样 **0.4 秒**后扣回去;全部配对完成即通关;
//   · an equal pair stays revealed, a different pair flips back after **0.4 s**; clear all pairs to win;
//   · 通关后点屏幕(或按 ESC / R)重开。
//   · after a win, tap the screen (or press ESC / R) for a new game.
//
// ## 这一份演示「延时动作」怎么写
// ## This sample shows **how to write a delayed action**
//
// 翻错的两张不能立刻扣回去(玩家会看不到第二张是什么),所以记一个**倒计时拍数**,
// 主循环每拍减一,减到 0 才扣 —— 不另起线程、不睡眠。
// A wrong pair must not flip back immediately (the player would never see the second card), so we
// keep a countdown in **ticks** and decrement it each frame -- no extra thread, no sleep.
//
// ## Rust 这一路的写法约束(见 `Examples/rust/app/snake.rs` 文件头)
// ## Style constraints on this Rust path (see the header of `Examples/rust/app/snake.rs`)
//
//   · **全部状态放 `main` 的局部变量**,循环一律 `while`(`for` 不作为默认写法);
//   · **all state lives in locals of `main`**, loops are always `while`;
//   · 数字转字符串走库里的 `int_to_str`(**返回值指向同一块静态缓冲**,每次取完立刻用掉);
//   · numbers go through the library's `int_to_str` (**the result points at one shared static
//     buffer**, so take it and use it immediately);
//   · 颜色写负数十进制。
//   · colours are negative decimals.
//
// 跑法 —— 手机:`vml run examples/rust/memory.rs`
// How to run — on the phone: `vml run examples/rust/memory.rs`
// 跑法 —— 桌面:`vmlcli Examples/rust/memory.rs --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/rust/memory.rs --screen 480x640 --frames /tmp/fr`

#include <vml_ui.rs>
use vml_ui::*;

fn main() {
    let mut lang = ui_scr_get_language();
    let mut sw = 360;
    let mut sh = 620;
    let mut cell = 80;
    let mut ox = 20;
    let mut oy = 110;

    let c_bg = -15724520;        // 0xFF101018
    let c_back = -14013898;      // 0xFF2A2A36
    let c_matched = -15329762;   // 0xFF16161E
    let c_text = -1513232;       // 0xFFE8E8F0
    let c_dim = -6643536;        // 0xFF9AA0B0
    let c_face = -15724520;      // 0xFF101018
    // 8 对卡片的颜色(同一个下标 = 同一对)
    // The colours of the eight pairs (one index = one pair)
    let pair_color = [-2069424, -2509750, -11409298, -11890471,
                      -6591248, -2069336, -11871802, -4145088];

    // `pair_of[i]`:这张牌属于哪一对(0..7),-1 = 已配对;`up[i]`:是否翻开
    // `pair_of[i]`: which pair this card belongs to (0..7), -1 = matched; `up[i]`: face up?
    let mut pair_of = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
    let mut up = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
    let mut first_x = -1;
    let mut first_y = -1;
    let mut hide = 0;
    let mut moves = 0;
    let mut matched = 0;
    let mut won = 0;
    let mut seed = 20265;
    let mut frames = 0;
    let mut done = 0;
    // 循环变量在一次声明后复用(本前端不支持同名 `let` 的重复声明 / shadowing)
    // Loop variables are declared once and reused (this frontend does not support repeated `let` / shadowing)
    let mut x = 0;
    let mut y = 0;
    let mut i = 0;
    let mut j = 0;
    let mut t = 0;
    let mut tx = 0;
    let mut ty = 0;
    let mut px = 0;
    let mut py = 0;
    let mut p = 0;
    let mut side = 0;
    let mut face = "";
    let mut hud = "";

    lang = ui_scr_get_language();
    ui_win_open("记忆翻牌", 0, 0);

    sw = ui_scr_get_w();
    sh = ui_scr_get_h();
    if sw <= 0 { sw = 360; }
    if sh <= 0 { sh = 620; }
    side = sw - 20;
    if sw >= sh - 140 { side = sh - 170; }
    cell = side / 4;
    if cell < 6 { cell = 6; }
    ox = (sw - 4 * cell) / 2;
    oy = 110;

    // ── 开一局:先铺成 0,0,1,1,2,2…再 Fisher–Yates 洗牌(自带随机源,可复现)──
    // ── New game: lay out 0,0,1,1,2,2... then Fisher-Yates shuffle (own RNG, reproducible) ──
    x = 0;
    while x < 4 {
        y = 0;
        while y < 4 {
            pair_of[x * 4 + y] = (y * 4 + x) / 2;
            up[x * 4 + y] = 0;
            y = y + 1;
        }
        x = x + 1;
    }
    i = 15;
    while i > 0 {
        seed = (seed * 1103 + 12345) % 32768;
        j = (seed / 256) % (i + 1);
        t = pair_of[i];
        pair_of[i] = pair_of[j];
        pair_of[j] = t;
        i = i - 1;
    }

    let mut tid = ui_timer_set(50, 0);
    frames = 0;
    done = 0;

    while done == 0 && ui_win_is_closed() == 0 {
        let 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
            tx = ui_msg_get_a();
            ty = ui_msg_get_b();
            if won == 1 {
                seed = 20265;
                x = 0;
                while x < 4 {
                    y = 0;
                    while y < 4 {
                        pair_of[x * 4 + y] = (y * 4 + x) / 2;
                        up[x * 4 + y] = 0;
                        y = y + 1;
                    }
                    x = x + 1;
                }
                i = 15;
                while i > 0 {
                    seed = (seed * 1103 + 12345) % 32768;
                    j = (seed / 256) % (i + 1);
                    t = pair_of[i];
                    pair_of[i] = pair_of[j];
                    pair_of[j] = t;
                    i = i - 1;
                }
                first_x = -1;
                first_y = -1;
                hide = 0;
                moves = 0;
                matched = 0;
                won = 0;
            } else {
                x = (tx - ox) / cell;
                y = (ty - oy) / cell;
                if x >= 0 && y >= 0 && x < 4 && y < 4 {
                    i = x * 4 + y;
                    if pair_of[i] >= 0 && up[i] == 0 && hide == 0 {
                        up[i] = 1;
                        if first_x < 0 {
                            first_x = x;
                            first_y = y;
                        } else {
                            moves = moves + 1;
                            j = first_x * 4 + first_y;
                            if pair_of[j] == pair_of[i] {
                                pair_of[j] = -1;
                                pair_of[i] = -1;
                                matched = matched + 2;
                                if matched >= 16 { won = 1; }
                                first_x = -1;
                            } else {
                                hide = 8;        // 8 拍 × 50ms ≈ 0.4 秒
                            }
                        }
                    }
                }
            }
        }

        if t == 1 {                              // MSG_KEYDOWN
            let k = ui_msg_get_a();
            if k == 27 || k == 82 {
                seed = 20265;
                x = 0;
                while x < 4 {
                    y = 0;
                    while y < 4 {
                        pair_of[x * 4 + y] = (y * 4 + x) / 2;
                        up[x * 4 + y] = 0;
                        y = y + 1;
                    }
                    x = x + 1;
                }
                i = 15;
                while i > 0 {
                    seed = (seed * 1103 + 12345) % 32768;
                    j = (seed / 256) % (i + 1);
                    t = pair_of[i];
                    pair_of[i] = pair_of[j];
                    pair_of[j] = t;
                    i = i - 1;
                }
                first_x = -1;
                first_y = -1;
                hide = 0;
                moves = 0;
                matched = 0;
                won = 0;
            }
        }

        if t == 11 {                             // MSG_WINDOWRESIZE
            sw = ui_scr_get_w();
            sh = ui_scr_get_h();
            if sw <= 0 { sw = 360; }
            if sh <= 0 { sh = 620; }
            side = sw - 20;
            if sw >= sh - 140 { side = sh - 170; }
            cell = side / 4;
            if cell < 6 { cell = 6; }
            ox = (sw - 4 * cell) / 2;
            oy = 110;
        }

        if t == 9 {                              // MSG_TIMER
            frames = frames + 1;
            if frames >= 8000 { done = 1; }
            if hide > 0 {
                hide = hide - 1;
                if hide == 0 {
                    x = 0;
                    while x < 4 {
                        y = 0;
                        while y < 4 {
                            up[x * 4 + y] = 0;
                            y = y + 1;
                        }
                        x = x + 1;
                    }
                    first_x = -1;
                }
            }
        }

        // ── 绘制(每帧重画)──
        ui_2d_clear(c_bg);
        x = 0;
        while x < 4 {
            y = 0;
            while y < 4 {
                px = ox + x * cell;
                py = oy + y * cell;
                p = pair_of[x * 4 + y];
                if p < 0 {
                    ui_2d_rect(px + 3, py + 3, cell - 6, cell - 6, c_matched, 1, 0, 6);
                } else if up[x * 4 + y] == 1 {
                    ui_2d_rect(px + 3, py + 3, cell - 6, cell - 6, pair_color[p], 1, 0, 8);
                    // 牌面用数字;`int_to_str` 的缓冲是共享的 ⇒ 取一次立刻用掉
                    // The face is a number; `int_to_str`'s buffer is shared => take it and use it at once
                    face = int_to_str(p + 1);
                    ui_2d_text(px + cell / 2, py + cell / 2 + cell / 8,
                        face, c_face, cell / 2, VML_ANCHOR_CENTER);
                } else {
                    ui_2d_rect(px + 3, py + 3, cell - 6, cell - 6, c_back, 1, 0, 8);
                }
                y = y + 1;
            }
            x = x + 1;
        }

        hud = int_to_str(moves);
        ui_2d_text(10, 22, hud, c_text, 15, VML_ANCHOR_LEFT);
        if lang == 0 { ui_2d_text(10, 42, "步数", c_dim, 10, VML_ANCHOR_LEFT); }
        else { ui_2d_text(10, 42, "moves", c_dim, 10, VML_ANCHOR_LEFT); }

        if won == 1 {
            if lang == 0 { ui_2d_text(sw / 2, oy + 4 * cell + 28, "全配好了!点一下再来", c_text, 15, VML_ANCHOR_CENTER); }
            else { ui_2d_text(sw / 2, oy + 4 * cell + 28, "All matched! Tap for a new one", c_text, 15, VML_ANCHOR_CENTER); }
        } else {
            if lang == 0 { ui_2d_text(sw / 2, oy + 4 * cell + 28, "翻两张一样的", c_dim, 12, VML_ANCHOR_CENTER); }
            else { ui_2d_text(sw / 2, oy + 4 * cell + 28, "Flip two that match", c_dim, 12, VML_ANCHOR_CENTER); }
        }
        ui_2d_present();
    }

    ui_timer_kill(tid);
    ui_win_close();
}