Breakout Tutorial 6
This tutorial will show you how to build a clone of Breakout  in Pascal using the SwinGame SDK. Breakout is the successor to Pong, making it one of the first few computer games. It took Steve Wozniak  four days to build the first prototype of Breakout, however in SwinGame you can make it in a couple of hours. By the end of this tutorial you will have a fully functional Breakout game, with different levels, sound effects, and completely addictive gameplay.
In this tutorial, we will implement lives in Breakout, so that you can lose the game. We already have in our Player data structure a variable for the number of lives remaining, and we have also already included the code to add a life for every second level that the player wins. The only code we need to implement is the code which is called when the player loses a life.
Firstly we need some code that will be called when the player is supposed to lose a life. This will be LoseALife() procedure. In Breakout, we will call this code when the ball hits the lower part of the screen. When this code runs, the number of lives will reduce by one. If there are lives left, then the ball will reset to its default position, and the game will continue. If there aren’t any lives left, we will show a screen asking if the player wants to continue. If they do, we will reset all of the objects, which will in effect start the game again.
Before we add the LoseALife procedure, we need to open GameResources.pas and add the following line to the LoadImages() procedure, to load the Game Over image:
Now add the following code to GameLogic.pas, above the Main procedure, but after all of the Initialise procedures.
procedure LoseALife(); begin // Take off one life player.lives -=1; if player.lives <0 then begin // The player is dead. // Remove the ball from the screen and prevent it from moving. ball.x := -ballWidth; ball.y := -ballHeight; ball.Movement := CreateVector(0,0); // Show the Game Over screen, asking the user if they want to continue. repeat ProcessEvents(); ClearScreen(ColorBlack); // Draw the Game Over bitmap at point (0,0). DrawBitmapOnScreen(GameImage('gameover'), CreatePoint(0,0)); RefreshScreen(25); // Wait until Y, N or Escape is pressed. until IsKeyPressed(VK_Y) or IsKeyPressed(VK_Escape) or IsKeyPressed(VK_N) or WindowCloseRequested(); // Did they press Y? if IsKeyPressed(VK_Y) then begin // Reset the game InitialisePlayer(); InitialiseBricks(); InitialiseBall(); end; end else begin // The player still has lives remaining, so just reset the ball position. InitialiseBall(); end; end;
You may notice that after the Game Over screen is shown, and the player has pressed either Y, N or Escape, that nothing will happen if N or Escape are pressed. This is because later on we will add code in the main game loop that will end the game if the number of lives is below zero. Therefore we don’t need to add any code: when this procedure finishes, the game will see that there are no lives remaining, and will close.
The code which shows the Game Over screen is actually quite similar to the main game loop, except instead of containing game logic it just displays a bitmap. You can use a similar piece of code to allow your player to pause the game, as the actual game logic isn’t running at this point.
Killing the Player
The player will lose a life when they hit the bottom edge of the screen. Now that we have our LoseALife procedure, we can call it when the player’s Y coordinate is equal to or greater than the height of the screen (minus the ball height). In the game’s main loop, find the following piece of code:
if ball.y > screenHeight-ballHeight then begin ball.y := screenHeight-ballHeight; ball.Movement.Y := -ball.Movement.Y; end;
This code will bounce the ball off the bottom of the screen. We don’t want this, this was just temporary while we wrote the rest of the game’s logic. Replace it with the following code:
if ball.y > screenHeight-ballHeight then begin LoseALife(); // Are we still playing? if player.lives <0 then break; end;
This code will take a life off the player when they hit the ground. After the LoseALife function runs, it will check if there are any more lives left. If there aren’t, it will break out of the main loop, which will end the game.
In this tutorial we added the logic to handle the player’s death. In the final tutorial, we will add the ability to pause the game, add sounds, and add a background image.