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

ruby/app/pong.rb · 9517 字节 · 下载原文

▶ 在浏览器里试玩

# 对打(Pong)—— 用 **Ruby** 写的手机小游戏
# Pong — a small phone game written in **Ruby**
#
# 玩法(与 Examples/c/app/pong.c / Examples/python/app/pong.py 同一套):
# Gameplay (same rules as the C / Python versions):
#   · 左板跟手指(点/拖左半屏),右板是 AI 追球,先到 5 分;
#   · the left paddle follows the finger (tap/drag on the left half), the right one is an AI, first to 5 wins;
#   · 球碰上下边界反弹,碰板反弹并提速;出界对方得分;
#   · the ball bounces off the top/bottom and off a paddle (speed up); going out scores for the opponent;
#   · 结束后点屏幕 / ESC / 回车重开。
#   · after the match ends, tap the screen / ESC / Enter to play again.
#
# ## 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 填充;数字转字符串用 .to_s;颜色写负数十进制;
#   · state is g_-prefixed; arrays are built with [] + push; numbers to strings use .to_s;
#     colours are negative decimals;
#   · **"只写不读"的名字在函数里写的是该函数自己的局部** ⇒ 用 pg_touch_state 先把状态名各读一遍。
#   · **a name that is only written and never read gets written to the function's own local**
#     ⇒ pg_touch_state reads every state name once first.
#
# 跑法 —— 手机:vml run examples/ruby/pong.rb
# How to run — on the phone: vml run examples/ruby/pong.rb
# 跑法 —— 桌面:vmlcli Examples/ruby/pong.rb --screen 480x640 --frames /tmp/fr
# How to run — on the desktop: vmlcli Examples/ruby/pong.rb --screen 480x640 --frames /tmp/fr

# UI 库的显式引用(用户 2026-10-01 定的口径),声明文件 Lib/ruby/vml_ui.rb 由 GenLib 生成。
require 'vml_ui'

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

def pg_touch_state()
  return g_sw + g_sh + g_pad_w + g_pad_h + g_ball_r + g_left_y + g_right_y + g_ball_x + g_ball_y + g_bvx + g_bvy + g_score_l + g_score_r + g_ended + g_frames + g_seed + g_serve_dir + g_lang + g_touch_y + g_m + g_fs_score + g_fs_hint + g_fs_end + g_score_dx + g_hint_y + g_end_y
end

def pg_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
  g_pad_w = g_sw / 28
  if g_pad_w < 8
    g_pad_w = 8
  end
  g_pad_h = g_sh / 6
  if g_pad_h < 40
    g_pad_h = 40
  end
  g_ball_r = g_sw / 40
  if g_ball_r < 4
    g_ball_r = 4
  end
  # 字号 / 比分间距随 min(W,H) 缩放(240x240 上 26 号比分 + 11 号提示要一起压住)。
  # Font sizes and score spacing scale with min(W,H) (on 240x240 the 26 px scores and
  # the 11 px hint have to fit together).
  g_m = g_sw
  if g_sh < g_m
    g_m = g_sh
  end
  g_fs_score = g_m / 16
  if g_fs_score < 14
    g_fs_score = 14
  end
  if g_fs_score > 26
    g_fs_score = 26
  end
  g_fs_hint = g_m / 26
  if g_fs_hint < 9
    g_fs_hint = 9
  end
  if g_fs_hint > 12
    g_fs_hint = 12
  end
  g_fs_end = g_fs_score - 4
  if g_fs_end < 14
    g_fs_end = 14
  end
  g_score_dx = g_sw / 6
  g_hint_y = 10 + g_fs_score + 6 + g_fs_hint
  g_end_y = g_sh / 2 - 10
end

def pg_serve()
  g_ball_x = g_sw / 2
  g_ball_y = g_sh / 2
  if g_serve_dir == 0
    g_bvx = -5
    g_serve_dir = 1
  else
    g_bvx = 5
    g_serve_dir = 0
  end
  g_seed = (g_seed * 1103 + 12345) % 32768
  if (g_seed / 256) % 2 == 0
    g_bvy = -3
  else
    g_bvy = 3
  end
end

def pg_new_game()
  g_left_y = g_sh / 2
  g_right_y = g_sh / 2
  g_score_l = 0
  g_score_r = 0
  g_ended = 0
  g_frames = 0
  g_serve_dir = 0
  pg_serve()
end

def pg_step()
  g_ball_x = g_ball_x + g_bvx
  g_ball_y = g_ball_y + g_bvy
  if g_ball_y < g_ball_r
    g_ball_y = g_ball_r
    g_bvy = 0 - g_bvy
  end
  if g_ball_y > g_sh - g_ball_r
    g_ball_y = g_sh - g_ball_r
    g_bvy = 0 - g_bvy
  end

  if g_bvx < 0 && g_ball_x <= 10 + g_pad_w + g_ball_r && g_ball_x >= 6
    top = g_left_y - g_pad_h / 2
    if g_ball_y >= top && g_ball_y <= top + g_pad_h
      g_ball_x = 10 + g_pad_w + g_ball_r
      g_bvx = 0 - g_bvx - 1
      if g_bvx < -14
        g_bvx = -14
      end
    end
  end

  if g_bvx > 0 && g_ball_x >= g_sw - 10 - g_pad_w - g_ball_r && g_ball_x <= g_sw - 6
    top2 = g_right_y - g_pad_h / 2
    if g_ball_y >= top2 && g_ball_y <= top2 + g_pad_h
      g_ball_x = g_sw - 10 - g_pad_w - g_ball_r
      g_bvx = 0 - g_bvx + 1
      if g_bvx > 14
        g_bvx = 14
      end
    end
  end

  if g_ball_x < 0 - g_ball_r
    g_score_r = g_score_r + 1
    if g_score_r >= 5
      g_ended = 1
    end
    pg_serve()
  end
  if g_ball_x > g_sw + g_ball_r
    g_score_l = g_score_l + 1
    if g_score_l >= 5
      g_ended = 1
    end
    pg_serve()
  end

  if g_right_y < g_ball_y
    g_right_y = g_right_y + 4
  end
  if g_right_y > g_ball_y
    g_right_y = g_right_y - 4
  end

  if g_left_y < g_pad_h / 2
    g_left_y = g_pad_h / 2
  end
  if g_left_y > g_sh - g_pad_h / 2
    g_left_y = g_sh - g_pad_h / 2
  end
  if g_right_y < g_pad_h / 2
    g_right_y = g_pad_h / 2
  end
  if g_right_y > g_sh - g_pad_h / 2
    g_right_y = g_sh - g_pad_h / 2
  end
end

def pg_draw()
  ui_2d_clear(g_c_bg)
  y = 10
  while y + 16 <= g_sh - 10
    ui_2d_rect(g_sw / 2 - 2, y, 4, 16, g_c_line, 1, 0, 0)
    y = y + 32
  end
  ui_2d_rect(10, g_left_y - g_pad_h / 2, g_pad_w, g_pad_h, g_c_left, 1, 0, 4)
  ui_2d_rect(g_sw - 10 - g_pad_w, g_right_y - g_pad_h / 2, g_pad_w, g_pad_h, g_c_right, 1, 0, 4)
  ui_2d_circle(g_ball_x, g_ball_y, g_ball_r, g_c_ball, 1, 0)

  ui_2d_text(g_sw / 2 - g_score_dx, 10 + g_fs_score, g_score_l.to_s, g_c_text, g_fs_score, VML_ANCHOR_CENTER)
  ui_2d_text(g_sw / 2 + g_score_dx, 10 + g_fs_score, g_score_r.to_s, g_c_text, g_fs_score, VML_ANCHOR_CENTER)

  if g_ended != 0
    if g_lang == 0
      if g_score_l >= 5
        ui_2d_text(g_sw / 2, g_end_y, "你赢了!点一下再来", g_c_left, g_fs_end, VML_ANCHOR_CENTER)
      else
        ui_2d_text(g_sw / 2, g_end_y, "电脑赢了,点一下再来", g_c_miss, g_fs_end, VML_ANCHOR_CENTER)
      end
    else
      if g_score_l >= 5
        ui_2d_text(g_sw / 2, g_end_y, "You win! Tap to retry", g_c_left, g_fs_end, VML_ANCHOR_CENTER)
      else
        ui_2d_text(g_sw / 2, g_end_y, "AI wins - tap to retry", g_c_miss, g_fs_end, VML_ANCHOR_CENTER)
      end
    end
  else
    if g_lang == 0
      ui_2d_text(g_sw / 2, g_hint_y, "左半屏滑动控制球板,先到 5 分", g_c_dim, g_fs_hint, VML_ANCHOR_CENTER)
    else
      ui_2d_text(g_sw / 2, g_hint_y, "Drag on the left half; first to 5", g_c_dim, g_fs_hint, VML_ANCHOR_CENTER)
    end
  end
end

# ══════════════════════════════════════════════════════════════════════════
# 第二段:全局布局与状态(⚠ 必须在所有 def 之后;文件头那条)
# ══════════════════════════════════════════════════════════════════════════

g_c_bg = -15724520        # 0xFF101018
g_c_line = -14013898      # 0xFF2A2A36
g_c_left = -11409298      # 0xFF51E86E
g_c_right = -11890471     # 0xFF4A90D9
g_c_ball = -1513232       # 0xFFE8E8F0
g_c_text = -1513232       # 0xFFE8E8F0
g_c_dim = -6643536        # 0xFF9AA0B0
g_c_miss = -2069424       # 0xFFE06C50

g_lang = ui_scr_get_language()
g_sw = 360
g_sh = 620
g_pad_w = 12
g_pad_h = 70
g_m = 360
g_fs_score = 26
g_fs_hint = 11
g_fs_end = 20
g_score_dx = 60
g_hint_y = 58
g_end_y = 270
g_ball_r = 6
g_left_y = 310
g_right_y = 310
g_ball_x = 180
g_ball_y = 310
g_bvx = -5
g_bvy = 3
g_score_l = 0
g_score_r = 0
g_ended = 0
g_frames = 0
g_seed = 20262
g_serve_dir = 0
g_touch_y = 0
g_alive = 1
g_done = 0

ui_win_open("对打", 0, 0)
pg_relayout()
pg_new_game()

g_tid = ui_timer_set(33, 1)

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

  if t == 9
    g_frames = g_frames + 1
    if g_frames >= 6000
      g_alive = 0
    end
    if g_ended == 0
      pg_step()
    end
  end

  if t == 6 || t == 4
    g_touch_y = ui_msg_get_b()
    if g_ended != 0
      pg_new_game()
    else
      g_left_y = g_touch_y
    end
  end

  if t == 7
    g_touch_y = ui_msg_get_b()
    if g_ended == 0
      g_left_y = g_touch_y
    end
  end

  if t == 1
    k = ui_msg_get_a()
    if k == 27
      g_alive = 0
    end
    if k == 13 || k == 82
      pg_new_game()
    end
  end

  if t == 11
    pg_relayout()
  end

  pg_draw()
  ui_2d_present()
end

ui_timer_kill(g_tid)
ui_win_close()