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

php/app/puzzle15.php · 4529 字节 · 下载原文

▶ 在浏览器里试玩

<?php
// 数字华容道 —— 用 **PHP** 写的手机小游戏(4×4 滑块)
// Puzzle 15 -- a small phone game written in **PHP** (4x4 sliding)
//
// 玩法:点空位旁边的数字,它就滑进空位;把 1..15 排回顺序即通关。ESC 重排。
//
// 跑法 —— 手机:vml run examples/php/puzzle15.php
// 跑法 —— 桌面:vmlcli Examples/php/puzzle15.php --screen 480x640 --frames /tmp/fr

$tile = array();
function shuffleTiles() {
    global $tile;
    for ($k = 0; $k < 15; $k++) { $tile[$k] = $k + 1; }
    $tile[15] = 0;
    $seed = 1234;
    for ($k = 0; $k < 400; $k++) {
        $seed = ($seed + 7919) % 32768;
        $a = intdiv($seed, 2048);
        $seed = ($seed + 7919) % 32768;
        $b = intdiv($seed, 2048);
        $tt = $tile[$a];
        $tile[$a] = $tile[$b];
        $tile[$b] = $tt;
    }
}

$k = ui_win_open_ex("数字华容道", 0, 0, 0, 0);
ui_scr_set_keep_on(1);
$w = ui_scr_get_w();
$h = ui_scr_get_h();
if ($w <= 0) { $w = 360; }
if ($h <= 0) { $h = 620; }

// 布局随画布缩放:字号跟着短边,格子从页脚以上的剩余高度反算。
// Layout scales with the canvas: fonts follow the short side, cells fit above the footer.
$s = $w;
if ($h < $s) { $s = $h; }
$pad = intdiv($s, 24);
if ($pad < 4) { $pad = 4; }
if ($pad > 40) { $pad = 40; }
$fs = intdiv($s * 22, 360);
if ($fs < 10) { $fs = 10; }
$fl = intdiv($s * 12, 360);
if ($fl < 8) { $fl = 8; }
$fmsg = intdiv($s * 18, 360);
if ($fmsg < 10) { $fmsg = 10; }
$hudY = $fs + intdiv($s, 30);
$labelY = $hudY + $fl + intdiv($s, 40);
$hintY = $labelY + $fl + intdiv($s, 40);
$footY = $h - intdiv($s, 40);
$oy = $hintY + intdiv($s, 30);
$cell = intdiv($w - $pad * 2, 4);
$cellh = intdiv($footY - $pad - $oy, 4);
if ($cellh < $cell) { $cell = $cellh; }
if ($cell < 12) { $cell = 12; }
$ox = intdiv($w - $cell * 4, 2);
$gap = intdiv($cell, 7);
if ($gap < 2) { $gap = 2; }
$fc = intdiv($cell * 24, 100);
if ($fc < 8) { $fc = 8; }
$moves = 0;
$won = 0;
$frames = 0;
$e = 15;
shuffleTiles();
$tid = ui_timer_set(200, 0);

while ($frames < 30000) {
    $k = ui_2d_clear(-15724520);
    for ($idx = 0; $idx < 16; $idx++) {
        $r = intdiv($idx, 4);
        $c = $idx - $r * 4;
        $tx = $ox + $c * $cell;
        $ty = $oy + $r * $cell;
        if ($tile[$idx] == 0) {
            $k = ui_2d_rect($tx, $ty, $cell - $gap, $cell - $gap, -15724520, 1, 0, 4);
        } else {
            $k = ui_2d_rect($tx, $ty, $cell - $gap, $cell - $gap, -14013902, 1, 0, 4);
            $k = ui_2d_text($tx + intdiv($cell, 2), $ty + intdiv($cell, 2) + intdiv($fc, 10), "" . $tile[$idx], -1513232, $fc, 1);
        }
    }
    $k = ui_2d_text($ox, $hudY, "" . $moves, -1513232, $fs, 0);
    $k = ui_2d_text($ox, $labelY, "步数", -6643536, $fl, 0);
    if ($won == 1) {
        $k = ui_2d_text(intdiv($w, 2), $hintY, "排好了!ESC 再来", -10044566, $fmsg, 1);
    } else {
        $k = ui_2d_text($ox, $hintY, "点空位旁边的数字", -6643536, $fl, 0);
    }
    $k = ui_2d_text(intdiv($w, 2), $footY, "ESC 重排", -6643536, $fl, 1);
    $k = ui_2d_present();

    $msg = ui_msg_fetch_wait(0);
    if ($msg == 10) { $frames = 30000; }
    if ($msg == 6) {
        $ax = ui_msg_get_a();
        $ay = ui_msg_get_b();
        if ($ax >= $ox && $ay >= $oy) {
            $c = intdiv($ax - $ox, $cell);
            $r = intdiv($ay - $oy, $cell);
            if ($c < 4 && $r < 4) {
                if ($won == 0) {
                    $idx = $r * 4 + $c;
                    $doit = 0;
                    if ($idx - 4 == $e) { $doit = 1; }
                    if ($idx + 4 == $e) { $doit = 1; }
                    if ($idx - 1 == $e && $c > 0) { $doit = 1; }
                    if ($idx + 1 == $e && $c < 3) { $doit = 1; }
                    if ($doit == 1) {
                        $tmp = $tile[$idx];
                        $tile[$idx] = $tile[$e];
                        $tile[$e] = $tmp;
                        $e = $idx;
                        $moves = $moves + 1;
                        $won = 1;
                        for ($i = 0; $i < 15; $i++) {
                            if ($tile[$i] != $i + 1) { $won = 0; }
                        }
                    }
                }
            }
        }
    }
    if ($msg == 1) {
        $key = ui_msg_get_a();
        if ($key == 27) { shuffleTiles(); $moves = 0; $won = 0; $e = 15; }
    }
    if ($msg == 9) { $frames = $frames + 1; }
}

ui_timer_kill($tid);
ui_scr_set_keep_on(0);
$k = ui_win_close();
echo "puzzle15 done", chr(10);
?>