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

lua/app/memory.lua · 9258 字节 · 下载原文

▶ 在浏览器里试玩

-- 记忆翻牌 —— 用 **Lua** 写的手机小游戏
-- Memory matching — a small phone game written in **Lua**
--
-- 玩法(与 Examples/c/app/memory.c / Examples/python/app/memory.py 同一套):
-- Gameplay (same rules as the C / Python versions):
--   · 4×4 共 8 对;点一张翻开,再点第二张;
--   · 一样就留在明面,不一样 **0.4 秒**后扣回去;全部配对完成即通关;
--   · 通关后点屏幕(或按 ESC / R)重开。
--
-- ## 这一份演示「延时动作」怎么写
-- ## This sample shows **how to write a delayed action**
--
-- 翻错的两张不能立刻扣回去(玩家会看不到第二张是什么),所以记一个**倒计时拍数**,
-- 主循环每拍减一,减到 0 才扣 —— 不另起线程、不睡眠。
--
-- ## 本语言的五条**实测约束**(见 Examples/lua/app/snake.lua 文件头)
-- ## Five **measured constraints** of this language (see the header of Examples/lua/app/snake.lua)
--
--   · **顶层 `local` 在函数里读不到** ⇒ 跨函数共用的状态**一律写成顶层全局**;
--   · **空表 `{}` 只有 8 个键位** ⇒ 先按字面量建表、再逐个填;
--   · **别写 `t[#t + 1] = v`**(内联 `#` 当下标);
--   · **`and` / `or` 求的是布尔 0/1** ⇒ 选择一律 `if/else`;
--   · `pairs` / `tostring` / `math.random` 没通;随机数走 `ui_math_rand(n)`;
--     数字上屏走**点阵**(`FONT` 15 位掩码),不拼字符串。
--
-- 跑法 —— 手机:vml run examples/lua/memory.lua
-- 跑法 —— 桌面:vmlcli Examples/lua/memory.lua --screen 480x640 --frames /tmp/fr

require("vml_ui")

-- ══════════════════════════════════════════════════════════════════════════
-- 顶层全局(⚠ 必须在**函数定义之前**赋值:Lua 前端的全局槽要在函数生成时就存在,
-- 写在下方的函数就读不到 —— 症状是 MZ_CELL/MZ_OX 读回 0、牌阵整片画不出来)
-- Top-level globals (⚠ assigned **before the function definitions**: the global slots must exist
-- when the functions are generated, otherwise they read back 0 and the board never draws)
-- ══════════════════════════════════════════════════════════════════════════
MZ_SW = 360
MZ_SH = 620
MZ_CELL = 80
MZ_OX = 20
MZ_OY = 110
MZ_SIDE = 0
SEED = 20265
FIRST_X = -1
FIRST_Y = -1
HIDE = 0
MOVES = 0
MATCHED = 0
WON = 0
ALIVE = 1
T_WON = "全配好了!点一下再来"
T_HINT = "翻两张一样的"
T_MOVES = "步数"
PAIR = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
UP = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}

-- 点阵数字:FONT[d+1] 是 15 位掩码(3 宽 × 5 高,位序 = 行*3+列)
FONT = {31599, 29850, 29671, 31207, 18925, 31183, 31695, 18727, 31727, 31215}

-- ⚠ 本前端**没有整数除法**:`/` 返回浮点,而 `ui_2d_*` 的参数是整数 —— 浮点会被按位型解释,
-- 症状是坐标错位、字符串指针偏移(实测 `ui_2d_text(SW / 2, ...)` 显示在 x=0 且文字少前三个字)。
-- ⇒ 所有坐标/尺寸/下标都用这个循环减法助手。
-- ⚠ No integer division here: `/` returns a float and `ui_2d_*` wants ints (the float is read as its
-- bit pattern: coordinates shift and string pointers go off by a few bytes). Use this helper instead.
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
BIT = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384}

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 pair_color(p)
    if p == 0 then return -2069424 end
    if p == 1 then return -2509750 end
    if p == 2 then return -11409298 end
    if p == 3 then return -11890471 end
    if p == 4 then return -6591248 end
    if p == 5 then return -2069336 end
    if p == 6 then return -11871802 end
    return -4145088
end

function mem_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
    -- 4x4 棋盘按宽、高两个上界取小者,并在剩余高度里垂直居中
    -- The 4x4 board takes the smaller of the width/height bounds and is centred vertically.
    MZ_SIDE = MZ_SW - 20
    if MZ_SH - 86 < MZ_SIDE then
        MZ_SIDE = MZ_SH - 86
    end
    MZ_CELL = idiv(MZ_SIDE, 4)
    if MZ_CELL < 6 then
        MZ_CELL = 6
    end
    MZ_OX = idiv(MZ_SW - 4 * MZ_CELL, 2)
    MZ_OY = 56 + idiv(MZ_SH - 86 - 4 * MZ_CELL, 2)
end

-- 开一局:0,0,1,1,…,7,7 再 Fisher–Yates 洗牌(自带随机源,可复现)
function new_game()
    local i = 0
    while i < 16 do
        PAIR[i + 1] = idiv(i, 2)
        UP[i + 1] = 0
        i = i + 1
    end
    i = 15
    while i > 0 do
        local j = mem_rnd(i + 1)
        local t = PAIR[i + 1]
        PAIR[i + 1] = PAIR[j + 1]
        PAIR[j + 1] = t
        i = i - 1
    end
    FIRST_X = -1
    FIRST_Y = -1
    HIDE = 0
    MOVES = 0
    MATCHED = 0
    WON = 0
end

function mem_tap(tx, ty)
    if tx < MZ_OX or ty < MZ_OY then
        return 0
    end
    local x = idiv(tx - MZ_OX, MZ_CELL)
    local y = idiv(ty - MZ_OY, MZ_CELL)
    if x < 0 or y < 0 or x >= 4 or y >= 4 then
        return 0
    end
    local i = x * 4 + y + 1
    if PAIR[i] < 0 or UP[i] == 1 or HIDE > 0 then
        return 0
    end
    UP[i] = 1
    if FIRST_X < 0 then
        FIRST_X = x
        FIRST_Y = y
        return 0
    end
    MOVES = MOVES + 1
    local fi = FIRST_X * 4 + FIRST_Y + 1
    if PAIR[fi] == PAIR[i] then
        PAIR[fi] = -1
        PAIR[i] = -1
        MATCHED = MATCHED + 2
        if MATCHED >= 16 then WON = 1 end
        FIRST_X = -1
    else
        HIDE = 8
    end
end

function draw()
    ui_2d_clear(-15724520)

    local x = 0
    while x < 4 do
        local y = 0
        while y < 4 do
            local px = MZ_OX + x * MZ_CELL
            local py = MZ_OY + y * MZ_CELL
            local i = x * 4 + y + 1
            if PAIR[i] < 0 then
                ui_2d_rect(px + 3, py + 3, MZ_CELL - 6, MZ_CELL - 6, -15329762, 1, 0, 6)
            elseif UP[i] == 1 then
                ui_2d_rect(px + 3, py + 3, MZ_CELL - 6, MZ_CELL - 6, pair_color(PAIR[i]), 1, 0, 8)
                draw_digit(px + idiv(MZ_CELL, 2) - idiv(MZ_CELL, 10), py + idiv(MZ_CELL, 2) - idiv(MZ_CELL, 6), PAIR[i] + 1, idiv(MZ_CELL, 10), -15724520)
            else
                ui_2d_rect(px + 3, py + 3, MZ_CELL - 6, MZ_CELL - 6, -14013898, 1, 0, 8)
            end
            y = y + 1
        end
        x = x + 1
    end

    draw_number(10, 12, MOVES, 4, -1513232)
    ui_2d_text(10, 42, T_MOVES, -6643536, 10, VML_ANCHOR_LEFT)

    if WON == 1 then
        ui_2d_text(idiv(MZ_SW, 2), MZ_OY + 4 * MZ_CELL + 18, T_WON, -1513232, 15, VML_ANCHOR_CENTER)
    else
        ui_2d_text(idiv(MZ_SW, 2), MZ_OY + 4 * 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()
new_game()

TID = ui_timer_set(50, 1)
FRAMES = 0

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 >= 8000 then ALIVE = 0 end
        if HIDE > 0 then
            HIDE = HIDE - 1
            if HIDE == 0 then
                local x = 0
                while x < 16 do
                    UP[x + 1] = 0
                    x = x + 1
                end
                FIRST_X = -1
            end
        end
    end

    if t == 6 or t == 4 then
        if WON == 1 then
            new_game()
        else
            mem_tap(ui_msg_get_a(), ui_msg_get_b())
        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()