home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Borland Plateform / Turbo Pascal V7.0 / BREAKOUT.ZIP / BREAKOUT.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-30  |  6.7 KB  |  306 lines

  1. {************************************************}
  2. {                                                }
  3. {   Breakout Demo Program                        }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. program Breakout;
  9.  
  10. {
  11.  
  12.   This is a version of the classic arcade game, Breakout.
  13.  
  14.     SCREEN.PAS
  15.     COUNT.PAS
  16.     BRICKS.PAS
  17.     BOUNDS.PAS
  18.     WALLS.PAS
  19.     BREAKOUT.PAS
  20.  
  21.   To build an executable file, compile from the command line with:
  22.  
  23.     tpc /m breakout
  24.  
  25.   or load BREAKOUT.PAS into the integrated development
  26.   environment and press F9.
  27.  
  28.   When testing the program, you may want to force the paddle to
  29.   follow the ball, so you'll never miss. The program contains
  30.   conditional compilation directives to produce this version, and
  31.   you can build it from the command line with:
  32.  
  33.     tpc /DTest breakout
  34.  
  35.   or load BREAKOUT.PAS into the integrated development
  36.   environment, select Alt-O/C/Alt-C, type 'Test' (without the quotes,
  37.   of course) followed by the Enter key, then select Alt-C/B to
  38.   rebuild the executable file.
  39. }
  40.  
  41. uses Screen, Count, Bricks, Bounds, Walls, Crt, Dos;
  42.  
  43. var
  44.   ss        : SaveScreen;
  45.   w         : Wall;
  46.   b         : Ball;
  47.   p         : Paddle;
  48.   Speed     : LimitCounter;
  49.   Left      : LeftBound;
  50.   Top       : UpperBound;
  51.   Right     : RightBound;
  52.   Bottom    : LowerBound;
  53.   Obstacles : ObstacleList;
  54.   PaddleMsg,
  55.   SpeedMsg,
  56.   StartMsg,
  57.   QuitMsg,
  58.   PauseMsg1,
  59.   PauseMsg2,
  60.   TypeMsg   : TextString;
  61.   Score     : Counter;
  62.   Highest   : Counter;
  63.   Balls     : DownCounter;
  64.   X         : Integer;
  65.   Finished  : Boolean;
  66.   FirstGame : Boolean;
  67.   TypeInc,
  68.   ch        : Char;
  69.  
  70. procedure Startup;
  71. begin
  72.   { First set up the screen and the cursor }
  73.   ss.Init;
  74.   TextBackground(BLACK);
  75.   ClrScr;
  76.  
  77.   { Create the boundaries of the playfield }
  78.   Left.Init(0, 0, 27, False);
  79.   Top.Init(0, 0, 82, False);
  80.   Right.Init(81, 0, 27, False);
  81.   Bottom.Init(0, 24, 82, True);
  82.  
  83.   { Initialize the score displays }
  84.   Score.Init(0, 65, 24, 'Score', 15);
  85.   Score.Show;
  86.   Highest.Init(0, 60, 25, 'High Score', 14);
  87.   Highest.Show;
  88.  
  89.   { Set up the various menu messages }
  90.   PauseMsg1.Init(31, 18, 'Paused.  Press any', 15);
  91.   PauseMsg2.Init(31, 19, ' key to continue.', 15);
  92.   SpeedMsg.Init(5, 23, #24 + #25  + ' to change speed', 14);
  93.   StartMsg.Init(5, 24, #17 + #196 + #217 + ' to begin game', 14);
  94.   PaddleMsg.Init(5, 24, #27 + #26 + ' to move paddle', 14);
  95.   QuitMsg.Init(5, 25, 'ESC to quit', 14);
  96.   QuitMsg.Show;
  97.  
  98.   { Set up the information messages }
  99.   Balls.Init(5, 40, 24, -1, 'Balls', 15);
  100.   Balls.Show;
  101.   Speed.Init(1, 40, 25, 1, 10, 'Speed', 14);
  102.   Speed.Show;
  103.  
  104.   { Build the wall }
  105.   w.Init(1, 1, 16, 10);
  106.   w.Show;
  107.  
  108.   { Need to initialize these, even though we're going to move them later }
  109.   b.Init(10, 22, 1, -1, YELLOW);
  110.   p.Init(8, 23, WHITE);
  111.  
  112.   { Put the various obstacles into a list.  We don't really need
  113.     to do this, but it makes changing things around much easier }
  114.   Obstacles.Init;
  115.   Obstacles.Append(@p);
  116.   Obstacles.Append(@w);
  117.   Obstacles.Append(@Left);
  118.   Obstacles.Append(@Top);
  119.   Obstacles.Append(@Right);
  120.   Obstacles.Append(@Bottom);
  121.  
  122.   TypeMsg.Init(22, 12, 'Increase typematic rate? (y/n) ', WHITE);
  123.   TypeMsg.Show;
  124.   repeat
  125.     TypeInc := UpCase(ReadKey);
  126.   until (TypeInc = 'Y') or (TypeInc = 'N');
  127.   TypeMsg.Hide;
  128.  
  129.   if TypeInc = 'Y' then
  130.     ss.Speedup;
  131.  
  132.   ss.SetCursor($2000);
  133.   Randomize;
  134.   FirstGame := True;
  135. end;
  136.  
  137. procedure NewGame;
  138. begin
  139.   Balls.Reset;
  140.   Score.Reset;
  141.   if not FirstGame then
  142.     w.Reset;
  143.   X := Random(78) + 3;
  144.   b.MoveTo(X, 22);
  145.   p.MoveTo(X-2, 23);
  146.   b.Show;
  147.   p.Show;
  148.   Balls.Decrement;
  149.   FirstGame := False;
  150. end;
  151.  
  152. { This procedure handles keystrokes between games.
  153.   It returns False if the user presses ESC, otherwise it returns True. }
  154. function MainMenu : Boolean;
  155. var
  156.   Done : Boolean;
  157. begin
  158.   MainMenu := True;
  159.   Done := False;
  160.   SpeedMsg.Show;
  161.   StartMsg.Show;
  162.   while not Done do
  163.   begin
  164.     ch := ReadKey;
  165.     case ch of
  166.       Chr(27) :
  167.         begin
  168.           MainMenu := False;
  169.           Done := True;
  170.         end;
  171.       #13 : Done := True;
  172.       #0  :
  173.         begin
  174.           ch := ReadKey;
  175.           if Ord(ch) = 72 then
  176.             Speed.Increment
  177.           else if Ord(ch) = 80 then
  178.             Speed.Decrement;
  179.         end;
  180.       end;
  181.   end;
  182.   SpeedMsg.Hide;
  183.   StartMsg.Hide;
  184. end;
  185.  
  186. { This procedure handles keystrokes while the game is in progress }
  187. procedure ProcessKeyStroke;
  188.  
  189. { Pause the game }
  190. procedure Pause;
  191. begin
  192.   PauseMsg1.Show;
  193.   PauseMsg2.Show;
  194.   ch := ReadKey;
  195.   if KeyPressed then
  196.     ch := ReadKey;  { Swallow extended keystrokes }
  197.   PauseMsg1.Hide;
  198.   PauseMsg2.Hide;
  199.   b.Show;
  200. end;
  201.  
  202. begin
  203.   ch := ReadKey;
  204.   case ch of
  205.     Chr(27) : Finished := True;
  206.     Chr(0)    :
  207.       begin
  208.         ch := ReadKey;
  209. {$IFNDEF Test}
  210.         case Ord(ch) of
  211.           75: p.MoveTo(p.GetX - 1, p.GetY);  { Left Arrow }
  212.           77: p.MoveTo(p.GetX + 1, p.GetY);  { Right Arrow }
  213.         else
  214.           Pause;
  215.         end;
  216. {$ELSE}
  217.         Pause;
  218. {$ENDIF}
  219.       end
  220.     else
  221.       Pause;
  222.   end;
  223. end;
  224.  
  225. { This procedure checks for collisions with any of the obstacles
  226.   and updates the screen accordingly. }
  227. procedure Update;
  228. var
  229.   Offset : Integer;
  230. begin
  231.   if Obstacles.CheckCollisions(b, Score) then
  232.   begin
  233.     b.MoveY;
  234.     p.MoveTo(b.GetX - 2, p.GetY);
  235.     sound(150);
  236.     Delay(300);
  237.     nosound;
  238.     Balls.Decrement;
  239.     while KeyPressed do
  240.       ch := ReadKey;
  241.   end;
  242.  
  243.   b.MoveX;
  244.   b.MoveY;
  245.  
  246. {$IFDEF Test}
  247.   p.MoveTo(b.NextX -2, p.GetY);
  248. {$ENDIF}
  249. end;
  250.  
  251. { This procedure cleans up when we're exiting from the program }
  252. procedure ShutDown;
  253. begin
  254.  b.Hide;
  255.  Obstacles.Hide;
  256.  Balls.Hide;
  257.  Score.Hide;
  258.  
  259.  Obstacles.Done;
  260.  
  261.  ss.Restore;
  262.  if TypeInc = 'Y' then
  263.    ss.Slowdown;
  264.  ClrScr;
  265. end;
  266.  
  267. { This procedure plays a game.  The main loop allows up to ten keystrokes,
  268.  then moves the ball and checks for collisions }
  269. procedure Play;
  270. var
  271.  KeyLoops : Integer;
  272. begin
  273.  NewGame;
  274. {$IFNDEF Test}
  275.   PaddleMsg.Show;
  276. {$ENDIF}
  277.   Finished := False;
  278.   KeyLoops := 0;
  279.   repeat
  280.     if KeyPressed then
  281.       ProcessKeyStroke;
  282.     Inc(KeyLoops);
  283.     if (KeyLoops = 10) and not Finished then
  284.     begin
  285.       KeyLoops := 0;
  286.       UpDate;
  287.     end;
  288.     Delay(12 - Speed.GetValue);
  289.   until Finished or Balls.Last;
  290.   PaddleMsg.Hide;
  291. end;
  292.  
  293. begin
  294.   Startup;
  295.   while MainMenu do
  296.   begin
  297.     Play;
  298.     Balls.Reset;
  299.     b.Hide;
  300.     p.Hide;
  301.     if Score.GetValue > Highest.GetValue then
  302.       Highest.SetValue(Score.GetValue);
  303.   end;
  304.   ShutDown;
  305. end.
  306.