Programs to Complete: Breakout

Jack Fearn contributed this program to give you a good start in creating your own version of Breakout. The comment at the start of the program provides some suggestions for development. We hope it is too tempting an offer for you to refuse!

program Breakout;

{
  This program is based on the classic arcade game "Breakout", developed by Atari Inc.

  The program is incomplete, and the following areas have not been coded in:

    -   Although the score has been accounted for in some areas, it has not been implemented yet.
    -   There is basic movement for the ball moving straight up and down,
        but there is no code to make the ball move diagonally.
           !!* The original game could alter the angle the ball was released at depending on which position
               of the paddle it made contact with. Since the paddle is 3 characters long,
               there are 5 potential positions to check for contact. *!!
    -   The paddle can move left and right across the screen, but does nothing else.
    -   The display looks very basic, and perhaps could do with some colour...

  Good Luck!

}
uses
  crt;

const
  maxX = 61;
  maxY = 10;
  cursor = ' === ';
  cursorY = 25;
  ballX = 20;
  speed = 70;

var
  blocks : array[2 .. maxX, 1 .. maxY] of Boolean;
  ballY, cursorX : integer;
  Key : char;
  ballDirection, remainingBlocks, lives : integer;
  response : string;

procedure setup;
var
  i, x : integer;
begin
  clrscr;
  for i := 1 to maxY do
    begin
      for x := 2 to maxX do
        begin
          gotoxy(x, i);
          write('-');
        end;
    end;
  cursorX := 19;
  gotoxy(cursorX, cursorY);
  write(cursor);
  for i := 1 to 40 do
    begin
      for x := 1 to 20 do
        begin
          Blocks[i, x] := True;
        end;
    end;

  lives := 3;   // Number of chances we will give the user
  ballY := 20;  // Starting position of ball
  balldirection := 1;  // Starting direction of ball
  remainingBlocks := 10;  // Number of blocks still on screen
end;

function endGame : Boolean;  // Returns True if user wishes to quit
begin
  result := False;
  delay(1000);
  clrscr;

  if lives = 0 then
    writeln('You died! :(')
  else if remainingBlocks = 0 then
    writeln('You won! :)');
  delay(2000);
  writeln;
  writeln('Play again? Y/N');
  readln(response);
  if response = 'N' then
    result := True;
end;

begin
  // Splash screen
  gotoxy(36, 10);
  writeln('Welcome to');
  gotoxy(37, 12);
  writeln('BREAKOUT!');
  delay(1000);
  gotoxy(27, 14);
  writeln('Hit Enter to start playing...');
  readln;

  clrscr;
  cursorOff;  // CRT function - turns the console's flashing cursor off

  repeat  // Program will loop until requested to exit
    setup;  // Primes the array and draws the screen
    repeat
      delay(speed); // speed of game
      if keypressed = true then
        begin
          key := readkey;
          case key of   // cursor direction + draw cursor
            'a' : begin
                    if cursorX <> 0 then
                      begin
                        cursorX -= 1;
                        gotoxy(cursorX, cursorY);
                        write(cursor);
                      end;
                  end;
            'd' : begin
                    if cursorX <> 58 then
                      begin
                        cursorX += 1;
                        gotoxy(cursorX, cursorY);
                        write(cursor);
                      end;
                  end;
          end;
        end;

      if ballY = 1 then
        ballDirection := 2  // Down
      else if ballY = (CursorY - 1) then
        ballDirection := 1  // Up
      else if ballY <= (maxY + 1) then
        begin
          if blocks[ballX][ballY - 1] = True then
            begin
              blocks[ballX][ballY - 1] := False;
              remainingBlocks -= 1;
              gotoxy(ballX, ballY - 1);
              write(' ');
              ballDirection := 2;
              //increase score
            end;
        end;

      case ballDirection of  // Draw ball
        1 : begin
              gotoxy(ballX, ballY);
              write(' ');
              ballY -= 1;
              gotoxy(ballX, ballY);
              write('0');
            end;
        2 : begin
              gotoxy(ballX, ballY);
              write(' ');
              ballY += 1;
              gotoxy(ballX, ballY);
              write('0');
            end;
      end;
    until (lives = 0) or (remainingBlocks = 0);

    if endGame = true then
      halt;
  until 1 = 2;
end.

Programming - a skill for life!

Unfinished programs for you to complete