php/app/maze.php · 4046 字节 · 下载原文
<?php
// 迷宫 —— 用 **PHP** 写的手机小游戏(探索)
// Maze -- a small phone game written in **PHP** (exploration)
//
// 玩法:方向键 / 点屏幕四边移动,走到右下角红格即通关;ESC 重开。
// 公式化蛇形迷宫(偶数行整行通道、奇数行交替在左右端开口)。
//
// 跑法 —— 手机:vml run examples/php/maze.php
// 跑法 —— 桌面:vmlcli Examples/php/maze.php --screen 480x640 --frames /tmp/fr
function isWall($cx, $cy) {
if ($cx < 0 || $cy < 0 || $cx > 20 || $cy > 20) { return 1; }
if ($cy == 0 || $cy == 20) { return 0; }
if ($cy % 2 == 0) { return 0; }
$b = intdiv($cy, 2);
if ($b % 2 == 0) {
if ($cx == 20) { return 0; }
} else {
if ($cx == 0) { return 0; }
}
return 1;
}
$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; }
$cell = intdiv($w - 16, 21);
if (intdiv($h - 170, 21) < $cell) { $cell = intdiv($h - 170, 21); }
if ($cell < 3) { $cell = 3; }
$ox = intdiv($w - 21 * $cell, 2);
$oy = 110;
$px = 1;
$py = 0;
$won = 0;
$steps = 0;
$frames = 0;
$tid = ui_timer_set(120, 0);
while ($frames < 12000) {
$k = ui_2d_clear(-15724520);
$x = 0;
while ($x < 21) {
$y = 0;
while ($y < 21) {
if (isWall($x, $y) == 1) {
$k = ui_2d_rect($ox + $x * $cell, $oy + $y * $cell, $cell, $cell, -15066590, 1, 0, 0);
} else {
$k = ui_2d_rect($ox + $x * $cell, $oy + $y * $cell, $cell, $cell, -14013902, 1, 0, 0);
}
$y = $y + 1;
}
$x = $x + 1;
}
$k = ui_2d_rect($ox + 19 * $cell + 1, $oy + 20 * $cell + 1, $cell - 2, $cell - 2, -2069424, 1, 0, 2);
$k = ui_2d_rect($ox + $px * $cell + 1, $oy + $py * $cell + 1, $cell - 2, $cell - 2, -10044566, 1, 0, 2);
$k = ui_2d_text(10, 40, "" . $steps, -1513232, 18, 0);
$k = ui_2d_text(10, 62, "步数", -6643536, 11, 0);
if ($won == 1) {
$k = ui_2d_text(intdiv($w, 2) - 110, $oy + 21 * $cell + 28, "走出来了!点一下重开", -10044566, 15, 0);
} else {
$k = ui_2d_text(intdiv($w, 2) - 100, $oy + 21 * $cell + 28, "方向键 / 点屏幕四边", -6643536, 12, 0);
}
$k = ui_2d_present();
$msg = ui_msg_fetch_wait(0);
if ($msg == 10) { $frames = 12000; }
if ($msg == 1) {
$key = ui_msg_get_a();
if ($key == 27) { $px = 1; $py = 0; $won = 0; $steps = 0; }
$nx = $px;
$ny = $py;
if ($key == 37) { $nx = $px - 1; }
if ($key == 39) { $nx = $px + 1; }
if ($key == 38) { $ny = $py - 1; }
if ($key == 40) { $ny = $py + 1; }
if ($won == 0) {
if ($nx != $px || $ny != $py) {
if (isWall($nx, $ny) == 0) {
$px = $nx;
$py = $ny;
$steps = $steps + 1;
if ($px == 19 && $py == 20) { $won = 1; }
}
}
}
}
if ($msg == 6) {
$ax = ui_msg_get_a();
$ay = ui_msg_get_b();
if ($won == 1) {
$px = 1; $py = 0; $won = 0; $steps = 0;
} else {
$dl = $ax;
$dr = $w - $ax;
$dt = $ay;
$db = $h - $ay;
$nx = $px;
$ny = $py;
if ($dl <= $dr && $dl <= $dt && $dl <= $db) { $nx = $px - 1; }
if ($dl > $dr && $dr <= $dt && $dr <= $db) { $nx = $px + 1; }
if ($dl > $dr && $dr > $dt && $dt <= $db) { $ny = $py - 1; }
if ($dl > $dr && $dr > $dt && $dt > $db) { $ny = $py + 1; }
if (isWall($nx, $ny) == 0) {
$px = $nx;
$py = $ny;
$steps = $steps + 1;
if ($px == 19 && $py == 20) { $won = 1; }
}
}
}
if ($msg == 9) { $frames = $frames + 1; }
}
ui_timer_kill($tid);
ui_scr_set_keep_on(0);
$k = ui_win_close();
echo "maze done", chr(10);
?>