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

dart/app/whack.dart · 7701 字节 · 下载原文

▶ 在浏览器里试玩

// 打地鼠(Whack)—— Dart 版小游戏
// Whack-a-mole — a small game in the Dart edition
//
// 玩法(与 `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.
//
// ## Dart 这一路的写法约束(见 `Examples/dart/app/snake.dart` 文件头)
// ## Style constraints on this Dart path (see the header of `Examples/dart/app/snake.dart`)
//
//   · **全部状态放 `main` 的局部变量**;颜色写十六进制 `0xAARRGGBB`;整除写 `~/`;
//   · **all state lives in locals of `main`**; colours are hexadecimal `0xAARRGGBB`; integer division is `~/`;
//   · 数字用**字符串插值**。
//   · numbers go through **string interpolation**.
//
// 跑法 —— 手机:`vml run examples/dart/whack.dart`
// How to run — on the phone: `vml run examples/dart/whack.dart`
// 跑法 —— 桌面:`vmlcli Examples/dart/whack.dart --screen 480x640 --frames /tmp/fr`
// How to run — on the desktop: `vmlcli Examples/dart/whack.dart --screen 480x640 --frames /tmp/fr`

import 'vml_ui.dart';

void main() {
  // ── 全部状态都放 main 的局部变量(见文件头)──
  int sw = 360;
  int sh = 620;
  int cell = 120;
  int ox = 0;
  int oy = 130;
  int lang = 0;
  int i = 0;
  int x = 0;
  int y = 0;
  int r = 0;
  int cx = 0;
  int cy = 0;
  int mc = 0;
  int idx = 0;
  int t = 0;
  int k = 0;
  int frames = 0;
  int done = 0;
  int moleAt = -1;
  int moleTicks = 0;
  int waitTicks = 6;
  int score = 0;
  int lives = 3;
  int over = 0;
  int hitFlash = 0;
  int seed = 20267;
  String hud = "";

  int cBg = 0xFF101018;
  int cHole = 0xFF1A1A24;
  int cMole = 0xFFD9B44A;
  int cText = 0xFFE8E8F0;
  int cDim = 0xFF9AA0B0;
  int cMiss = 0xFFE06C50;
  int cHit = 0xFFFFFFFF;

  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; }
  // 宽高两头夹:小屏把顶部空白压掉,大屏不让九宫格顶出底部提示
  // Clamp on both axes: small screens drop the top gap, big screens keep the 3x3 grid above the hint.
  int side = sw - 24;
  int sideH = sh - 64 - 34;
  if (sideH < side) { side = sideH; }
  cell = side ~/ 3;
  if (cell < 12) { cell = 12; }
  ox = (sw - 3 * 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
      x = ui_msg_get_a();
      y = ui_msg_get_b();
      if (over == 1) {
        // 结束后点一下重开
        score = 0;
        lives = 3;
        over = 0;
        hitFlash = 0;
        moleAt = -1;
        waitTicks = 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 == moleAt) {
            score = score + 1;
            moleAt = -1;
            seed = (seed * 1103 + 12345) % 32768;
            waitTicks = 4 + (seed ~/ 256) % 4;
            hitFlash = 1;
          }
        }
      }
    }

    if (t == 1) {                            // MSG_KEYDOWN
      k = ui_msg_get_a();
      if (k == 27 || k == 82) {
        score = 0;
        lives = 3;
        over = 0;
        hitFlash = 0;
        moleAt = -1;
        waitTicks = 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;
      sideH = sh - 64 - 34;
      if (sideH < side) { side = sideH; }
      cell = side ~/ 3;
      if (cell < 12) { cell = 12; }
      ox = (sw - 3 * cell) ~/ 2;
      oy = 64;
    }

    if (t == 9) {                            // MSG_TIMER
      frames = frames + 1;
      if (frames >= 6000) { 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) {
            seed = (seed * 1103 + 12345) % 32768;
            moleAt = (seed ~/ 256) % 9;
            moleTicks = 14;
          }
        }
      }
    }

    // ── 绘制(每帧重画)──
    ui_2d_clear(cBg);
    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, cHole, 1, 0);
      if (i == moleAt) {
        // ⚠ 地鼠颜色**不能先赋给局部变量再传**:本前端在双循环里对"循环内被赋值的变量"
        //   实参传递不可靠(实测地鼠主体被画成透明/黑),改成 if/else 各传常量。
        // ⚠ The mole colour is **not** routed through a local: this frontend is unreliable at
        //   passing a variable that is assigned inside the nested loop (measured: the mole body
        //   came out transparent/black), so each branch passes a constant.
        if (hitFlash == 1) {
          ui_2d_circle(cx, cy, r, cHit, 1, 0);
        } else {
          ui_2d_circle(cx, cy, r, cMole, 1, 0);
        }
        ui_2d_circle(cx - r ~/ 3, cy - r ~/ 4, r ~/ 5, cBg, 1, 0);
        ui_2d_circle(cx + r ~/ 3, cy - r ~/ 4, r ~/ 5, cBg, 1, 0);
      }
      i = i + 1;
    }

    hud = "$score";
    ui_2d_text(10, 24, hud, cText, 18, 0);
    if (lang == 0) { ui_2d_text(10, 48, "得分", cDim, 10, 0); }
    else { ui_2d_text(10, 48, "score", cDim, 10, 0); }
    hud = "$lives";
    ui_2d_text(sw - 10, 24, hud, cMiss, 18, 2);
    if (lang == 0) { ui_2d_text(sw - 10, 48, "命", cDim, 10, 2); }
    else { ui_2d_text(sw - 10, 48, "lives", cDim, 10, 2); }

    int msgY = oy + 3 * cell + 24;
    if (msgY > sh - 16) { msgY = sh - 16; }
    if (over == 1) {
      if (lang == 0) { ui_2d_text(sw ~/ 2, msgY, "结束,点一下再来", cText, 16, 1); }
      else { ui_2d_text(sw ~/ 2, msgY, "Over - tap to retry", cText, 16, 1); }
    } else {
      if (lang == 0) { ui_2d_text(sw ~/ 2, msgY, "冒头就点它", cDim, 12, 1); }
      else { ui_2d_text(sw ~/ 2, msgY, "Tap the mole when it pops", cDim, 12, 1); }
    }
    ui_2d_present();
  }

  ui_timer_kill(tid);
  ui_win_close();
}