← 都来码 / Dolaima← 例程索引ada/app/maze.adb▶ 在浏览器里试玩 原文

ada/app/maze.adb · 7474 字节 · 下载原文

▶ 在浏览器里试玩

-- 迷宫 —— 用 **Ada** 写的手机小游戏(探索)
-- Maze -- a small phone game written in **Ada** (exploration)
--
-- 玩法(与 Examples/c/app/maze.c / Examples/python/app/maze.py 同一套的目标):
--   方向键 / 点屏幕四边移动,走到右下角红格即通关;ESC / 通关后点一下重开。
--
-- 这一份用**公式化蛇形迷宫**(偶数行整行通道、奇数行交替在左右端开口),一定连通、一定有路。
--
-- ## 本语言的实测约束(见 Examples/ada/app/stopwatch.adb 文件头)
--
-- 跑法 —— 手机:vml run examples/ada/maze.adb
-- 跑法 —— 桌面:vmlcli Examples/ada/maze.adb --screen 480x640 --frames /tmp/fr

with VML_UI;

procedure Maze is
   K : Integer := 0;
   W : Integer := 360;
   H : Integer := 620;
   Cell : Integer := 20;
   CellW : Integer := 0;
   CellH : Integer := 0;
   Top : Integer := 64;
   Bot : Integer := 22;
   Ox : Integer := 10;
   Oy : Integer := 110;
   Hfs : Integer := 11;
   Wfs : Integer := 15;
   Px : Integer := 1;
   Py : Integer := 0;
   Won : Integer := 0;
   Steps : Integer := 0;
   Frames : Integer := 0;
   I : Integer := 0;
   X : Integer := 0;
   Y : Integer := 0;
   Band : Integer := 0;
   Wall : Integer := 0;
   T : Integer := 0;
   Tid : Integer := 0;
   Tx : Integer := 0;
   Ty : Integer := 0;
   Dl : Integer := 0;
   Dr : Integer := 0;
   Dt : Integer := 0;
   Db : Integer := 0;
   Nx : Integer := 0;
   Ny : 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 < 64 then
      Top := 64;
   end if;
   if Top > 100 then
      Top := 100;
   end if;
   Bot := H / 14;
   if Bot < 22 then
      Bot := 22;
   end if;
   if Bot > 64 then
      Bot := 64;
   end if;
   CellW := (W - 16) / 21;
   CellH := (H - Top - Bot - 8) / 21;
   Cell := CellW;
   if CellH < Cell then
      Cell := CellH;
   end if;
   if Cell < 3 then
      Cell := 3;
   end if;
   Ox := (W - 21 * Cell) / 2;
   Oy := Top + (H - Top - Bot - 21 * Cell) / 2;
   Hfs := H / 48;
   if Hfs < 9 then
      Hfs := 9;
   end if;
   if Hfs > 13 then
      Hfs := 13;
   end if;
   Wfs := H / 40;
   if Wfs < 12 then
      Wfs := 12;
   end if;
   if Wfs > 18 then
      Wfs := 18;
   end if;

   Tid := ui_timer_set(120, 0);

   while ui_win_is_closed() = 0 loop
      K := ui_2d_clear(-15724520);
      X := 0;
      while X < 21 loop
         Y := 0;
         while Y < 21 loop
            Wall := 1;
            if Y mod 2 = 0 then
               Wall := 0;
            else
               Band := Y / 2;
               if Band mod 2 = 0 then
                  if X = 20 then
                     Wall := 0;
                  end if;
               else
                  if X = 0 then
                     Wall := 0;
                  end if;
               end if;
            end if;
            if Wall = 1 then
               K := ui_2d_rect(Ox + X * Cell, Oy + Y * Cell, Cell, Cell, -14013898, 1, 0, 0);
            else
               K := ui_2d_rect(Ox + X * Cell, Oy + Y * Cell, Cell, Cell, -15066588, 1, 0, 0);
            end if;
            Y := Y + 1;
         end loop;
         X := X + 1;
      end loop;
      K := ui_2d_rect(Ox + 19 * Cell + 1, Oy + 20 * Cell + 1, Cell - 2, Cell - 2, -2069424, 1, 0, 2);
      K := ui_2d_rect(Ox + Px * Cell + 1, Oy + Py * Cell + 1, Cell - 2, Cell - 2, -11409298, 1, 0, 2);

      K := ui_2d_text(10, 20, Integer'Image(Steps), -1513232, 15, 0);
      if Lang = 0 then
         K := ui_2d_text(10, 46, "步数", -6643536, 10, 0);
      else
         K := ui_2d_text(10, 46, "steps", -6643536, 10, 0);
      end if;

      if Won = 1 then
         if Lang = 0 then
            K := ui_2d_text(W / 2, H - Bot + 2, "走出来了!点一下 / ESC 换一张", -11409298, Wfs, 1);
         else
            K := ui_2d_text(W / 2, H - Bot + 2, "Out! Tap / ESC for a new maze", -11409298, Wfs, 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 = 1 then
         I := ui_msg_get_a();
         if I = 27 then
            Px := 1;
            Py := 0;
            Won := 0;
            Steps := 0;
         end if;
         Nx := Px;
         Ny := Py;
         if I = 37 then
            Nx := Px - 1;
         elsif I = 39 then
            Nx := Px + 1;
         elsif I = 38 then
            Ny := Py - 1;
         elsif I = 40 then
            Ny := Py + 1;
         end if;
         if Won = 0 and (Nx /= Px or Ny /= Py) then
            Wall := 1;
            if Nx >= 0 and Ny >= 0 and Nx < 21 and Ny < 21 then
               if Ny mod 2 = 0 then
                  Wall := 0;
               else
                  Band := Ny / 2;
                  if Band mod 2 = 0 then
                     if Nx = 20 then
                        Wall := 0;
                     end if;
                  else
                     if Nx = 0 then
                        Wall := 0;
                     end if;
                  end if;
               end if;
            end if;
            if Wall = 0 then
               Px := Nx;
               Py := Ny;
               Steps := Steps + 1;
               if Px = 19 and Py = 20 then
                  Won := 1;
               end if;
            end if;
         end if;
      end if;

      if T = 6 or T = 4 then
         Tx := ui_msg_get_a();
         Ty := ui_msg_get_b();
         if Won = 1 then
            Px := 1;
            Py := 0;
            Won := 0;
            Steps := 0;
         else
            Dl := Tx;
            Dr := W - Tx;
            Dt := Ty;
            Db := H - Ty;
            Nx := Px;
            Ny := Py;
            if Dl <= Dr and Dl <= Dt and Dl <= Db then
               Nx := Px - 1;
            end if;
            if Dl > Dr and Dr <= Dt and Dr <= Db then
               Nx := Px + 1;
            end if;
            if Dl > Dr and Dr > Dt and Dt <= Db then
               Ny := Py - 1;
            end if;
            if Dl > Dr and Dr > Dt and Dt > Db then
               Ny := Py + 1;
            end if;
            Wall := 1;
            if Nx >= 0 and Ny >= 0 and Nx < 21 and Ny < 21 then
               if Ny mod 2 = 0 then
                  Wall := 0;
               else
                  Band := Ny / 2;
                  if Band mod 2 = 0 then
                     if Nx = 20 then
                        Wall := 0;
                     end if;
                  else
                     if Nx = 0 then
                        Wall := 0;
                     end if;
                  end if;
               end if;
            end if;
            if Wall = 0 then
               Px := Nx;
               Py := Ny;
               Steps := Steps + 1;
               if Px = 19 and Py = 20 then
                  Won := 1;
               end if;
            end if;
         end if;
      end if;

      if T = 9 then
         Frames := Frames + 1;
         if Frames >= 12000 then
            exit;
         end if;
      end if;
   end loop;

   K := ui_timer_kill(Tid);
   K := ui_scr_set_keep_on(0);
   K := ui_win_close();
end Maze;