dart/app/memory.dart · 12307 字节 · 下载原文
// 记忆翻牌(Memory)—— Dart 版小游戏
// Memory matching — a small game in the Dart edition
//
// 玩法(与 `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.
//
// ## Dart 这一路的写法约束(见 `Examples/dart/app/snake.dart` 文件头)
// ## Style constraints on this Dart path (see the header of `Examples/dart/app/snake.dart`)
//
// · **全部状态放 `main` 的局部变量**(Dart 顶层可变状态读回来是脏的);
// · **all state lives in locals of `main`** (top-level mutable state reads back dirty in Dart);
// · 颜色一律写十六进制 `0xAARRGGBB`;整除写 `~/`;
// · colours are hexadecimal `0xAARRGGBB`; integer division is `~/`;
// · 数字直接用**字符串插值**(`"$moves"`),不需要手写转换;
// · numbers go straight through **string interpolation** (`"$moves"`), no hand-written conversion.
//
// 跑法 —— 手机:`vml run examples/dart/memory.dart`
// How to run — on the phone: `vml run examples/dart/memory.dart`
// 跑法 —— 桌面:`vmlcli Examples/dart/memory.dart --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/dart/memory.dart --screen 480x640 --frames /tmp/fr`
import 'vml_ui.dart';
void main() {
// ── 全部状态都放 main 的局部变量(见文件头)──
// ── All state lives in locals of main (see the header) ──
int sw = 0;
int sh = 0;
int cell = 80;
int ox = 20;
int oy = 110;
int lang = 0;
int i = 0;
int j = 0;
int x = 0;
int y = 0;
int p = 0;
int t = 0;
int k = 0;
int fi = 0;
int frames = 0;
int done = 0;
int firstX = -1;
int firstY = -1;
int hide = 0;
int moves = 0;
int matched = 0;
int won = 0;
int seed = 20265;
int px = 0;
int py = 0;
int cellHalf = 0;
String hud = "";
// 4×4 的状态表:pairOf[i] = 配对号(-1 = 已配对),up[i] = 是否翻开
// The 4x4 state tables: pairOf[i] = pair number (-1 = matched), up[i] = face up?
List<int> pairOf = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
List<int> up = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
int cBg = 0xFF101018;
int cBack = 0xFF2A2A36;
int cMatched = 0xFF16161E;
int cText = 0xFFE8E8F0;
int cDim = 0xFF9AA0B0;
int cFace = 0xFF101018;
// ⚠ 八种配对色分别放成**循环外赋值的独立局部**:本前端在双循环里对
// `pairColor[pairOf[x * 4 + y]]` 这样的嵌套下标实参不可靠 —— 实测颜色参数变成了 8
// (就是 `ui_2d_rect` 的最后一个圆角实参),那张牌直接画成了黑色。
// ⚠ The eight pair colours are separate locals assigned **outside the loop**: this frontend is
// unreliable at a nested subscript argument like `pairColor[pairOf[x * 4 + y]]` inside a nested
// loop -- measured, the colour argument became 8 (the trailing corner-radius argument) and that
// card was drawn black.
int cPair0 = 0xFFE06C50;
int cPair1 = 0xFFD9B44A;
int cPair2 = 0xFF51E86E;
int cPair3 = 0xFF4A90D9;
int cPair4 = 0xFF9B6CF0;
int cPair5 = 0xFFE06CA8;
int cPair6 = 0xFF4AD9C6;
int cPair7 = 0xFFC0C040;
lang = ui_scr_get_language();
ui_win_open_ex("记忆翻牌", 0, 0, 1, 1);
sw = ui_scr_get_w();
sh = ui_scr_get_h();
if (sw <= 0) { sw = 360; }
if (sh <= 0) { sh = 620; }
cellHalf = 0;
// ── 开一局:先铺成 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) {
pairOf[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);
p = pairOf[i];
pairOf[i] = pairOf[j];
pairOf[j] = p;
i = i - 1;
}
// ── 版面 ──
// ── Layout ──
// 宽高两头夹:小屏把顶部空白压掉,大屏不让棋盘顶出底部提示
// Clamp on both axes: small screens drop the top gap, big screens keep the board above the hint.
int side = sw - 20;
int sideH = sh - 64 - 34;
if (sideH < side) { side = sideH; }
cell = side ~/ 4;
if (cell < 12) { cell = 12; }
ox = (sw - 4 * cell) ~/ 2;
oy = 64;
int tid = ui_timer_set(50, 1);
frames = 0;
done = 0;
while (done == 0 && ui_win_is_closed() == 0) {
t = ui_msg_fetch_wait(50);
if (t == 0) { continue; }
if (t == 10) { break; } // MSG_WINDOWCLOSE
if (t == 6 || t == 4) { // MSG_TOUCHDOWN / MSG_MOUSEDOWN
px = ui_msg_get_a();
py = ui_msg_get_b();
if (won == 1) {
// 通关后点一下重开 —— 与其它语言版一致
// Tap after winning starts a new game — same as the other language versions
seed = 20265;
x = 0;
while (x < 4) {
y = 0;
while (y < 4) {
pairOf[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);
p = pairOf[i];
pairOf[i] = pairOf[j];
pairOf[j] = p;
i = i - 1;
}
firstX = -1;
firstY = -1;
hide = 0;
moves = 0;
matched = 0;
won = 0;
} else {
x = (px - ox) ~/ cell;
y = (py - oy) ~/ cell;
if (x >= 0 && y >= 0 && x < 4 && y < 4) {
i = x * 4 + y;
if (pairOf[i] >= 0 && up[i] == 0 && hide == 0) {
up[i] = 1;
if (firstX < 0) {
firstX = x;
firstY = y;
} else {
moves = moves + 1;
fi = firstX * 4 + firstY;
if (pairOf[fi] == pairOf[i]) {
pairOf[fi] = -1;
pairOf[i] = -1;
matched = matched + 2;
if (matched >= 16) { won = 1; }
firstX = -1;
} else {
hide = 8; // 8 拍 × 50ms ≈ 0.4 秒 / 8 ticks x 50 ms
}
}
}
}
}
}
if (t == 1) { // MSG_KEYDOWN
k = ui_msg_get_a();
if (k == 27 || k == 82) {
// ESC / R:重开(同上面的重开逻辑)
// ESC / R: new game (same reset as above)
seed = 20265;
x = 0;
while (x < 4) {
y = 0;
while (y < 4) {
pairOf[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);
p = pairOf[i];
pairOf[i] = pairOf[j];
pairOf[j] = p;
i = i - 1;
}
firstX = -1;
firstY = -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;
sideH = sh - 64 - 34;
if (sideH < side) { side = sideH; }
cell = side ~/ 4;
if (cell < 12) { cell = 12; }
ox = (sw - 4 * cell) ~/ 2;
oy = 64;
}
if (t == 9) { // MSG_TIMER
frames = frames + 1;
if (frames >= 8000) { done = 1; }
if (hide > 0) {
hide = hide - 1;
if (hide == 0) {
// 扣回所有翻开的牌
// Flip every face-up card back
x = 0;
while (x < 4) {
y = 0;
while (y < 4) {
up[x * 4 + y] = 0;
y = y + 1;
}
x = x + 1;
}
firstX = -1;
}
}
}
// ── 绘制(每帧重画)──
// ── Draw (every frame) ──
ui_2d_clear(cBg);
x = 0;
while (x < 4) {
y = 0;
while (y < 4) {
// ⚠ 坐标与牌面**全部内联**,不要先赋给"循环内被赋值的局部变量"再作实参:
// 本前端在双循环里对这种实参不可靠(实测牌位歪斜、行列不齐;与 `whack.dart` 同源)。
// ⚠ Coordinates and face are **inlined** rather than routed through a local assigned inside
// the nested loop: this frontend is unreliable at such arguments (measured: cards came out
// skewed row by row; same source as `whack.dart`).
if (pairOf[x * 4 + y] < 0) {
// 已配对:淡淡地留个印子
ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, cMatched, 1, 0, 6);
} else if (up[x * 4 + y] == 1) {
if (pairOf[x * 4 + y] == 0) {
ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, cPair0, 1, 0, 8);
} else if (pairOf[x * 4 + y] == 1) {
ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, cPair1, 1, 0, 8);
} else if (pairOf[x * 4 + y] == 2) {
ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, cPair2, 1, 0, 8);
} else if (pairOf[x * 4 + y] == 3) {
ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, cPair3, 1, 0, 8);
} else if (pairOf[x * 4 + y] == 4) {
ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, cPair4, 1, 0, 8);
} else if (pairOf[x * 4 + y] == 5) {
ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, cPair5, 1, 0, 8);
} else if (pairOf[x * 4 + y] == 6) {
ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, cPair6, 1, 0, 8);
} else {
ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, cPair7, 1, 0, 8);
}
// 牌面用数字(与 C/Python 版一致:随包字体缺花色字形会成豆腐块)
ui_2d_text(ox + x * cell + cell ~/ 2, oy + y * cell + cell ~/ 2 + cell ~/ 8,
"${pairOf[x * 4 + y] + 1}", cFace, cell ~/ 2, 1);
} else {
ui_2d_rect(ox + x * cell + 3, oy + y * cell + 3, cell - 6, cell - 6, cBack, 1, 0, 8);
}
y = y + 1;
}
x = x + 1;
}
hud = "$moves";
ui_2d_text(10, 22, hud, cText, 15, 0);
if (lang == 0) { ui_2d_text(10, 42, "步数", cDim, 10, 0); }
else { ui_2d_text(10, 42, "moves", cDim, 10, 0); }
int msgY = oy + 4 * cell + 22;
if (msgY > sh - 16) { msgY = sh - 16; }
if (won == 1) {
if (lang == 0) { ui_2d_text(sw ~/ 2, msgY, "全配好了!点一下再来", cText, 15, 1); }
else { ui_2d_text(sw ~/ 2, msgY, "All matched! Tap for a new one", cText, 15, 1); }
} else {
if (lang == 0) { ui_2d_text(sw ~/ 2, msgY, "翻两张一样的", cDim, 12, 1); }
else { ui_2d_text(sw ~/ 2, msgY, "Flip two that match", cDim, 12, 1); }
}
ui_2d_present();
}
ui_timer_kill(tid);
ui_win_close();
}