pascal/app/whack.pas · 4290 字节 · 下载原文
(* 打地鼠 —— 用 **Pascal** 写的手机小游戏
Whack-a-mole -- a small phone game written in **Pascal**
玩法:3×3 的洞里随机冒地鼠,30 秒内点得越多分越高;点空扣分。
跑法 —— 手机:vml run examples/pascal/whack.pas
跑法 —— 桌面:vmlcli Examples/pascal/whack.pas --screen 480x640 --frames /tmp/fr
*)
program Whack;
uses vml_ui;
var
hole: array[0..8] of integer;
w, h, cell, ox, oy, i, j, c, r, tick2, move2: integer;
hdrH, footH, inset, hdrY, fontBig, fontSmall: integer;
score, ticks, over, frames, msg, ax, ay, key, tid, tmp, tmp2: integer;
s: string;
procedure NewMole;
var
k: integer;
begin
for k := 0 to 8 do
hole[k] := 0;
hole[ui_math_rand(9)] := 1;
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 现算:顶部信息区 + 3×3 洞 + 底部提示 }
hdrH := h div 5;
if hdrH < 64 then
hdrH := 64;
if hdrH > 130 then
hdrH := 130;
footH := 30;
cell := (w - 24) div 3;
if (h - hdrH - footH) div 3 < cell then
cell := (h - hdrH - footH) div 3;
if cell < 16 then
cell := 16;
if cell > 330 then
cell := 330;
ox := (w - cell * 3) div 2;
oy := hdrH;
inset := cell div 8;
if inset < 4 then
inset := 4;
if inset > 14 then
inset := 14;
hdrY := hdrH div 3;
fontBig := cell div 4;
if fontBig < 16 then
fontBig := 16;
if fontBig > 50 then
fontBig := 50;
fontSmall := hdrH div 6;
if fontSmall < 10 then
fontSmall := 10;
if fontSmall > 16 then
fontSmall := 16;
score := 0;
ticks := 0;
move2 := 0;
over := 0;
frames := 0;
NewMole;
tid := ui_timer_set(100, 0);
while frames < 30000 do
begin
ui_2d_clear($FF101018);
for i := 0 to 8 do
begin
r := i div 3;
c := i - r * 3;
tmp := ox + c * cell + inset;
tmp2 := oy + r * cell + inset;
if hole[i] = 1 then
begin
ui_2d_rect(tmp, tmp2, cell - inset * 2, cell - inset * 2, $FF2E7D32, 1, 0, 10);
ui_2d_circle(tmp + (cell - inset * 2) div 2, tmp2 + (cell - inset * 2) div 2,
(cell - inset * 2) div 4, $FFE0A030, 1, 0);
end
else
ui_2d_rect(tmp, tmp2, cell - inset * 2, cell - inset * 2, $FF24242C, 1, 0, 10);
end;
Str(score, s);
ui_2d_text(ox, hdrY, s, $FFFFFFFF, fontBig, 0);
ui_2d_text(ox, hdrY + fontBig, '得分', $FF8A9AA8, fontSmall, 0);
tmp := 300 - ticks;
if tmp < 0 then
tmp := 0;
Str(tmp div 10, s);
ui_2d_text(w - ox, hdrY, s, $FF66BB6A, fontBig, 2);
ui_2d_text(w - ox, hdrY + fontBig, '剩余秒', $FF8A9AA8, fontSmall, 2);
if over = 1 then
ui_2d_text(w div 2, hdrH - 8, '时间到!ESC 再来', $FFE05A30, fontSmall + 2, 1)
else
ui_2d_text(w div 2, hdrH - 8, '点冒头的绿色地鼠', $FF8A9AA8, fontSmall, 1);
ui_2d_text(w div 2, h - 8, '30 秒计分 · ESC 重开', $FF8A9AA8, fontSmall, 1);
ui_2d_present();
msg := ui_msg_fetch_wait(0);
if msg = 10 then
frames := 30000;
if msg = 6 then
begin
ax := ui_msg_get_a();
ay := ui_msg_get_b();
if (over = 0) and (ax >= ox) and (ay >= oy) then
begin
c := (ax - ox) div cell;
r := (ay - oy) div cell;
if (c < 3) and (r < 3) then
begin
j := r * 3 + c;
if hole[j] = 1 then
begin
score := score + 10;
hole[j] := 0;
NewMole;
end
else
score := score - 5;
end;
end;
end;
if msg = 1 then
begin
key := ui_msg_get_a();
if key = 27 then
begin
score := 0;
ticks := 0;
over := 0;
move2 := 0;
NewMole;
end;
end;
if msg = 9 then
begin
frames := frames + 1;
if over = 0 then
begin
ticks := ticks + 1;
move2 := move2 + 1;
if move2 >= 6 then
begin
move2 := 0;
NewMole;
end;
if ticks >= 300 then
over := 1;
end;
end;
end;
ui_timer_kill(tid);
ui_scr_set_keep_on(0);
ui_win_close();
end.