csharp/app/whack.cs · 10315 字节 · 下载原文
// 打地鼠 —— 用 **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/csharp/app/snake.cs` 文件头)
// ## Style constraints (same as the header of `Examples/csharp/app/snake.cs`)
//
// · 常量写**数字**、颜色写**负数十进制**;
// · constants are **numbers** and colours are **negative decimals**;
// · 数字转字符串走本文件的 `numToStr()` / `digit()`;
// · numbers to strings go through `numToStr()` / `digit()` here;
// · 循环更新写 `i = i + 1`。
// · loop updates are written `i = i + 1`.
//
// 跑法 —— 手机:`vml run examples/csharp/whack.cs`
// How to run — on the phone: `vml run examples/csharp/whack.cs`
// 跑法 —— 桌面:`vmlcli Examples/csharp/whack.cs --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/csharp/whack.cs --screen 480x640 --frames /tmp/fr`
// 显式引用 UI 库:`ui_*` 的声明在 `Lib/csharp/VmlUi.cs`(tools/GenLib 生成,别手改)。
// Explicit reference to the UI library: `ui_*` is declared in `Lib/csharp/VmlUi.cs` (generated by tools/GenLib; don't hand-edit).
using Vml.Ui;
class WhackGame
{
// ── 常量(源:Lib/c/vml_ui.h)──
// ── Constants (source: Lib/c/vml_ui.h) ──
const int MSG_KEYDOWN = 1;
const int MSG_MOUSEDOWN = 4;
const int MSG_TOUCHDOWN = 6;
const int MSG_TIMER = 9;
const int MSG_WINDOWCLOSE = 10;
const int MSG_WINDOWRESIZE = 11;
const int KEY_ESCAPE = 27;
const int KEY_R = 82;
const int ANCHOR_LEFT = 0;
const int ANCHOR_CENTER = 1;
const int ANCHOR_RIGHT = 2;
const int MAX_FRAMES = 6000;
const int GRID = 3;
const int SHOW_TICKS = 14; // 一只地鼠活 14 拍 ≈ 0.7 秒
const int C_BG = -15724520; // 0xFF101018
const int C_HOLE = -15066588; // 0xFF1A1A24
const int C_MOLE = -2509750; // 0xFFD9B44A
const int C_TEXT = -1513232; // 0xFFE8E8F0
const int C_DIM = -6643536; // 0xFF9AA0B0
const int C_MISS = -2069424; // 0xFFE06C50
const int C_HIT = -1; // 0xFFFFFFFF
static int lang;
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;
// 数字 → 字符串。**不能借标准库的字符串转换**:那是 C 的运行库,别的前端不一定有。
// Number → string. **Cannot borrow the standard library's string conversion**: that is C's runtime library.
static string numToStr(int v)
{
if (v == 0) return "0";
string s = "";
int neg = 0;
if (v < 0) { neg = 1; v = 0 - v; }
while (v > 0)
{
int d = v % 10;
s = digit(d) + s;
v = v / 10;
}
if (neg != 0) s = "-" + s;
return s;
}
static string digit(int d)
{
if (d == 0) return "0";
if (d == 1) return "1";
if (d == 2) return "2";
if (d == 3) return "3";
if (d == 4) return "4";
if (d == 5) return "5";
if (d == 6) return "6";
if (d == 7) return "7";
if (d == 8) return "8";
return "9";
}
// 确定性伪随机(验收要能复现同一局)
// Deterministic PRNG (acceptance needs to reproduce the same run)
static int rnd(int n)
{
seed = (seed * 1103 + 12345) % 32768;
return (seed / 256) % n;
}
static void layout()
{
sw = ui_scr_get_w();
sh = ui_scr_get_h();
if (sw <= 0) sw = 360;
if (sh <= 0) sh = 620;
// 边长取宽/高可用区中小的那个;240x240 上原来的绝对阈值只剩 60px。
// Side = the smaller available dimension; the old absolute threshold left only 60px on
// 240x240.
int topReserve = 70; // 顶部得分/命 HUD
int botReserve = 50; // 网格下方提示行
int maxW = sw - 24;
int maxH = sh - topReserve - botReserve;
int side = maxW < maxH ? maxW : maxH;
cell = side / GRID;
if (cell < 8) cell = 8;
ox = (sw - GRID * cell) / 2;
oy = 130;
if (oy + GRID * cell > sh - botReserve)
oy = topReserve + (maxH - GRID * cell) / 2;
if (oy < topReserve) oy = topReserve;
}
static void pickHole()
{
moleAt = rnd(GRID * GRID);
moleTicks = SHOW_TICKS;
}
static void newGame()
{
score = 0;
lives = 3;
over = 0;
hitFlash = 0;
moleAt = -1;
waitTicks = 6;
}
static 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, numToStr(score), C_TEXT, 18, ANCHOR_LEFT);
if (lang == 0) ui_2d_text(10, 48, "得分", C_DIM, 10, ANCHOR_LEFT);
else ui_2d_text(10, 48, "score", C_DIM, 10, ANCHOR_LEFT);
ui_2d_text(sw - 10, 24, numToStr(lives), C_MISS, 18, ANCHOR_RIGHT);
if (lang == 0) ui_2d_text(sw - 10, 48, "命", C_DIM, 10, ANCHOR_RIGHT);
else ui_2d_text(sw - 10, 48, "lives", C_DIM, 10, ANCHOR_RIGHT);
if (over == 1)
{
if (lang == 0) ui_2d_text(sw / 2, oy + GRID * cell + 30, "结束,点一下再来", C_TEXT, 16, ANCHOR_CENTER);
else ui_2d_text(sw / 2, oy + GRID * cell + 30, "Over - tap to retry", C_TEXT, 16, ANCHOR_CENTER);
}
else
{
if (lang == 0) ui_2d_text(sw / 2, oy + GRID * cell + 30, "冒头就点它", C_DIM, 12, ANCHOR_CENTER);
else ui_2d_text(sw / 2, oy + GRID * cell + 30, "Tap the mole when it pops", C_DIM, 12, ANCHOR_CENTER);
}
ui_2d_present();
}
static 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;
}
}
static void Main()
{
lang = ui_scr_get_language();
ui_win_open(lang == 0 ? "打地鼠" : "Whack", 0, 0);
layout();
ui_scr_set_keep_on(1);
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 == 0) continue;
if (t == MSG_WINDOWCLOSE) break;
if (t == MSG_TOUCHDOWN || t == MSG_MOUSEDOWN)
{
tap(ui_msg_get_a(), ui_msg_get_b());
draw();
continue;
}
if (t == MSG_KEYDOWN)
{
int k = ui_msg_get_a();
if (k == KEY_ESCAPE || k == KEY_R) { newGame(); draw(); }
continue;
}
if (t == MSG_WINDOWRESIZE) { layout(); draw(); continue; }
if (t == 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_scr_set_keep_on(0);
ui_win_close();
}
}