home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / TP_ADV.ZIP / LIST0707.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-11-20  |  8.8 KB  |  314 lines

  1. Program Game;
  2.  
  3. Uses
  4.   CRT,
  5.   DOS
  6. {$IFDEF DEBUG}
  7.   ;
  8. {$ELSE}
  9.   ,
  10.   GameInt9;
  11. {$ENDIF}
  12.  
  13. Const
  14.   ShotChar      = #24; { Define the shot character as an arrow }
  15.   ShotWaitTime  = 500; { Number of times to call UpdateShot before doing so }
  16.   MaxShots      = 2;   { 5 shots on the screen at a time }
  17.  
  18.   ShipChar      = '^';
  19.   Ship          = ShipChar + ShipChar + ShipChar;
  20.  
  21.   AlienChar     = '▒';
  22.   Alien         = AlienChar + AlienChar + AlienChar;
  23.  
  24.   AlienWaitTime = 500;
  25.   MaxAliens     = 5;
  26.   AlienY        = 5;
  27.   AlienPts      = 50;
  28.  
  29. {$IFDEF DEBUG}
  30.   left   : boolean = false;
  31.   right  : boolean = true;
  32.   space  : boolean = false;
  33.   escape : boolean = false;
  34. {$ENDIF}
  35.  
  36. Type
  37.   ShotRec  = Record
  38.                Shooting : Boolean;
  39.                ShotX,
  40.                ShotY    : Byte;
  41.              End;
  42.   ShotArr  = Array [1..MaxShots] of ShotRec;
  43.   AlienRec = Record
  44.                Alive : Boolean;
  45.                X     : Byte;
  46.                Delta : ShortInt;
  47.              End;
  48.   AlienArr = Array [1..MaxAliens] of AlienRec;
  49.  
  50. Var
  51.   Score      : Word;
  52.   Shot       : ShotArr;
  53.   Aliens     : AlienArr;
  54.   ShotDelay  : Word;
  55.   AlienDelay : Word;
  56.   ScrSeg     : Word;
  57.   ScrWidth   : Byte;
  58.   ShipX,
  59.   ShipY      : Byte;
  60.   i          : Byte;
  61.  
  62. Function ScrChar ( x, y : Byte ) : Char;
  63. { This procedure will calculate the offset into video memory based   }
  64. { on the [X,Y] coordinates passed in. The character at this location }
  65. { in memory will be returned as the function result.                 }
  66. Begin
  67.   If ( y > 0 ) Then
  68.     ScrChar := Chr ( Mem [ ScrSeg : (( x - 1 ) * 2 ) +
  69.                                     (( y - 1 ) * ScrWidth * 2)]);
  70. End;
  71.  
  72. Procedure MoveLeft;
  73. { This procedure is called if a key event occurs indicating that }
  74. { the ship is to be moved left one space.                        }
  75. Begin
  76.   If ( ShipX > 1 ) Then
  77.   Begin
  78.     Dec ( ShipX );
  79.     GotoXY ( ShipX, ShipY );
  80.     Write ( Ship + ' ' );
  81.   End;
  82.   Left := FALSE;
  83. End;
  84.  
  85. Procedure MoveRight;
  86. { This procedure is called if a key event occurs indicating that }
  87. { the ship is to be moved right one space.                       }
  88. Begin
  89.   If ( ShipX < ( ScrWidth - Length ( Ship ) ) ) Then
  90.   Begin
  91.     GotoXY ( ShipX, ShipY );
  92.     Write ( ' ' + Ship );
  93.     Inc ( ShipX );
  94.   End;
  95.   Right := FALSE;
  96. End;
  97.  
  98. Procedure Fire;
  99. { This procedure is called if a key event occurs indicating that }
  100. { the ship has fired a shot at the aliens.                       }
  101. Var
  102.   i : Byte;
  103. Begin
  104.   Space := FALSE;
  105.   i := 1;
  106.   While ( Shot [i].Shooting ) AND ( i < MaxShots ) Do { Find an open }
  107.     Inc ( i );                                        { shot record. }
  108.   If ( Not Shot [i].Shooting ) AND ( i <= MaxShots ) Then
  109.   Begin
  110.     Shot [i].Shooting := TRUE;                        { Set the shot }
  111.     Shot [i].ShotX := ShipX + ( Length ( Ship ) div 2 ); { record.   }
  112.     Shot [i].ShotY := ShipY - 1;
  113.   End;
  114. End;
  115.  
  116. Function RemoveAlien ( x, y : Byte ) : Byte;
  117. { This function is called to remove an Alien from the screen.  }
  118. { The first Alien discovered at the [X,Y] coordinates passed   }
  119. { in will be removed from the screen. The function will return }
  120. { array offset of the Alien removed ( for scoring purposes ).  }
  121. Var
  122.   i    : Byte;
  123.   done : Boolean;
  124. Begin
  125.   i := 0;
  126.   done := FALSE;
  127.   While ( i < MaxAliens ) AND ( Not done ) Do
  128.   Begin
  129.     Inc ( i );
  130.     If ( AlienY = y ) AND ( Aliens [i].Alive ) AND
  131.        ( x in [(Aliens [i].X)..(Aliens [i].X+Length (Alien))] ) Then
  132.     Begin
  133.       done := TRUE;
  134.       Aliens [i].Alive := FALSE;
  135.       GotoXY ( Aliens [i].X, AlienY );
  136.       Write ( '   ' );
  137.     End;
  138.   End;
  139.   RemoveAlien := i;
  140. End;
  141.  
  142. Procedure IncScore ( i : Byte );
  143. { This procedure will increment the score using the base }
  144. { score value multiplied by the speed of the Alien.      }
  145. Begin
  146.   Score := Score + ( AlienPts * Abs ( Aliens [i].Delta ) );
  147.   GotoXY ( ( ScrWidth - 10 ), 1 );
  148.   Write ( Score : 5 );
  149.   Sound ( 500 );
  150.   Delay ( 100 );
  151.   NoSound;
  152. End;
  153.  
  154. Procedure UpdateAliens;
  155. { This routine will update the Aliens on the video screen. }
  156. Begin
  157.   Inc ( AlienDelay );                   { Add one to the global counter }
  158.   If ( ( AlienDelay Mod AlienWaitTime ) = 0 ) Then { Check delay factor }
  159.     For i := 1 to MaxAliens Do
  160.       If ( Aliens [i].Alive ) Then       { If the Alien is active }
  161.         If ( Aliens [i].X >= ScrWidth - ( Length ( Alien ) + 1 ) ) OR
  162.            ( Aliens [i].X <= 1 ) Then
  163.         Begin
  164.           GotoXY ( Aliens [i].X, AlienY );
  165.           Write ( '   ');
  166.           Aliens [i].Delta := -Aliens [i].Delta;
  167.           Inc ( Aliens [i].X, Aliens [i].Delta );
  168.           GotoXY ( Aliens [i].X, AlienY );
  169.           Write ( Alien );
  170.         End
  171.         Else
  172.         Begin
  173.           GotoXY ( Aliens [i].X, AlienY );
  174.           Write ( '   ' );
  175.           Inc ( Aliens [i].X, Aliens [i].Delta );
  176.           GotoXY ( Aliens [i].X, AlienY );
  177.           Write ( Alien );
  178.         End;
  179. End;
  180.  
  181. Procedure UpdateShot;
  182. { This procedure will update any shots that are active on the screen. }
  183. Var
  184.   i : Integer;
  185. Begin
  186.   Inc ( ShotDelay );                      { Add one to the global counter }
  187.   If ( ( ShotDelay mod ShotWaitTime ) = 0 ) Then     { Check delay factor }
  188.     For i := 1 to MaxShots Do
  189.     Begin
  190.       If ( Shot [i].Shooting ) Then               { If the shot is active }
  191.       Begin
  192.         If ( ScrChar ( Shot [i].ShotX, Shot [i].ShotY ) = AlienChar ) Then
  193.         Begin
  194.                   { An Alien moved on top of the bullet }
  195.           IncScore ( RemoveAlien ( Shot [i].ShotX, Shot [i].ShotY ) );
  196.           Shot [i].Shooting := FALSE;
  197.         End
  198.         Else
  199.         If ( ScrChar ( Shot [i].ShotX, Shot [i].ShotY - 1 ) = AlienChar ) Then
  200.         Begin
  201.                   { The bullet will move into an Alien }
  202.           GotoXY ( Shot [i].ShotX, Shot [i].ShotY );
  203.           Write ( ' ' );
  204.           IncScore (RemoveAlien ( Shot [i].ShotX, Shot [i].ShotY - 1 ) );
  205.           Shot [i].Shooting := FALSE;
  206.         End
  207.         Else
  208.         Begin
  209.                   { The shot will not hit anything yet }
  210.           GotoXY ( Shot [i].ShotX, Shot [i].ShotY );
  211.           Write ( ' ' );
  212.           Dec ( Shot [i].ShotY );
  213.           If ( Shot [i].ShotY <= AlienY - 1 ) Then
  214.             Shot [i].Shooting := FALSE
  215.           Else
  216.           Begin
  217.             GotoXY ( Shot [i].ShotX, Shot [i].ShotY );
  218.             Write ( ShotChar );
  219.           End
  220.         End
  221.       End
  222.     End
  223. End;
  224.  
  225. Procedure EventLoop;
  226. { Main event loop of the program. }
  227. var
  228.   ch : char;
  229. Begin
  230.   Repeat
  231.     If ( Left ) Then        { Key Event }
  232.       MoveLeft;
  233.     If ( Right ) Then       { Key Event }
  234.       MoveRight;
  235.     If ( Space ) Then       { Key Event }
  236.       Fire;
  237.     UpdateAliens;           { Update Event }
  238.     UpdateShot;             { Update Event }
  239.     While ( KeyPressed ) Do
  240.       ch := ReadKey;
  241.   Until ( Escape );         { Key Event signalling Exit }
  242. End;
  243.  
  244. Procedure SetUpScrInfo;
  245. { Get the screen width and base video address for the ScrChar routine. }
  246. Var
  247.   r : Registers;
  248. Begin
  249.   r.ah := $0F;
  250.   Intr ( $10, r );   { Call BIOS Interrupt 10h Service 0Fh }
  251.   ScrWidth := r.ah;
  252.   Case ( r.al ) Of
  253.     1,2,3 : ScrSeg := $B800;
  254.     7 : ScrSeg := $B000;
  255.   End;
  256. End;
  257.  
  258. Procedure OffCursor;
  259. { Deactivate the cursor on the screen }
  260. Var
  261.   r : Registers;
  262. Begin
  263.   r.ah := $01;
  264.   r.cx := $2020;     { Start and end scan lines for the cursor      }
  265.   Intr ( $10, r );   { Interrupt 10h, service 01h - Set cursor size }
  266. End;
  267.  
  268. Procedure OnCursor;
  269. { Reactivate the cursor on the screen }
  270. Var
  271.   r : Registers;
  272. Begin
  273.   r.ah := $01;
  274.   If ( ScrSeg = $B000 ) Then
  275.     r.cx := $0C0D    { Start and end scan lines for Monochrome cursor }
  276.   Else
  277.     r.cx := $0607;   { Start and end scan lines for Color cursor }
  278.   Intr ( $10, r );   { Interrupt 10h, service 01h - Set cursor size }
  279. End;
  280.  
  281. Procedure InitVars;
  282. { Initialize the global variables }
  283. Var
  284.   i : Integer;
  285. Begin
  286.   Score := 0;
  287.   ShotDelay := 0;
  288.   GotoXY ( ( ScrWidth - 17 ), 1 );
  289.   Write ( 'Score: ', Score : 5 );
  290.   For i := 1 to MaxShots do
  291.     Shot [i].Shooting := FALSE;
  292.   Randomize;
  293.   For i := 1 to MaxAliens do
  294.   Begin
  295.     Aliens [i].X := Random ( ScrWidth - 5 ) + 2;
  296.     Aliens [i].Alive := TRUE;
  297.     Aliens [i].Delta := Random (3) + 1;
  298.   End;
  299.   ShipX := 1;
  300.   ShipY := 25;
  301. End;
  302.  
  303. Begin
  304.   OffCursor;               { Turn off the cursor              }
  305.   ClrScr;                  { Clear the video screen           }
  306.   SetUpScrInfo;            { Get current video information    }
  307.   InitVars;                { Initialize the global variables  }
  308.   GotoXY ( ShipX, ShipY ); { Draw the intial ship location on }
  309.   Write ( Ship );          {   the screen.                    }
  310.   EventLoop;               { Main event loop                  }
  311.   OnCursor;                { Turn on the cursor               }
  312.   ClrScr;
  313. End.
  314.