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

ruby/app/whack.rb · 10534 字节 · 下载原文

▶ 在浏览器里试玩

# 打地鼠 —— 用 **Ruby** 写的手机小游戏(反应类 / 限时点中)
# Whack-a-mole — a small phone game written in **Ruby** (reaction / hit it in time)
#
# 玩法(与 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
#
# 地鼠的存活时间记在**拍数**上(g_mole_ticks),不是 time() —— 定时器就是心跳,
# "一只地鼠活多少拍"可计算、不随机器快慢漂移。
# A mole's lifetime is counted in **ticks** (g_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.
#
# ## Ruby 这一路的写法约束(见 Examples/ruby/app/minesweeper.rb 文件头)
# ## Style constraints on this Ruby path (see the header of Examples/ruby/app/minesweeper.rb)
#
#   · **所有 def 放在文件最前面,全局变量赋值在它们之后**(反过来函数里读到的是垃圾值);
#   · **every def sits at the very top, and global assignments come after them**;
#   · 状态一律 g_ 前缀;数组用 [] + push 填充;
#   · state is g_-prefixed; arrays are built with [] + push;
#   · 数字转字符串用 .to_s,拼接用 +;
#   · numbers to strings use .to_s; concatenation uses +;
#   · 颜色写负数十进制;
#   · colours are negative decimals;
#   · **"只写不读"的名字在函数里写的是该函数自己的局部** ⇒ 用 wh_touch_state 先把状态名各读一遍。
#   · **a name that is only written and never read gets written to the function's own local**
#     ⇒ wh_touch_state reads every state name once first.
#
# 跑法 —— 手机:vml run examples/ruby/whack.rb
# How to run — on the phone: vml run examples/ruby/whack.rb
# 跑法 —— 桌面:vmlcli Examples/ruby/whack.rb --screen 480x640 --frames /tmp/fr
# How to run — on the desktop: vmlcli Examples/ruby/whack.rb --screen 480x640 --frames /tmp/fr

# UI 库的显式引用(用户 2026-10-01 定的口径),声明文件 Lib/ruby/vml_ui.rb 由 GenLib 生成。
# The explicit reference to the UI library (user convention of 2026-10-01); Lib/ruby/vml_ui.rb is generated by GenLib.
require 'vml_ui'

# ══════════════════════════════════════════════════════════════════════════
# 第一段:全部函数(⚠ 必须排在"给全局赋值"之前,理由见文件头那条)
# Section 1: all the functions (⚠ must come before any assignment to a global; see the header)
# ══════════════════════════════════════════════════════════════════════════

# ⚠ 编译期保险(见 minesweeper.rb 同名函数):让各状态的全局槽在任何函数生成之前就存在。
# ⚠ Compile-time safeguard (see the same function in minesweeper.rb): make every state slot exist
#   before any function is generated.
def wh_touch_state()
  return g_seed + g_score + g_lives + g_over + g_hit_flash + g_mole + g_mole_ticks + g_wait + g_frames + g_sw + g_sh + g_cell + g_ox + g_oy + g_lang + g_m + g_fs_score + g_fs_lbl + g_fs_hint + g_top_y + g_top + g_hint_y + g_side
end

def wh_relayout()
  g_sw = ui_scr_get_w()
  g_sh = ui_scr_get_h()
  if g_sw <= 0
    g_sw = 360
  end
  if g_sh <= 0
    g_sh = 620
  end
  # 字号随 min(W,H) 缩放;顶栏 + 底部提示先量出来,剩下的才给 3x3 的洞。
  # 旧写法在 240x240 上把格子压到 20px(整块只有 60px),且 130 的固定顶栏把洞挤到下半屏。
  # Font sizes scale with min(W,H); the top bar and footer hint are measured first and
  # only the rest goes to the 3x3 holes. The old layout squeezed the cells to 20 px on
  # 240x240 (a 60 px board) and the fixed 130 px header pushed it into the lower half.
  g_m = g_sw
  if g_sh < g_m
    g_m = g_sh
  end
  g_fs_score = g_m / 26
  if g_fs_score < 12
    g_fs_score = 12
  end
  if g_fs_score > 20
    g_fs_score = 20
  end
  g_fs_lbl = g_m / 50
  if g_fs_lbl < 9
    g_fs_lbl = 9
  end
  if g_fs_lbl > 12
    g_fs_lbl = 12
  end
  g_fs_hint = g_m / 28
  if g_fs_hint < 9
    g_fs_hint = 9
  end
  if g_fs_hint > 12
    g_fs_hint = 12
  end
  g_top_y = 6
  g_top = g_top_y + g_fs_score + 2 + g_fs_lbl + 8
  g_side = g_sw - 24
  avail = g_sh - g_top - g_fs_hint - 20
  if avail < g_side
    g_side = avail
  end
  g_cell = g_side / 3
  if g_cell < 8
    g_cell = 8
  end
  g_ox = (g_sw - 3 * g_cell) / 2
  g_oy = g_top
  g_hint_y = g_oy + 3 * g_cell + 14
  if g_hint_y > g_sh - g_fs_hint - 6
    g_hint_y = g_sh - g_fs_hint - 6
  end
end

def wh_new_game()
  g_score = 0
  g_lives = 3
  g_over = 0
  g_hit_flash = 0
  g_mole = -1
  g_wait = 6
  g_frames = 0
end

def wh_draw()
  ui_2d_clear(g_c_bg)
  i = 0
  while i < 9
    cx = g_ox + (i % 3) * g_cell + g_cell / 2
    cy = g_oy + (i / 3) * g_cell + g_cell / 2
    r = g_cell / 3
    ui_2d_ellipse(cx, cy + r / 3, r + g_cell / 12, r / 2 + g_cell / 20, g_c_hole, 1, 0)
    if i == g_mole
      # ⚠ 地鼠颜色不经过"循环内被赋值的局部变量"再传(本前端这种实参偶尔会丢,与 dart 同源)
      # ⚠ The mole colour is not routed through a variable assigned inside the loop (same measured
      #   issue as the Dart edition: such an argument occasionally goes missing)
      if g_hit_flash == 1
        ui_2d_circle(cx, cy, r, g_c_hit, 1, 0)
      else
        ui_2d_circle(cx, cy, r, g_c_mole, 1, 0)
      end
      ui_2d_circle(cx - r / 3, cy - r / 4, r / 5, g_c_bg, 1, 0)
      ui_2d_circle(cx + r / 3, cy - r / 4, r / 5, g_c_bg, 1, 0)
    end
    i = i + 1
  end

  ui_2d_text(10, g_top_y + g_fs_score, g_score.to_s, g_c_text, g_fs_score, VML_ANCHOR_LEFT)
  if g_lang == 0
    ui_2d_text(10, g_top_y + g_fs_score + 2 + g_fs_lbl, "得分", g_c_dim, g_fs_lbl, VML_ANCHOR_LEFT)
  else
    ui_2d_text(10, g_top_y + g_fs_score + 2 + g_fs_lbl, "score", g_c_dim, g_fs_lbl, VML_ANCHOR_LEFT)
  end
  ui_2d_text(g_sw - 10, g_top_y + g_fs_score, g_lives.to_s, g_c_miss, g_fs_score, VML_ANCHOR_RIGHT)
  if g_lang == 0
    ui_2d_text(g_sw - 10, g_top_y + g_fs_score + 2 + g_fs_lbl, "命", g_c_dim, g_fs_lbl, VML_ANCHOR_RIGHT)
  else
    ui_2d_text(g_sw - 10, g_top_y + g_fs_score + 2 + g_fs_lbl, "lives", g_c_dim, g_fs_lbl, VML_ANCHOR_RIGHT)
  end

  if g_over == 1
    if g_lang == 0
      ui_2d_text(g_sw / 2, g_hint_y, "结束,点一下再来", g_c_text, g_fs_score, VML_ANCHOR_CENTER)
    else
      ui_2d_text(g_sw / 2, g_hint_y, "Over - tap to retry", g_c_text, g_fs_score, VML_ANCHOR_CENTER)
    end
  else
    if g_lang == 0
      ui_2d_text(g_sw / 2, g_hint_y, "冒头就点它", g_c_dim, g_fs_hint, VML_ANCHOR_CENTER)
    else
      ui_2d_text(g_sw / 2, g_hint_y, "Tap the mole when it pops", g_c_dim, g_fs_hint, VML_ANCHOR_CENTER)
    end
  end
end

# ══════════════════════════════════════════════════════════════════════════
# 第二段:全局布局与状态(⚠ 必须在所有 def 之后;文件头那条)
# Section 2: layout and state (⚠ must come after every def; see the header)
# ══════════════════════════════════════════════════════════════════════════

g_c_bg = -15724520        # 0xFF101018
g_c_hole = -15066588      # 0xFF1A1A24
g_c_mole = -2509750       # 0xFFD9B44A
g_c_text = -1513232       # 0xFFE8E8F0
g_c_dim = -6643536        # 0xFF9AA0B0
g_c_miss = -2069424       # 0xFFE06C50
g_c_hit = -1              # 0xFFFFFFFF

g_lang = ui_scr_get_language()
g_sw = 360
g_sh = 620
g_cell = 120
g_ox = 0
g_oy = 130
g_side = 0
g_m = 360
g_fs_score = 18
g_fs_lbl = 10
g_fs_hint = 12
g_top_y = 24
g_top = 130
g_hint_y = 390

g_seed = 20267
g_score = 0
g_lives = 3
g_over = 0
g_hit_flash = 0
g_mole = -1
g_mole_ticks = 0
g_wait = 6
g_frames = 0
g_alive = 1

ui_win_open("打地鼠", 0, 0)
wh_relayout()
wh_new_game()

g_tid = ui_timer_set(50, 1)

while g_alive == 1
  t = ui_msg_fetch_wait(50)
  if t == 10
    g_alive = 0
  end

  if t == 6 || t == 4
    tx = ui_msg_get_a()
    ty = ui_msg_get_b()
    if g_over == 1
      # 结束后点一下重开 / after the run ends, a tap starts a new one
      g_score = 0
      g_lives = 3
      g_over = 0
      g_hit_flash = 0
      g_mole = -1
      g_wait = 6
    else
      x = (tx - g_ox) / g_cell
      y = (ty - g_oy) / g_cell
      if x >= 0 && y >= 0 && x < 3 && y < 3
        idx = y * 3 + x
        if idx == g_mole
          g_score = g_score + 1
          g_mole = -1
          g_seed = (g_seed * 1103 + 12345) % 32768
          g_wait = 4 + (g_seed / 256) % 4
          g_hit_flash = 1
        end
      end
    end
  end

  if t == 1
    k = ui_msg_get_a()
    if k == 27 || k == 82
      g_score = 0
      g_lives = 3
      g_over = 0
      g_hit_flash = 0
      g_mole = -1
      g_wait = 6
    end
  end

  if t == 11
    wh_relayout()
  end

  if t == 9
    g_frames = g_frames + 1
    if g_frames >= 6000
      g_alive = 0
    end
    if g_over == 0
      g_hit_flash = 0
      if g_mole >= 0
        g_mole_ticks = g_mole_ticks - 1
        if g_mole_ticks <= 0
          # 没点中:扣命 / missed: lose a life
          g_mole = -1
          g_lives = g_lives - 1
          g_wait = 8
          if g_lives <= 0
            g_over = 1
          end
        end
      else
        g_wait = g_wait - 1
        if g_wait <= 0
          g_seed = (g_seed * 1103 + 12345) % 32768
          g_mole = (g_seed / 256) % 9
          g_mole_ticks = 14
        end
      end
    end
  end

  wh_draw()
  ui_2d_present()
end

ui_timer_kill(g_tid)
ui_win_close()