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

ada/app/pong.adb · 6819 字节 · 下载原文

▶ 在浏览器里试玩

-- 对打(Pong)—— 用 **Ada** 写的手机小游戏
-- Pong -- a small phone game written in **Ada**
--
-- 玩法(与 Examples/c/app/pong.c / Examples/python/app/pong.py 同一套):
--   左板跟手指(点/拖左半屏),右板是 AI 追球,先到 5 分;
--   球碰上下边界反弹,碰板反弹并提速;出界对方得分;结束后点屏幕 / ESC 回车重开。
--
-- ## 本语言的实测约束(见 Examples/ada/app/stopwatch.adb 文件头)
--
-- 跑法 —— 手机:vml run examples/ada/pong.adb
-- 跑法 —— 桌面:vmlcli Examples/ada/pong.adb --screen 480x640 --frames /tmp/fr

with VML_UI;

procedure Pong is
   K : Integer := 0;
   W : Integer := 360;
   H : Integer := 620;
   Padw : Integer := 12;
   Padh : Integer := 70;
   Ballr : Integer := 6;
   Ly : Integer := 310;
   Ry : Integer := 310;
   Bx : Integer := 180;
   By : Integer := 310;
   Bvx : Integer := -5;
   Bvy : Integer := 3;
   Sl : Integer := 0;
   Sr : Integer := 0;
   Ended : Integer := 0;
   Frames : Integer := 0;
   Seed : Integer := 20262;
   Servedir : Integer := 0;
   I : Integer := 0;
   Top : Integer := 0;
   T : Integer := 0;
   Tid : 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;
   Padw := W / 28;
   if Padw < 8 then
      Padw := 8;
   end if;
   Padh := H / 6;
   if Padh < 40 then
      Padh := 40;
   end if;
   Ballr := W / 40;
   if Ballr < 4 then
      Ballr := 4;
   end if;
   Ly := H / 2;
   Ry := H / 2;
   Bx := W / 2;
   By := H / 2;

   Tid := ui_timer_set(33, 0);

   while ui_win_is_closed() = 0 loop
      if Ended = 0 then
         Bx := Bx + Bvx;
         By := By + Bvy;
         if By < Ballr then
            By := Ballr;
            Bvy := 0 - Bvy;
         end if;
         if By > H - Ballr then
            By := H - Ballr;
            Bvy := 0 - Bvy;
         end if;
         if Bvx < 0 and Bx <= 10 + Padw + Ballr and Bx >= 6 then
            Top := Ly - Padh / 2;
            if By >= Top and By <= Top + Padh then
               Bx := 10 + Padw + Ballr;
               Bvx := 0 - Bvx - 1;
               if Bvx < -14 then
                  Bvx := -14;
               end if;
            end if;
         end if;
         if Bvx > 0 and Bx >= W - 10 - Padw - Ballr and Bx <= W - 6 then
            Top := Ry - Padh / 2;
            if By >= Top and By <= Top + Padh then
               Bx := W - 10 - Padw - Ballr;
               Bvx := 0 - Bvx + 1;
               if Bvx > 14 then
                  Bvx := 14;
               end if;
            end if;
         end if;
         if Bx < 0 - Ballr then
            Sr := Sr + 1;
            if Sr >= 5 then
               Ended := 1;
            end if;
            Bx := W / 2;
            By := H / 2;
            if Servedir = 0 then
               Bvx := -5;
               Servedir := 1;
            else
               Bvx := 5;
               Servedir := 0;
            end if;
            Seed := (Seed * 1103 + 12345) mod 32768;
            if (Seed / 256) mod 2 = 0 then
               Bvy := -3;
            else
               Bvy := 3;
            end if;
         end if;
         if Bx > W + Ballr then
            Sl := Sl + 1;
            if Sl >= 5 then
               Ended := 1;
            end if;
            Bx := W / 2;
            By := H / 2;
            if Servedir = 0 then
               Bvx := -5;
               Servedir := 1;
            else
               Bvx := 5;
               Servedir := 0;
            end if;
            Seed := (Seed * 1103 + 12345) mod 32768;
            if (Seed / 256) mod 2 = 0 then
               Bvy := -3;
            else
               Bvy := 3;
            end if;
         end if;
         if Ry < By then
            Ry := Ry + 4;
         end if;
         if Ry > By then
            Ry := Ry - 4;
         end if;
         if Ly < Padh / 2 then
            Ly := Padh / 2;
         end if;
         if Ly > H - Padh / 2 then
            Ly := H - Padh / 2;
         end if;
         if Ry < Padh / 2 then
            Ry := Padh / 2;
         end if;
         if Ry > H - Padh / 2 then
            Ry := H - Padh / 2;
         end if;
      end if;

      K := ui_2d_clear(-15724520);
      I := 10;
      while I + 16 <= H - 10 loop
         K := ui_2d_rect(W / 2 - 2, I, 4, 16, -14013898, 1, 0, 0);
         I := I + 32;
      end loop;
      K := ui_2d_rect(10, Ly - Padh / 2, Padw, Padh, -11409298, 1, 0, 4);
      K := ui_2d_rect(W - 10 - Padw, Ry - Padh / 2, Padw, Padh, -11890471, 1, 0, 4);
      K := ui_2d_circle(Bx, By, Ballr, -1513232, 1, 0);

      K := ui_2d_text(W / 2 - 70, 20, Integer'Image(Sl), -1513232, 26, 1);
      K := ui_2d_text(W / 2 + 70, 20, Integer'Image(Sr), -1513232, 26, 1);

      if Ended = 1 then
         if Sl >= 5 then
            if Lang = 0 then
               K := ui_2d_text(W / 2, H / 2 - 40, "你赢了!点一下再来", -11409298, 20, 1);
            else
               K := ui_2d_text(W / 2, H / 2 - 40, "You win! Tap to retry", -11409298, 20, 1);
            end if;
         else
            if Lang = 0 then
               K := ui_2d_text(W / 2, H / 2 - 40, "电脑赢了,点一下再来", -2069424, 20, 1);
            else
               K := ui_2d_text(W / 2, H / 2 - 40, "AI wins - tap to retry", -2069424, 20, 1);
            end if;
         end if;
      else
         if Lang = 0 then
            K := ui_2d_text(W / 2, 58, "左半屏滑动控制球板,先到 5 分", -6643536, 11, 1);
         else
            K := ui_2d_text(W / 2, 58, "Drag on the left half; first to 5", -6643536, 11, 1);
         end if;
      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
         if Ended /= 0 then
            Sl := 0;
            Sr := 0;
            Ended := 0;
            Frames := 0;
            Bx := W / 2;
            By := H / 2;
         else
            Ly := ui_msg_get_b();
         end if;
      end if;

      if T = 7 then
         if Ended = 0 then
            Ly := ui_msg_get_b();
         end if;
      end if;

      if T = 1 then
         I := ui_msg_get_a();
         if I = 27 then
            exit;
         end if;
         if I = 13 or I = 82 then
            Sl := 0;
            Sr := 0;
            Ended := 0;
            Frames := 0;
         end if;
      end if;

      if T = 9 then
         Frames := Frames + 1;
         if Frames >= 6000 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 Pong;