Breakout Tutorial 5

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 add scoring and levels to Breakout. Since a lot of the groundwork is already in place (assuming you have done the previous tutorials), it is not very much more extra code to implement scoring, however it is still an important part of the game.

Scoring
When a brick is hit, the player’s score will increase depending on which row the brick is from. If it is from the closest row, the player will score 50 points. If it is the next row, it will be 100 points, and so on for however many rows you have. (In this tutorial it is three, but it’s very easy to change the number of rows in the CONST section of the code). Scroll down to the main loop in GameLogic.pas, and find the code that is called when a brick is hit. Above the code which modifies bricksRemaining, add this line of code:

Drawing the score
If you compile the code now, the score will increase, however it isn’t written to the screen so you won’t see if it is actually increasing. First we need to open GameResources.pas and create the font used for drawing the text. Scroll down to find the LoadFonts procedure, and add the following code: This will create an instance of the Arial font, size 14. You can set this font to anything you want, as long as you copy its TTF file (TrueType Font file) to the Resources\Fonts folder. Now to add the code which will print the text. In the game loop, just above the RefreshScreen(60); call, add this line of code: This code will print the player’s level, lives, score, and bricks remaining to the screen at point (8,8), in the Arial font we defined before.

Levels
When the player breaks all the bricks, they will advance to the next level. In this implementation of the game, advancing to the next level means the ball and the paddle will travel faster, resulting in more difficult gameplay. We will also give the player an extra life for every second level they advance to (even though we don’t handle lives yet, that is in the next tutorial). To increase code readability, we will put all of the code for moving to the next level in a procedure called NextLevel. Add the following code to GameLogic.pas, before the main procedure: This code increases the level number and resets the game. Now we need to add code to the game loop to call this code when the player runs out of bricks. Scroll down the game loop code to the block called when the brick is hit. After the line beginning with “player.bricksRemaining :=”, add these two lines of code:

Making the levels harder
We now need to change the game so the speed will be varied when the level changes. The best way to do this is to replace the constants for speed with expressions based on the level. Go to the start of the game loop, and replace the code that check for the player’s movement with the following: Now when the game runs, the player’s paddle will move at a speed that is proportional to the player’s level. To make the ball go faster, go to the InitialiseBall procedure and change the line beginning with “ball.movement” to: Now the ball will move faster as the levels increase. If you compile the game now, you will be able to play it and see that it becomes progressively more difficult as the levels increase.



Summary
In this tutorial we changed the game’s logic so that it will become progressively harder as the player keeps playing. In the next tutorial, we will implement player lives and the ability to lose the game.