pascal/app/maze.pas · 4826 字节 · 下载原文
(* 迷宫 —— 用 **Pascal** 写的手机小游戏(探索)
Maze -- a small phone game written in **Pascal** (exploration)
玩法:方向键 / 点屏幕四边移动,走到右下角红格即通关;ESC 重开。
用**公式化蛇形迷宫**(偶数行整行通道、奇数行交替在左右端开口)。
跑法 —— 手机:vml run examples/pascal/maze.pas
跑法 —— 桌面:vmlcli Examples/pascal/maze.pas --screen 480x640 --frames /tmp/fr
*)
program Maze;
uses vml_ui;
var
w, h, cell, ox, oy, px, py, won, steps, frames: integer;
x, y, band, wall, nx, ny, tx, ty: integer;
dl, dr, dt, db: integer;
msg, ax, ay, key, tid: integer;
topH, footH, statusY, hdrY, fontSmall: integer;
s: string;
function IsWall(cx, cy: integer): boolean;
var
b: integer;
begin
if (cx < 0) or (cy < 0) or (cx > 20) or (cy > 20) then
begin
IsWall := true;
end
else if (cy = 0) or (cy = 20) then
begin
IsWall := false;
end
else if (cy mod 2) = 0 then
begin
IsWall := false;
end
else
begin
b := cy div 2;
if (b mod 2) = 0 then
IsWall := (cx <> 20)
else
IsWall := (cx <> 0);
end;
end;
begin
ui_win_open('迷宫', 0, 0);
ui_scr_set_keep_on(1);
w := ui_scr_get_w();
h := ui_scr_get_h();
if w <= 0 then
w := 360;
if h <= 0 then
h := 620;
{ 版面全部由 scr_w / scr_h 现算:顶部步数区 + 21×21 迷宫 + 底部提示 }
topH := h div 7;
if topH < 72 then
topH := 72;
if topH > 130 then
topH := 130;
footH := 26;
cell := (w - 16) div 21;
if (h - topH - footH) div 21 < cell then
cell := (h - topH - footH) div 21;
if cell < 3 then
cell := 3;
ox := (w - 21 * cell) div 2;
oy := topH;
hdrY := topH div 3;
fontSmall := topH div 6;
if fontSmall < 10 then
fontSmall := 10;
if fontSmall > 14 then
fontSmall := 14;
statusY := oy + 21 * cell + fontSmall + 14;
if statusY > h - 8 then
statusY := h - 8;
px := 1;
py := 0;
won := 0;
steps := 0;
frames := 0;
tid := ui_timer_set(120, 0);
while frames < 12000 do
begin
ui_2d_clear($FF101018);
x := 0;
while x < 21 do
begin
y := 0;
while y < 21 do
begin
if IsWall(x, y) then
ui_2d_rect(ox + x * cell, oy + y * cell, cell, cell, $FF1A1A22, 1, 0, 0)
else
ui_2d_rect(ox + x * cell, oy + y * cell, cell, cell, $FF2A2A32, 1, 0, 0);
y := y + 1;
end;
x := x + 1;
end;
ui_2d_rect(ox + 19 * cell + 1, oy + 20 * cell + 1, cell - 2, cell - 2, $FFE05A30, 1, 0, 2);
ui_2d_rect(ox + px * cell + 1, oy + py * cell + 1, cell - 2, cell - 2, $FF66BB6A, 1, 0, 2);
Str(steps, s);
ui_2d_text(10, hdrY, s, $FFFFFFFF, fontSmall + 6, 0);
ui_2d_text(10, hdrY + fontSmall + 6, '步数', $FF8A9AA8, fontSmall, 0);
if won = 1 then
ui_2d_text(w div 2, statusY, '走出来了!点一下重开', $FF66BB6A, fontSmall + 2, 1)
else
ui_2d_text(w div 2, statusY, '方向键 / 点屏幕四边', $FF8A9AA8, fontSmall, 1);
ui_2d_present();
msg := ui_msg_fetch_wait(0);
if msg = 10 then
frames := 12000;
if msg = 1 then
begin
key := ui_msg_get_a();
if key = 27 then
begin
px := 1;
py := 0;
won := 0;
steps := 0;
end;
nx := px;
ny := py;
if key = 37 then
nx := px - 1;
if key = 39 then
nx := px + 1;
if key = 38 then
ny := py - 1;
if key = 40 then
ny := py + 1;
if (won = 0) and ((nx <> px) or (ny <> py)) then
begin
if not IsWall(nx, ny) then
begin
px := nx;
py := ny;
steps := steps + 1;
if (px = 19) and (py = 20) then
won := 1;
end;
end;
end;
if msg = 6 then
begin
ax := ui_msg_get_a();
ay := ui_msg_get_b();
if won = 1 then
begin
px := 1;
py := 0;
won := 0;
steps := 0;
end
else
begin
dl := ax;
dr := w - ax;
dt := ay;
db := h - ay;
nx := px;
ny := py;
if (dl <= dr) and (dl <= dt) and (dl <= db) then
nx := px - 1;
if (dl > dr) and (dr <= dt) and (dr <= db) then
nx := px + 1;
if (dl > dr) and (dr > dt) and (dt <= db) then
ny := py - 1;
if (dl > dr) and (dr > dt) and (dt > db) then
ny := py + 1;
if not IsWall(nx, ny) then
begin
px := nx;
py := ny;
steps := steps + 1;
if (px = 19) and (py = 20) then
won := 1;
end;
end;
end;
if msg = 9 then
frames := frames + 1;
end;
ui_timer_kill(tid);
ui_scr_set_keep_on(0);
ui_win_close();
end.