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

python/app/whack.py · 6702 字节 · 下载原文

▶ 在浏览器里试玩

# whack.py —— 打地鼠(VML 的 Python 前端)
# whack.py -- whack-a-mole (VML's Python frontend)
#
# 与 `Examples/c/app/whack.c` **同一套玩法**:
# Same rules as `Examples/c/app/whack.c`:
#   · 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.
#
# 这一类游戏的关键是**"计时归谁管"**:地鼠的存活时间记在**拍数**上(mole_ticks),
# The key thing in this genre is **who owns the clock**: a mole's lifetime is counted in
# **ticks**, not in time() -- the timer is the heartbeat, so "how many ticks a mole stays"
# is computable and does not drift with the machine.
# 而不是 time();拍数与真实时间的关系由 ui_timer_set 定死。

# 显式引入共享库声明(GenLib 生成,勿手写)。
# Explicitly import the shared-library declarations (generated by GenLib, do not hand-write).
# ⚠ 注释行别以 `Import` / `Include` 开头 —— 前端会把整行当 `#include` 指令。
# ⚠ Never start a comment line with `Import` / `Include` -- it becomes a `#include` directive.
import vml_ui

MAX_FRAMES = 6000
GRID = 3
SHOW_TICKS = 14          # 一只地鼠活 14 拍 ≈ 0.7 秒(50ms 一拍)/ a mole lives 14 ticks ~= 0.7 s

C_BG = 0xFF101018
C_HOLE = 0xFF1A1A24
C_MOLE = 0xFFD9B44A
C_TEXT = 0xFFE8E8F0
C_DIM = 0xFF9AA0B0
C_MISS = 0xFFE06C50

SW = 360
SH = 620
CELL = 120
OX = 0
OY = 130
LANG = 0

mole_at = -1
mole_ticks = 0
wait_ticks = 6
score = 0
lives = 3
over = 0
hit_flash = 0
seed = 20267


def rnd(n):
    # 16 位 LCG(32 位整数下 C 的 32 位 LCG 会溢出、恒返回 0,见 game2048.py)。
    # 16-bit LCG (the C 32-bit LCG overflows with 32-bit ints and always returns 0; see game2048.py).
    global seed
    seed = (seed * 1103 + 12345) % 32768
    return (seed // 256) % n


def relayout():
    global SW, SH, CELL, OX, OY
    SW = ui_scr_get_w()
    SH = ui_scr_get_h()
    if SW <= 0:
        SW = 360
    if SH <= 0:
        SH = 620
    # 九宫格取「宽度」与「扣掉顶栏/底部提示后的高度」里较小的那个 ——
    # 方形小画布上原来的 `SW < SH - 150` 分支会让格子只剩 70px。
    # The 3x3 grid takes the smaller of "width" and "height after header/footer" --
    # the old `SW < SH - 150` branch left a 70px grid on a square tiny canvas.
    OY = 60
    side = SW - 16
    tall = SH - OY - 44
    if tall < side:
        side = tall
    CELL = side // GRID
    if CELL < 8:
        CELL = 8
    OX = (SW - GRID * CELL) // 2


def pick_hole():
    global mole_at, mole_ticks
    mole_at = rnd(GRID * GRID)
    mole_ticks = SHOW_TICKS


def new_game():
    global score, lives, over, hit_flash, mole_at, wait_ticks
    score = 0
    lives = 3
    over = 0
    hit_flash = 0
    mole_at = -1
    wait_ticks = 6


def draw():
    ui_2d_clear(C_BG)

    i = 0
    while i < GRID * GRID:
        cx = OX + (i % GRID) * CELL + CELL // 2
        cy = OY + (i // GRID) * CELL + CELL // 2
        r = CELL // 3
        ui_2d_ellipse(cx, cy + r // 3, r + CELL // 12, r // 2 + CELL // 20, C_HOLE, 1, 0)
        if i == mole_at:
            if hit_flash == 1:
                mc = 0xFFFFFFFF
            else:
                mc = C_MOLE
            ui_2d_circle(cx, cy, r, mc, 1, 0)
            ui_2d_circle(cx - r // 3, cy - r // 4, r // 5, 0xFF101018, 1, 0)
            ui_2d_circle(cx + r // 3, cy - r // 4, r // 5, 0xFF101018, 1, 0)
        i = i + 1

    ui_2d_text(10, 24, str(score), C_TEXT, 18, VML_ANCHOR_LEFT)
    if LANG == 0:
        ui_2d_text(10, 48, "得分", C_DIM, 10, VML_ANCHOR_LEFT)
    else:
        ui_2d_text(10, 48, "score", C_DIM, 10, VML_ANCHOR_LEFT)
    ui_2d_text(SW - 10, 24, str(lives), C_MISS, 18, VML_ANCHOR_RIGHT)
    if LANG == 0:
        ui_2d_text(SW - 10, 48, "命", C_DIM, 10, VML_ANCHOR_RIGHT)
    else:
        ui_2d_text(SW - 10, 48, "lives", C_DIM, 10, VML_ANCHOR_RIGHT)

    if over == 1:
        if LANG == 0:
            msg = "结束,点一下再来"
        else:
            msg = "Over - tap to retry"
        ui_2d_text(SW // 2, OY + GRID * CELL + 30, msg, C_TEXT, 16, VML_ANCHOR_CENTER)
    else:
        if LANG == 0:
            msg = "冒头就点它"
        else:
            msg = "Tap the mole when it pops"
        ui_2d_text(SW // 2, OY + GRID * CELL + 30, msg, C_DIM, 12, VML_ANCHOR_CENTER)
    ui_2d_present()


def main():
    global LANG, mole_at, mole_ticks, wait_ticks, score, lives, over, hit_flash
    LANG = ui_scr_get_language()

    relayout()
    ui_win_open_ex("打地鼠" if LANG == 0 else "Whack (py)", 0, 0, 1, 0)
    relayout()
    new_game()
    draw()

    tid = ui_timer_set(50, 1)
    frames = 0
    done = 0
    while done == 0 and ui_win_is_closed() == 0:
        t = ui_msg_fetch()
        if t == 0:
            t = ui_msg_fetch_wait(50)
        if t == 0:
            continue

        if t == 6 or t == 4:
            if over == 1:
                new_game()
                draw()
                continue
            x = (ui_msg_get_a() - OX) // CELL
            y = (ui_msg_get_b() - OY) // CELL
            if x >= 0 and y >= 0 and x < GRID and y < GRID:
                idx = y * GRID + x
                if idx == mole_at:
                    score = score + 1
                    mole_at = -1
                    wait_ticks = 4 + rnd(4)
                    hit_flash = 1
            draw()
            continue

        if t == 1:
            if ui_msg_get_a() == 27:
                new_game()
                draw()
            continue

        if t == 11:
            relayout()
            draw()
            continue

        if t == 9:
            frames = frames + 1
            if frames >= MAX_FRAMES:
                done = 1
            if over == 0:
                hit_flash = 0
                if mole_at >= 0:
                    mole_ticks = mole_ticks - 1
                    if mole_ticks <= 0:          # 没点中:扣命 / missed: lose a life
                        mole_at = -1
                        lives = lives - 1
                        wait_ticks = 8
                        if lives <= 0:
                            over = 1
                            done = 1
                else:
                    wait_ticks = wait_ticks - 1
                    if wait_ticks <= 0:
                        pick_hole()
            draw()
            continue

        if t == 10:
            break

    ui_timer_kill(tid)
    ui_win_close()
    return 0


main()