php/app/memory.php · 5233 字节 · 下载原文
<?php
// 翻牌配对 —— 用 **PHP** 写的手机小游戏(记忆)
// Memory -- a small phone game written in **PHP** (matching pairs)
//
// 玩法:4×4 共 16 张牌、8 对颜色;同色就留着,不同色过一会儿翻回去;
// 全配对即通关,步数计分。ESC 重洗。
//
// 跑法 —— 手机:vml run examples/php/memory.php
// 跑法 —— 桌面:vmlcli Examples/php/memory.php --screen 480x640 --frames /tmp/fr
$card = array(); $rev = array(); $don = array();
$pal = array(-2073552, -2516442, -10044566, -11890471, -6587448, -16728155, -2265265, -2961710);
function shuffleCards() {
global $card, $rev, $don, $frames;
for ($k = 0; $k < 16; $k++) {
$card[$k] = intdiv($k, 2) + 1;
$rev[$k] = 0;
$don[$k] = 0;
}
$seed = 1234 + $frames;
for ($k = 0; $k < 300; $k++) {
$seed = ($seed + 7919) % 32768;
$a = intdiv($seed, 2048);
$seed = ($seed + 7919) % 32768;
$b = intdiv($seed, 2048);
$tt = $card[$a];
$card[$a] = $card[$b];
$card[$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, 6);
if ($gap < 2) { $gap = 2; }
$half = intdiv($gap, 2);
$frames = 0;
$first = -1;
$second = -1;
$pend = 0;
$tick = 0;
$pairs = 0;
$moves = 0;
$won = 0;
shuffleCards();
$tid = ui_timer_set(120, 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 + $half;
$ty = $oy + $r * $cell + $half;
if ($don[$idx] == 1 || $rev[$idx] == 1) {
$k = ui_2d_rect($tx, $ty, $cell - $gap, $cell - $gap, $pal[$card[$idx] - 1], 1, 0, 6);
} else {
$k = ui_2d_rect($tx, $ty, $cell - $gap, $cell - $gap, -14013902, 1, 0, 6);
}
}
$k = ui_2d_text($ox, $hudY, "" . $moves, -1513232, $fs, 0);
$k = ui_2d_text($ox, $labelY, "翻牌次数", -6643536, $fl, 0);
$k = ui_2d_text($w - $pad, $hudY, "" . $pairs, -10044566, $fs, 2);
$k = ui_2d_text($w - $pad, $labelY, "已配对", -6643536, $fl, 2);
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 ($pend == 0 && $won == 0 && $ax >= $ox && $ay >= $oy) {
$c = intdiv($ax - $ox, $cell);
$r = intdiv($ay - $oy, $cell);
if ($c < 4 && $r < 4) {
$idx = $r * 4 + $c;
if ($don[$idx] == 0 && $rev[$idx] == 0) {
$rev[$idx] = 1;
$moves = $moves + 1;
if ($first == -1) {
$first = $idx;
} else {
$second = $idx;
if ($card[$idx] == $card[$first]) {
$don[$idx] = 1;
$don[$first] = 1;
$pairs = $pairs + 1;
$first = -1;
$second = -1;
if ($pairs >= 8) { $won = 1; }
} else {
$pend = 1;
$tick = 0;
}
}
}
}
}
}
if ($msg == 1) {
$key = ui_msg_get_a();
if ($key == 27) { shuffleCards(); $first = -1; $second = -1; $pairs = 0; $moves = 0; $won = 0; }
}
if ($msg == 9) {
$frames = $frames + 1;
if ($pend == 1) {
$tick = $tick + 1;
if ($tick >= 6) {
$pend = 0;
$tick = 0;
$rev[$first] = 0;
$rev[$second] = 0;
$first = -1;
$second = -1;
}
}
}
}
ui_timer_kill($tid);
ui_scr_set_keep_on(0);
$k = ui_win_close();
echo "memory done", chr(10);
?>