home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / T-Pascal.70 / BREAKOUT.ZIP / BRICKS.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-30  |  3.2 KB  |  156 lines

  1. {************************************************}
  2. {                                                }
  3. {   Breakout Demo Program                        }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. unit Bricks;
  9.  
  10. {
  11.   See BREAKOUT.PAS.
  12.   This unit contains the Ball object and the object types that
  13.   end up as bricks on the screen.
  14. }
  15.  
  16. interface
  17.  
  18. uses Screen, Count;
  19.  
  20. type
  21.   Block = object(Location)
  22.     Color : Integer;
  23.     Width : Integer;
  24.     BChar : Char;
  25.     constructor Init(InitX, InitY, InitColor, InitWidth : Integer;
  26.                      InitChr : Char);
  27.     procedure Show; virtual;
  28.     procedure Hide; virtual;
  29.   end;
  30.  
  31.   Ball = object(Block)
  32.     XVel : Integer;
  33.     YVel : Integer;
  34.     constructor Init(InitX, InitY, InitXVel, InitYVel, InitColor : Integer);
  35.     function NextX : Integer;
  36.     function NextY : Integer;
  37.     procedure MoveX;
  38.     procedure MoveY;
  39.     procedure ReverseX;
  40.     procedure ReverseY;
  41.     procedure ChangeXVel(Delta : Integer);
  42.   end;
  43.  
  44.   Brick = object(Block)
  45.     Value : Integer;
  46.     constructor Init(InitX, InitY, InitColor, InitValue : Integer);
  47.     function GetValue : Integer;
  48.   end;
  49.  
  50. implementation
  51.  
  52. uses Crt;
  53.  
  54. constructor Block.Init(InitX, InitY, InitColor, InitWidth : Integer;
  55.                        InitChr : Char);
  56. begin
  57.   Location.Init(InitX, InitY);
  58.   Color := InitColor;
  59.   Width := InitWidth;
  60.   BChar := InitChr;
  61. end;
  62.  
  63. procedure Block.Show;
  64. var
  65.   Str : String[10];
  66. begin
  67.   FillChar(Str[1], Width, BChar);
  68.   Str[0] := Chr(Width);
  69.   Location.Show;
  70.   TextColor(Color);
  71.   GoToXY(X, Y);
  72.   Write(Str);
  73. end;
  74.  
  75. procedure Block.Hide;
  76. begin
  77.   Location.Hide;
  78.   GoToXY(X, Y);
  79.   Write('' : Width);
  80. end;
  81.  
  82. constructor Brick.Init(InitX, InitY, InitColor, InitValue : Integer);
  83. var
  84.   BlockChar : Char;
  85. begin
  86.   BlockChar := Chr($B2);
  87.   if (LastMode = Mono) and Odd(InitX + InitY) then
  88.         BlockChar := Chr($B0);
  89.   Block.Init(InitX, InitY, InitColor, 5, BlockChar);
  90.   Value := InitValue;
  91. end;
  92.  
  93. function Brick.GetValue : Integer;
  94. begin
  95.   GetValue := Value;
  96. end;
  97.  
  98. constructor Ball.Init(InitX, InitY, InitXVel, InitYVel, InitColor : Integer);
  99. begin
  100.   Block.Init(InitX, InitY, InitColor, 1, Chr(15));
  101.   XVel := InitXVel;
  102.   YVel := InitYVel;
  103. end;
  104.  
  105. function Ball.NextX : Integer;
  106. begin
  107.   NextX := X + XVel;
  108. end;
  109.  
  110. function Ball.NextY : Integer;
  111. begin
  112.   NextY := Y + YVel;
  113. end;
  114.  
  115. procedure Ball.MoveX;
  116. begin
  117.   Hide;
  118.   X := NextX;
  119.   Show;
  120. end;
  121.  
  122. procedure Ball.MoveY;
  123. begin
  124.   Hide;
  125.   Y := NextY;
  126.   Show;
  127. end;
  128.  
  129. procedure Ball.ReverseX;
  130. begin
  131.   XVel := -XVel;
  132. end;
  133.  
  134. procedure Ball.ReverseY;
  135. begin
  136.   YVel := -YVel;
  137. end;
  138.  
  139. { This procedure introduces the variations in horizontal velocity for
  140.   the ball.  Horizontal velocity ranges from -2 to 2.  If you hit the
  141.   ball with the edge of the paddle, you'll get a large change in
  142.   horizontal velocity. }
  143.  
  144. procedure Ball.ChangeXVel(Delta : Integer);
  145. begin
  146.   Inc(XVel, Delta);
  147.   if XVel < -2 then
  148.     XVel := -2
  149.   else if XVel > 2 then
  150.     XVel := 2
  151.   else if XVel = 0 then
  152.     XVel := Integer(Random(2))*2 - 1;
  153. end;
  154.  
  155. end.
  156.