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.

Losing lives
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.

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:

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: 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.

Summary
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.