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

ada/app/whack.adb · 6049 字节 · 下载原文

▶ 在浏览器里试玩

-- 打地鼠 —— 用 **Ada** 写的手机小游戏
-- Whack-a-mole -- a small phone game written in **Ada**
--
-- 玩法(与 Examples/c/app/whack.c / Examples/python/app/whack.py 同一套):
--   3×3 九个洞,每 14 拍有一个洞冒出地鼠;在它缩回去之前点中 +1 分,
--   超时没点中扣一条命;三条命用完结束;结束后点屏幕重开。
--
-- 地鼠的存活时间记在**拍数**上,不是 time() —— 定时器就是心跳。
--
-- ## 本语言的实测约束(见 Examples/ada/app/stopwatch.adb 文件头)
--
-- 跑法 —— 手机:vml run examples/ada/whack.adb
-- 跑法 —— 桌面:vmlcli Examples/ada/whack.adb --screen 480x640 --frames /tmp/fr

with VML_UI;

procedure Whack is
   K : Integer := 0;
   W : Integer := 0;
   H : Integer := 0;
   Cell : Integer := 120;
   CellW : Integer := 0;
   CellH : Integer := 0;
   Top : Integer := 62;
   Bot : Integer := 22;
   Ox : Integer := 0;
   Oy : Integer := 130;
   Hfs : Integer := 12;
   Mole : Integer := -1;
   Molett : Integer := 0;
   Wait : Integer := 6;
   Score : Integer := 0;
   Lives : Integer := 3;
   Over : Integer := 0;
   Flash : Integer := 0;
   Frames : Integer := 0;
   Seed : Integer := 20267;
   I : Integer := 0;
   X : Integer := 0;
   Y : Integer := 0;
   Cx : Integer := 0;
   Cy : Integer := 0;
   R : Integer := 0;
   T : Integer := 0;
   Tid : Integer := 0;
   Tx : Integer := 0;
   Ty : Integer := 0;
   Idx : 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 < 62 then
      Top := 62;
   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 - 24) / 3;
   CellH := (H - Top - Bot - 8) / 3;
   Cell := CellW;
   if CellH < Cell then
      Cell := CellH;
   end if;
   if Cell < 8 then
      Cell := 8;
   end if;
   Ox := (W - 3 * Cell) / 2;
   Oy := Top + (H - Top - Bot - 3 * Cell) / 2;
   Hfs := H / 40;
   if Hfs < 11 then
      Hfs := 11;
   end if;
   if Hfs > 16 then
      Hfs := 16;
   end if;

   Tid := ui_timer_set(50, 0);

   while ui_win_is_closed() = 0 loop
      K := ui_2d_clear(-15724520);
      I := 0;
      while I < 9 loop
         Cx := Ox + (I mod 3) * Cell + Cell / 2;
         Cy := Oy + (I / 3) * Cell + Cell / 2;
         R := Cell / 3;
         K := ui_2d_ellipse(Cx, Cy + R / 3, R + Cell / 12, R / 2 + Cell / 20, -15066588, 1, 0);
         if I = Mole then
            if Flash = 1 then
               K := ui_2d_circle(Cx, Cy, R, -1, 1, 0);
            else
               K := ui_2d_circle(Cx, Cy, R, -2509750, 1, 0);
            end if;
            K := ui_2d_circle(Cx - R / 3, Cy - R / 4, R / 5, -15724520, 1, 0);
            K := ui_2d_circle(Cx + R / 3, Cy - R / 4, R / 5, -15724520, 1, 0);
         end if;
         I := I + 1;
      end loop;

      K := ui_2d_text(10, 20, Integer'Image(Score), -1513232, 18, 0);
      if Lang = 0 then
         K := ui_2d_text(10, 48, "得分", -6643536, 10, 0);
      else
         K := ui_2d_text(10, 48, "score", -6643536, 10, 0);
      end if;
      K := ui_2d_text(W - 10, 20, Integer'Image(Lives), -2069424, 18, 2);
      if Lang = 0 then
         K := ui_2d_text(W - 10, 48, "命", -6643536, 10, 2);
      else
         K := ui_2d_text(W - 10, 48, "lives", -6643536, 10, 2);
      end if;

      if Over = 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, "Over - tap to retry", -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 = 9 then
         Frames := Frames + 1;
         if Frames >= 6000 then
            exit;
         end if;
         if Over = 0 then
            Flash := 0;
            if Mole >= 0 then
               Molett := Molett - 1;
               if Molett <= 0 then
                  Mole := -1;
                  Lives := Lives - 1;
                  Wait := 8;
                  if Lives <= 0 then
                     Over := 1;
                  end if;
               end if;
            else
               Wait := Wait - 1;
               if Wait <= 0 then
                  Seed := (Seed * 1103 + 12345) mod 32768;
                  Mole := (Seed / 256) mod 9;
                  Molett := 14;
               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 Over = 1 then
            Score := 0;
            Lives := 3;
            Over := 0;
            Flash := 0;
            Mole := -1;
            Wait := 6;
            Frames := 0;
         elsif Tx >= Ox and Ty >= Oy then
            X := (Tx - Ox) / Cell;
            Y := (Ty - Oy) / Cell;
            if X >= 0 and Y >= 0 and X < 3 and Y < 3 then
               Idx := Y * 3 + X;
               if Idx = Mole then
                  Score := Score + 1;
                  Mole := -1;
                  Seed := (Seed * 1103 + 12345) mod 32768;
                  Wait := 4 + (Seed / 256) mod 4;
                  Flash := 1;
               end if;
            end if;
         end if;
      end if;

      if T = 1 then
         I := ui_msg_get_a();
         if I = 27 then
            exit;
         end if;
         if I = 82 then
            Score := 0;
            Lives := 3;
            Over := 0;
            Mole := -1;
         end if;
      end if;
   end loop;

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