rust/app/whack.rs · 8740 字节 · 下载原文
// 打地鼠 —— 用 **Rust** 写的手机小游戏
// Whack-a-mole — a small phone game written in **Rust**
//
// 玩法(与 `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
//
// 地鼠的存活时间记在**拍数**上(`mole_ticks`),不是 `time()` —— 定时器就是心跳,
// "一只地鼠活多少拍"可计算、不随机器快慢漂移。
// A mole's lifetime is counted in **ticks** (`mole_ticks`), not `time()` -- the timer is the
// heartbeat, so "how many ticks a mole stays" is computable and does not drift with the machine.
//
// ## Rust 这一路的写法约束(见 `Examples/rust/app/snake.rs` 文件头)
// ## Style constraints on this Rust path (see the header of `Examples/rust/app/snake.rs`)
//
// · `#include <vml_ui.rs>` + `use vml_ui::*;` 两行都要;
// · both `#include <vml_ui.rs>` and `use vml_ui::*;` are required;
// · **循环变量在 `main` 顶部声明一次**(本前端不支持同名 `let` 的重复声明);
// · **loop variables are declared once at the top of `main`** (this frontend does not support
// repeated `let` / shadowing);
// · 数字用 `int_to_str`(**共享静态缓冲**,取一次立刻用掉);
// · numbers use `int_to_str` (**one shared static buffer**, take and use it at once).
//
// 跑法 —— 手机:`vml run examples/rust/whack.rs`
// How to run — on the phone: `vml run examples/rust/whack.rs`
// 跑法 —— 桌面:`vmlcli Examples/rust/whack.rs --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/rust/whack.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 = 120;
let mut ox = 0;
let mut oy = 130;
let c_bg = -15724520; // 0xFF101018
let c_hole = -15066588; // 0xFF1A1A24
let c_mole = -2509750; // 0xFFD9B44A
let c_text = -1513232; // 0xFFE8E8F0
let c_dim = -6643536; // 0xFF9AA0B0
let c_miss = -2069424; // 0xFFE06C50
let c_hit = -1; // 0xFFFFFFFF
let mut mole_at = -1;
let mut mole_ticks = 0;
let mut wait_ticks = 6;
let mut score = 0;
let mut lives = 3;
let mut over = 0;
let mut hit_flash = 0;
let mut seed = 20267;
let mut frames = 0;
let mut done = 0;
// 循环变量一次声明、之后复用
// Loop variables are declared once and reused afterwards
let mut i = 0;
let mut x = 0;
let mut y = 0;
let mut cx = 0;
let mut cy = 0;
let mut r = 0;
let mut idx = 0;
let mut side = 0;
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 - 24;
if sw >= sh - 150 { side = sh - 180; }
cell = side / 3;
if cell < 8 { cell = 8; }
ox = (sw - 3 * cell) / 2;
oy = 130;
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
x = ui_msg_get_a();
y = ui_msg_get_b();
if over == 1 {
score = 0;
lives = 3;
over = 0;
hit_flash = 0;
mole_at = -1;
wait_ticks = 6;
} else {
x = (x - ox) / cell;
y = (y - oy) / cell;
if x >= 0 && y >= 0 && x < 3 && y < 3 {
idx = y * 3 + x;
if idx == mole_at {
score = score + 1;
mole_at = -1;
seed = (seed * 1103 + 12345) % 32768;
wait_ticks = 4 + (seed / 256) % 4;
hit_flash = 1;
}
}
}
}
if t == 1 { // MSG_KEYDOWN
let k = ui_msg_get_a();
if k == 27 || k == 82 {
score = 0;
lives = 3;
over = 0;
hit_flash = 0;
mole_at = -1;
wait_ticks = 6;
}
}
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 - 24;
if sw >= sh - 150 { side = sh - 180; }
cell = side / 3;
if cell < 8 { cell = 8; }
ox = (sw - 3 * cell) / 2;
oy = 130;
}
if t == 9 { // MSG_TIMER
frames = frames + 1;
if frames >= 6000 { done = 1; }
if over == 0 {
hit_flash = 0;
if mole_at >= 0 {
mole_ticks = mole_ticks - 1;
if mole_ticks <= 0 {
// 没点中:扣命 / missed: lose a life
mole_at = -1;
lives = lives - 1;
wait_ticks = 8;
if lives <= 0 {
over = 1;
done = 1;
}
}
} else {
wait_ticks = wait_ticks - 1;
if wait_ticks <= 0 {
seed = (seed * 1103 + 12345) % 32768;
mole_at = (seed / 256) % 9;
mole_ticks = 14;
}
}
}
}
// ── 绘制(每帧重画)──
ui_2d_clear(c_bg);
i = 0;
while i < 9 {
cx = ox + (i % 3) * cell + cell / 2;
cy = oy + (i / 3) * cell + cell / 2;
r = cell / 3;
ui_2d_ellipse(cx, cy + r / 3, r + cell / 12, r / 2 + cell / 20, c_hole, 1, 0);
if i == mole_at {
// 颜色用 if/else 各传常量(本前端对循环内被赋值的变量实参不可靠)
// The colour is passed as a constant from each branch (variables assigned in the
// loop are unreliable as arguments on this frontend)
if hit_flash == 1 {
ui_2d_circle(cx, cy, r, c_hit, 1, 0);
} else {
ui_2d_circle(cx, cy, r, c_mole, 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;
}
hud = int_to_str(score);
ui_2d_text(10, 24, hud, 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); }
hud = int_to_str(lives);
ui_2d_text(sw - 10, 24, hud, 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 + 3 * cell + 30, "结束,点一下再来", c_text, 16, VML_ANCHOR_CENTER); }
else { ui_2d_text(sw / 2, oy + 3 * cell + 30, "Over - tap to retry", c_text, 16, VML_ANCHOR_CENTER); }
} else {
if lang == 0 { ui_2d_text(sw / 2, oy + 3 * cell + 30, "冒头就点它", c_dim, 12, VML_ANCHOR_CENTER); }
else { ui_2d_text(sw / 2, oy + 3 * cell + 30, "Tap the mole when it pops", c_dim, 12, VML_ANCHOR_CENTER); }
}
ui_2d_present();
}
ui_timer_kill(tid);
ui_win_close();
}