php/app/whack.php · 4028 字节 · 下载原文
<?php
// 打地鼠 —— 用 **PHP** 写的手机小游戏
// Whack-a-mole -- a small phone game written in **PHP**
//
// 玩法:3×3 的洞里随机冒地鼠,30 秒内点得越多分越高;点空扣分。
//
// 跑法 —— 手机:vml run examples/php/whack.php
// 跑法 —— 桌面:vmlcli Examples/php/whack.php --screen 480x640 --frames /tmp/fr
$hole = array();
function newMole() {
global $hole;
for ($k = 0; $k < 9; $k++) { $hole[$k] = 0; }
$hole[ui_math_rand(9)] = 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; }
// 布局随画布缩放:字号跟着短边,格子从页脚以上的剩余高度反算。
// 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 * 26, 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, 3);
$cellh = intdiv($footY - $pad - $oy, 3);
if ($cellh < $cell) { $cell = $cellh; }
if ($cell < 12) { $cell = 12; }
$ox = intdiv($w - $cell * 3, 2);
$inset = intdiv($cell, 10);
if ($inset < 1) { $inset = 1; }
$score = 0;
$ticks = 0;
$move = 0;
$over = 0;
$frames = 0;
newMole();
$tid = ui_timer_set(100, 0);
while ($frames < 30000) {
$k = ui_2d_clear(-15724520);
for ($i = 0; $i < 9; $i++) {
$r = intdiv($i, 3);
$c = $i - $r * 3;
$tx = $ox + $c * $cell + $inset;
$ty = $oy + $r * $cell + $inset;
$sz = $cell - $inset * 2;
if ($hole[$i] == 1) {
$k = ui_2d_rect($tx, $ty, $sz, $sz, -13730510, 1, 0, 10);
$k = ui_2d_circle($tx + intdiv($sz, 2), $ty + intdiv($sz, 2), intdiv($sz, 4), -2516442, 1, 0);
} else {
$k = ui_2d_rect($tx, $ty, $sz, $sz, -14408660, 1, 0, 10);
}
}
$k = ui_2d_text($ox, $hudY, "" . $score, -1513232, $fs, 0);
$k = ui_2d_text($ox, $labelY, "得分", -6643536, $fl, 0);
$rem = 300 - $ticks;
if ($rem < 0) { $rem = 0; }
$k = ui_2d_text($w - $pad, $hudY, "" . intdiv($rem, 10), -10044566, $fs, 2);
$k = ui_2d_text($w - $pad, $labelY, "剩余秒", -6643536, $fl, 2);
if ($over == 1) {
$k = ui_2d_text(intdiv($w, 2), $hintY, "时间到!ESC 再来", -2073552, $fmsg, 1);
} else {
$k = ui_2d_text($ox, $hintY, "点冒头的绿色地鼠", -6643536, $fl, 0);
}
$k = ui_2d_text(intdiv($w, 2), $footY, "30 秒计分 · 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 ($over == 0 && $ax >= $ox && $ay >= $oy) {
$c = intdiv($ax - $ox, $cell);
$r = intdiv($ay - $oy, $cell);
if ($c < 3 && $r < 3) {
$j = $r * 3 + $c;
if ($hole[$j] == 1) {
$score = $score + 10;
$hole[$j] = 0;
newMole();
} else {
$score = $score - 5;
}
}
}
}
if ($msg == 1) {
$key = ui_msg_get_a();
if ($key == 27) { $score = 0; $ticks = 0; $over = 0; $move = 0; newMole(); }
}
if ($msg == 9) {
$frames = $frames + 1;
if ($over == 0) {
$ticks = $ticks + 1;
$move = $move + 1;
if ($move >= 6) { $move = 0; newMole(); }
if ($ticks >= 300) { $over = 1; }
}
}
}
ui_timer_kill($tid);
ui_scr_set_keep_on(0);
$k = ui_win_close();
echo "whack done", chr(10);
?>