ada/app/memory.adb · 8133 字节 · 下载原文
-- 记忆翻牌 —— 用 **Ada** 写的手机小游戏
-- Memory matching -- a small phone game written in **Ada**
--
-- 玩法(与 Examples/c/app/memory.c / Examples/python/app/memory.py 同一套):
-- 4×4 共 8 对;点一张翻开,再点第二张;一样就留在明面,不一样 0.4 秒后扣回去;
-- 全部配对完成即通关;通关后点屏幕重开。
--
-- ## 本语言的实测约束(见 Examples/ada/app/stopwatch.adb 文件头)
-- · 库函数 `K := ui_...(…)`;数组 1 基;`Integer'Image` 带一个前导空格;
-- · `and/or/not`,不等 `/=`;`while … loop … end loop;`
--
-- 跑法 —— 手机:vml run examples/ada/memory.adb
-- 跑法 —— 桌面:vmlcli Examples/ada/memory.adb --screen 480x640 --frames /tmp/fr
with VML_UI;
procedure Memory is
type Pair_Arr is array (1 .. 16) of Integer;
type Up_Arr is array (1 .. 16) of Integer;
Pair : Pair_Arr;
Up : Up_Arr;
K : Integer := 0;
W : Integer := 0;
H : Integer := 0;
Cell : Integer := 80;
CellW : Integer := 0;
CellH : Integer := 0;
Top : Integer := 56;
Bot : Integer := 22;
Ox : Integer := 20;
Oy : Integer := 110;
Dfs : Integer := 20;
Hfs : Integer := 12;
Seed : Integer := 20265;
Firstx : Integer := -1;
Firsty : Integer := -1;
Hide : Integer := 0;
Moves : Integer := 0;
Matched : Integer := 0;
Won : Integer := 0;
Frames : Integer := 0;
I : Integer := 0;
J : Integer := 0;
T : Integer := 0;
Tid : Integer := 0;
X : Integer := 0;
Y : Integer := 0;
Tx : Integer := 0;
Ty : Integer := 0;
Idx : Integer := 0;
Tmp : Integer := 0;
Lang : Integer := 0;
begin
Lang := ui_scr_get_language();
K := ui_win_open(0, 0, 0);
K := ui_scr_set_keep_on(1);
W := ui_scr_get_w();
H := ui_scr_get_h();
if W <= 0 then
W := 360;
end if;
if H <= 0 then
H := 620;
end if;
Top := H / 12;
if Top < 56 then
Top := 56;
end if;
if Top > 96 then
Top := 96;
end if;
Bot := H / 14;
if Bot < 22 then
Bot := 22;
end if;
if Bot > 64 then
Bot := 64;
end if;
CellW := (W - 16) / 4;
CellH := (H - Top - Bot - 8) / 4;
Cell := CellW;
if CellH < Cell then
Cell := CellH;
end if;
if Cell < 6 then
Cell := 6;
end if;
Ox := (W - 4 * Cell) / 2;
Oy := Top + (H - Top - Bot - 4 * Cell) / 2;
Dfs := Cell / 2;
if Dfs < 8 then
Dfs := 8;
end if;
if Dfs > 36 then
Dfs := 36;
end if;
Hfs := H / 48;
if Hfs < 9 then
Hfs := 9;
end if;
if Hfs > 13 then
Hfs := 13;
end if;
I := 1;
while I <= 16 loop
Pair(I) := (I - 1) / 2;
Up(I) := 0;
I := I + 1;
end loop;
I := 16;
while I > 1 loop
Seed := (Seed * 1103 + 12345) mod 32768;
J := (Seed / 256) mod I + 1;
Tmp := Pair(I);
Pair(I) := Pair(J);
Pair(J) := Tmp;
I := I - 1;
end loop;
Tid := ui_timer_set(50, 0);
while ui_win_is_closed() = 0 loop
K := ui_2d_clear(-15724520);
X := 0;
while X < 4 loop
Y := 0;
while Y < 4 loop
Idx := X * 4 + Y + 1;
if Pair(Idx) < 0 then
K := ui_2d_rect(Ox + X * Cell + 3, Oy + Y * Cell + 3, Cell - 6, Cell - 6, -15329762, 1, 0, 6);
elsif Up(Idx) = 1 then
if Pair(Idx) = 0 then
K := ui_2d_rect(Ox + X * Cell + 3, Oy + Y * Cell + 3, Cell - 6, Cell - 6, -2069424, 1, 0, 8);
elsif Pair(Idx) = 1 then
K := ui_2d_rect(Ox + X * Cell + 3, Oy + Y * Cell + 3, Cell - 6, Cell - 6, -2509750, 1, 0, 8);
elsif Pair(Idx) = 2 then
K := ui_2d_rect(Ox + X * Cell + 3, Oy + Y * Cell + 3, Cell - 6, Cell - 6, -11409298, 1, 0, 8);
elsif Pair(Idx) = 3 then
K := ui_2d_rect(Ox + X * Cell + 3, Oy + Y * Cell + 3, Cell - 6, Cell - 6, -11890471, 1, 0, 8);
elsif Pair(Idx) = 4 then
K := ui_2d_rect(Ox + X * Cell + 3, Oy + Y * Cell + 3, Cell - 6, Cell - 6, -6591248, 1, 0, 8);
elsif Pair(Idx) = 5 then
K := ui_2d_rect(Ox + X * Cell + 3, Oy + Y * Cell + 3, Cell - 6, Cell - 6, -2069336, 1, 0, 8);
elsif Pair(Idx) = 6 then
K := ui_2d_rect(Ox + X * Cell + 3, Oy + Y * Cell + 3, Cell - 6, Cell - 6, -11871802, 1, 0, 8);
else
K := ui_2d_rect(Ox + X * Cell + 3, Oy + Y * Cell + 3, Cell - 6, Cell - 6, -4145088, 1, 0, 8);
end if;
K := ui_2d_text(Ox + X * Cell + Cell / 2, Oy + Y * Cell + (Cell - Dfs) / 2,
Integer'Image(Pair(Idx) + 1), -15724520, Dfs, 1);
else
K := ui_2d_rect(Ox + X * Cell + 3, Oy + Y * Cell + 3, Cell - 6, Cell - 6, -14013898, 1, 0, 8);
end if;
Y := Y + 1;
end loop;
X := X + 1;
end loop;
K := ui_2d_text(10, 20, Integer'Image(Moves), -1513232, 15, 0);
if Lang = 0 then
K := ui_2d_text(10, 42, "步数", -6643536, 10, 0);
else
K := ui_2d_text(10, 42, "moves", -6643536, 10, 0);
end if;
if Won = 1 then
if Lang = 0 then
K := ui_2d_text(W / 2, H - Bot + 2, "全配好了!点一下再来", -1513232, Hfs + 2, 1);
else
K := ui_2d_text(W / 2, H - Bot + 2, "All matched! Tap for a new one", -1513232, Hfs + 2, 1);
end if;
else
K := ui_2d_text(W / 2, H - Bot + 2, "翻两张一样的", -6643536, Hfs, 1);
end if;
K := ui_2d_present();
T := ui_msg_fetch_wait(0);
if T = 10 then
exit;
end if;
if T = 6 or T = 4 then
Tx := ui_msg_get_a();
Ty := ui_msg_get_b();
if Won = 1 then
Seed := 20265;
I := 1;
while I <= 16 loop
Pair(I) := (I - 1) / 2;
Up(I) := 0;
I := I + 1;
end loop;
I := 16;
while I > 1 loop
Seed := (Seed * 1103 + 12345) mod 32768;
J := (Seed / 256) mod I + 1;
Tmp := Pair(I);
Pair(I) := Pair(J);
Pair(J) := Tmp;
I := I - 1;
end loop;
Firstx := -1;
Hide := 0;
Moves := 0;
Matched := 0;
Won := 0;
elsif Tx >= Ox and Ty >= Oy then
X := (Tx - Ox) / Cell;
Y := (Ty - Oy) / Cell;
if X >= 0 and Y >= 0 and X < 4 and Y < 4 then
Idx := X * 4 + Y + 1;
if Pair(Idx) >= 0 and Up(Idx) = 0 and Hide = 0 then
Up(Idx) := 1;
if Firstx < 0 then
Firstx := X;
Firsty := Y;
else
Moves := Moves + 1;
J := Firstx * 4 + Firsty + 1;
if Pair(J) = Pair(Idx) then
Pair(J) := -1;
Pair(Idx) := -1;
Matched := Matched + 2;
if Matched >= 16 then
Won := 1;
end if;
Firstx := -1;
else
Hide := 8;
end if;
end if;
end if;
end if;
end if;
end if;
if T = 1 then
if ui_msg_get_a() = 27 then
exit;
end if;
end if;
if T = 9 then
Frames := Frames + 1;
if Frames >= 8000 then
exit;
end if;
if Hide > 0 then
Hide := Hide - 1;
if Hide = 0 then
I := 1;
while I <= 16 loop
Up(I) := 0;
I := I + 1;
end loop;
Firstx := -1;
end if;
end if;
end if;
end loop;
K := ui_timer_kill(Tid);
K := ui_scr_set_keep_on(0);
K := ui_win_close();
end Memory;