-- 对打(Pong)—— 用 **Lua** 写的手机小游戏 -- Pong — a small phone game written in **Lua** -- -- 玩法(与 Examples/c/app/pong.c / Examples/python/app/pong.py 同一套): -- 左板跟手指(点/拖左半屏),右板是 AI 追球,先到 5 分; -- 球碰上下边界反弹,碰板反弹并提速;出界对方得分;结束后点屏幕 / ESC 回车重开。 -- -- ## 本语言的实测约束(见 Examples/lua/app/snake.lua / memory.lua 文件头) -- · 全局状态**必须写在函数定义之前**;入口是 `main()`; -- · **没有整数除法**:`/` 返回浮点、UI 参数按位型解释 ⇒ 一律 `idiv`; -- · 逻辑运算符是 `or` / `and`;`!=` 写 `~=`; -- · 数字上屏走**点阵**(`FONT` 15 位掩码);位测试用取模,**别用 idiv**(b=1 时会慢到卡死)。 -- -- 跑法 —— 手机:vml run examples/lua/pong.lua -- 跑法 —— 桌面:vmlcli Examples/lua/pong.lua --screen 480x640 --frames /tmp/fr require("vml_ui") FONT = {31599, 29850, 29671, 31207, 18925, 31183, 31695, 18727, 31727, 31215} BIT = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384} MZ_SW = 360 MZ_SH = 620 PAD_W = 12 PAD_H = 70 BALL_R = 6 LEFT_Y = 310 RIGHT_Y = 310 BALL_X = 180 BALL_Y = 310 BVX = -5 BVY = 3 SCORE_L = 0 SCORE_R = 0 ENDED = 0 FRAMES = 0 SEED = 20262 SERVE_DIR = 0 LANG = 0 ALIVE = 1 T_WIN = "你赢了!点一下再来" T_LOSE = "电脑赢了,点一下再来" T_HINT = "左半屏滑动控制球板,先到 5 分" function idiv(a, b) if b <= 0 then return 0 end local q = 0 local r = a if r < 0 then r = 0 - r end while r >= b do r = r - b q = q + 1 end if a < 0 then return 0 - q end return q end function draw_digit(x, y, d, px, color) if d < 0 then d = 0 end if d > 9 then d = 9 end local mask = FONT[d + 1] local r = 0 while r < 5 do local c = 0 while c < 3 do local bitv = BIT[r * 3 + c + 1] if mask % (bitv * 2) >= bitv then ui_2d_rect(x + c * px, y + r * px, px, px, color, 1, 0, 0) end c = c + 1 end r = r + 1 end end function draw_number(x, y, v, px, color) if v < 0 then v = 0 end if v > 999999 then v = 999999 end local digits = 1 local n = v while n >= 10 do n = idiv(n, 10) digits = digits + 1 end local i = digits local rest = v while i >= 1 do local d = rest % 10 rest = idiv(rest, 10) draw_digit(x + (i - 1) * px * 4, y, d, px, color) i = i - 1 end end function pong_rnd(n) SEED = (SEED * 1103 + 12345) % 32768 return idiv(SEED, 256) % n end function relayout() MZ_SW = ui_scr_get_w() MZ_SH = ui_scr_get_h() if MZ_SW <= 0 then MZ_SW = 360 end if MZ_SH <= 0 then MZ_SH = 620 end PAD_W = idiv(MZ_SW, 28) if PAD_W < 8 then PAD_W = 8 end PAD_H = idiv(MZ_SH, 6) if PAD_H < 40 then PAD_H = 40 end BALL_R = idiv(MZ_SW, 40) if BALL_R < 4 then BALL_R = 4 end end function serve() BALL_X = idiv(MZ_SW, 2) BALL_Y = idiv(MZ_SH, 2) if SERVE_DIR == 0 then BVX = -5 SERVE_DIR = 1 else BVX = 5 SERVE_DIR = 0 end if pong_rnd(2) == 0 then BVY = -3 else BVY = 3 end end function new_game() LEFT_Y = idiv(MZ_SH, 2) RIGHT_Y = idiv(MZ_SH, 2) SCORE_L = 0 SCORE_R = 0 ENDED = 0 FRAMES = 0 SERVE_DIR = 0 serve() end function step() BALL_X = BALL_X + BVX BALL_Y = BALL_Y + BVY if BALL_Y < BALL_R then BALL_Y = BALL_R BVY = 0 - BVY end if BALL_Y > MZ_SH - BALL_R then BALL_Y = MZ_SH - BALL_R BVY = 0 - BVY end if BVX < 0 and BALL_X <= 10 + PAD_W + BALL_R and BALL_X >= 6 then local top = LEFT_Y - idiv(PAD_H, 2) if BALL_Y >= top and BALL_Y <= top + PAD_H then BALL_X = 10 + PAD_W + BALL_R BVX = 0 - BVX - 1 if BVX < -14 then BVX = -14 end end end if BVX > 0 and BALL_X >= MZ_SW - 10 - PAD_W - BALL_R and BALL_X <= MZ_SW - 6 then local top2 = RIGHT_Y - idiv(PAD_H, 2) if BALL_Y >= top2 and BALL_Y <= top2 + PAD_H then BALL_X = MZ_SW - 10 - PAD_W - BALL_R BVX = 0 - BVX + 1 if BVX > 14 then BVX = 14 end end end if BALL_X < 0 - BALL_R then SCORE_R = SCORE_R + 1 if SCORE_R >= 5 then ENDED = 1 end serve() end if BALL_X > MZ_SW + BALL_R then SCORE_L = SCORE_L + 1 if SCORE_L >= 5 then ENDED = 1 end serve() end if RIGHT_Y < BALL_Y then RIGHT_Y = RIGHT_Y + 4 end if RIGHT_Y > BALL_Y then RIGHT_Y = RIGHT_Y - 4 end if LEFT_Y < idiv(PAD_H, 2) then LEFT_Y = idiv(PAD_H, 2) end if LEFT_Y > MZ_SH - idiv(PAD_H, 2) then LEFT_Y = MZ_SH - idiv(PAD_H, 2) end if RIGHT_Y < idiv(PAD_H, 2) then RIGHT_Y = idiv(PAD_H, 2) end if RIGHT_Y > MZ_SH - idiv(PAD_H, 2) then RIGHT_Y = MZ_SH - idiv(PAD_H, 2) end end function draw() ui_2d_clear(-15724520) local y = 10 while y + 16 <= MZ_SH - 10 do ui_2d_rect(idiv(MZ_SW, 2) - 2, y, 4, 16, -14013898, 1, 0, 0) y = y + 32 end ui_2d_rect(10, LEFT_Y - idiv(PAD_H, 2), PAD_W, PAD_H, -11409298, 1, 0, 4) ui_2d_rect(MZ_SW - 10 - PAD_W, RIGHT_Y - idiv(PAD_H, 2), PAD_W, PAD_H, -11890471, 1, 0, 4) ui_2d_circle(BALL_X, BALL_Y, BALL_R, -1513232, 1, 0) draw_number(idiv(MZ_SW, 2) - 78, 14, SCORE_L, 5, -1513232) draw_number(idiv(MZ_SW, 2) + 62, 14, SCORE_R, 5, -1513232) if ENDED ~= 0 then if SCORE_L >= 5 then ui_2d_text(idiv(MZ_SW, 2), idiv(MZ_SH, 2) - 40, T_WIN, -11409298, 20, VML_ANCHOR_CENTER) else ui_2d_text(idiv(MZ_SW, 2), idiv(MZ_SH, 2) - 40, T_LOSE, -2069424, 20, VML_ANCHOR_CENTER) end else ui_2d_text(idiv(MZ_SW, 2), 58, T_HINT, -6643536, 11, VML_ANCHOR_CENTER) end ui_2d_present() end function main() LANG = ui_scr_get_language() ui_win_open("对打", 0, 0) relayout() SEED = 20262 new_game() draw() TID = ui_timer_set(33, 1) while ALIVE == 1 do local t = ui_msg_fetch_wait(33) if t == 10 then ALIVE = 0 end if t == 9 then FRAMES = FRAMES + 1 if FRAMES >= 6000 then ALIVE = 0 end if ENDED == 0 then step() end end if t == 6 or t == 4 then if ENDED ~= 0 then new_game() else LEFT_Y = ui_msg_get_b() end end if t == 7 then if ENDED == 0 then LEFT_Y = ui_msg_get_b() end end if t == 1 then local k = ui_msg_get_a() if k == 27 then ALIVE = 0 end if k == 13 or k == 82 then new_game() end end if t == 11 then relayout() end draw() end ui_timer_kill(TID) ui_win_close() end main()