-- 打地鼠 —— 用 **Lua** 写的手机小游戏 -- Whack-a-mole — a small phone game written in **Lua** -- -- 玩法(与 Examples/c/app/whack.c / Examples/python/app/whack.py 同一套): -- 3×3 九个洞,每 SHOW_TICKS 拍有一个洞冒出地鼠;在它缩回去之前点中 +1 分, -- 超时没点中扣一条命;三条命用完结束;结束后点屏幕 / ESC 重开。 -- -- ## "计时归谁管"是这一类游戏的关键 -- -- 地鼠的存活时间记在**拍数**上(MOLE_TICKS),不是 time() —— 定时器就是心跳。 -- -- ## 本语言的实测约束(见 Examples/lua/app/snake.lua / memory.lua 文件头) -- · 全局状态**必须写在函数定义之前**;逻辑写在顶层函数、入口是 `main()`; -- · **没有整数除法**:`/` 返回浮点、UI 参数会被按位型解释 ⇒ 一律 `idiv`; -- · 逻辑运算符是 `or` / `and`(`||`/`&&` 是词法错误);裸 `return` 写 `return 0`; -- · 数字上屏走**点阵**(`FONT` 15 位掩码),不拼字符串。 -- -- 跑法 —— 手机:vml run examples/lua/whack.lua -- 跑法 —— 桌面:vmlcli Examples/lua/whack.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 MZ_CELL = 120 MZ_OX = 0 MZ_OY = 130 LANG = 0 MOLE = -1 MOLE_TICKS = 0 WAIT = 6 SCORE = 0 LIVES = 3 OVER = 0 FLASH = 0 SEED = 20267 FRAMES = 0 ALIVE = 1 T_SCORE = "得分" T_LIVES = "命" T_OVER = "结束,点一下再来" T_HINT = "冒头就点它" function idiv(a, b) 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 whack_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 -- 3x3 地鼠格按宽、高两个上界取小者;大屏上封顶,免得格子大到离谱 -- The 3x3 grid takes the smaller of the width/height bounds; capped on large screens. local side = MZ_SW - 24 if MZ_SH - 94 < side then side = MZ_SH - 94 end MZ_CELL = idiv(side, 3) if MZ_CELL < 8 then MZ_CELL = 8 end if MZ_CELL > 160 then MZ_CELL = 160 end MZ_OX = idiv(MZ_SW - 3 * MZ_CELL, 2) MZ_OY = 64 + idiv(MZ_SH - 94 - 3 * MZ_CELL, 2) end function new_game() SCORE = 0 LIVES = 3 OVER = 0 FLASH = 0 MOLE = -1 WAIT = 6 FRAMES = 0 end function draw() ui_2d_clear(-15724520) local i = 0 while i < 9 do local cx = MZ_OX + (i % 3) * MZ_CELL + idiv(MZ_CELL, 2) local cy = MZ_OY + idiv(i, 3) * MZ_CELL + idiv(MZ_CELL, 2) local r = idiv(MZ_CELL, 3) ui_2d_ellipse(cx, cy + idiv(r, 3), r + idiv(MZ_CELL, 12), idiv(r, 2) + idiv(MZ_CELL, 20), -15066588, 1, 0) if i == MOLE then if FLASH == 1 then ui_2d_circle(cx, cy, r, -1, 1, 0) else ui_2d_circle(cx, cy, r, -2509750, 1, 0) end ui_2d_circle(cx - idiv(r, 3), cy - idiv(r, 4), idiv(r, 5), -15724520, 1, 0) ui_2d_circle(cx + idiv(r, 3), cy - idiv(r, 4), idiv(r, 5), -15724520, 1, 0) end i = i + 1 end draw_number(10, 12, SCORE, 4, -1513232) ui_2d_text(10, 48, T_SCORE, -6643536, 10, VML_ANCHOR_LEFT) draw_number(MZ_SW - 30, 12, LIVES, 4, -2069424) ui_2d_text(MZ_SW - 10, 48, T_LIVES, -6643536, 10, VML_ANCHOR_RIGHT) if OVER == 1 then ui_2d_text(idiv(MZ_SW, 2), MZ_OY + 3 * MZ_CELL + 18, T_OVER, -1513232, 16, VML_ANCHOR_CENTER) else ui_2d_text(idiv(MZ_SW, 2), MZ_OY + 3 * MZ_CELL + 18, T_HINT, -6643536, 12, VML_ANCHOR_CENTER) end ui_2d_present() end function main() LANG = ui_scr_get_language() ui_win_open("打地鼠", 0, 0) relayout() SEED = 20267 new_game() draw() TID = ui_timer_set(50, 1) while ALIVE == 1 do local t = ui_msg_fetch_wait(50) if t == 10 then ALIVE = 0 end if t == 9 then FRAMES = FRAMES + 1 if FRAMES >= 6000 then ALIVE = 0 end if OVER == 0 then FLASH = 0 if MOLE >= 0 then MOLE_TICKS = MOLE_TICKS - 1 if MOLE_TICKS <= 0 then MOLE = -1 LIVES = LIVES - 1 WAIT = 8 if LIVES <= 0 then OVER = 1 end end else WAIT = WAIT - 1 if WAIT <= 0 then MOLE = whack_rnd(9) MOLE_TICKS = 14 end end end end if t == 6 or t == 4 then local tx = ui_msg_get_a() local ty = ui_msg_get_b() if OVER == 1 then new_game() else local x = idiv(tx - MZ_OX, MZ_CELL) local y = idiv(ty - MZ_OY, MZ_CELL) if tx >= MZ_OX and ty >= MZ_OY then if x >= 0 and y >= 0 and x < 3 and y < 3 then local idx = y * 3 + x if idx == MOLE then SCORE = SCORE + 1 MOLE = -1 WAIT = 4 + whack_rnd(4) FLASH = 1 end end end end end if t == 1 then local k = ui_msg_get_a() if k == 27 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()