Breakout Tutorial 3

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.

We are now going to create the paddle which the player will be able to use to control the ball. In this version of the game we are going to use the keyboard as the controller for the paddle, however you can make it follow the mouse if you wish.

Adding resources
First we need to open up GameResources.pas again, like in the previous tutorial, and add the paddle image:

Also make sure paddle.png is in the Resources\Images folder. If you unzipped all the images from the zip file at the start of the tutorial into the Resources\Images folder, everything will work.

Setting up the Player
We need to add some player constants. These constants will tell the game a few crucial details, like how many lives the player should have by default, and the size of the paddle. We have also included a playerY constant as the Y coordinate of the paddle is fixed (we can’t move the paddle up and down). In the "const" section of GameLogic.pas, add the following constants:

Now we need a record for the player. Above the var section, add the following code:

Finally, we need to create an instance of this type in memory. Under the var heading, add the following line of code:

Initialising the Player
We’re going to create two functions to initialize the player. Firstly InitialisePlayer will reset the player’s health, score, level etc. to the defaults, as well as create the player’s sprite. This will call another function MovePlayerToDefaultPosition, which is fairly self explanatory.

Place this code after the variables have been declared (var block) but before procedure Main:

Drawing the Paddle
We now need to update the game loop so it draws the paddle. In the Main procedure, before InitialiseBall, add this line of code:

Now in the game loop (the section between the Repeat and Until statements), add the following code after the ProcessEvents line:

This code is fairly simple. If the player is pressing the Left key, then the paddle will start moving to the left, and if they press the Right key, the paddle will move to the right. The next series of if statements checks to make sure that the paddle is inside the window.

Move down to the end of the code which checks if the ball has hit the bottom of the screen (it should finish with an end; statement) and add the following code:

This code checks to see if the ball has hit the paddle. If it has, it will bounce off.

Finally we need code to actually draw the paddle to the screen. After the line ClearScreen(ColorBlack);, add these lines of code:

When you compile and run the game now, you should see a paddle. You can move the paddle to the left or right with the arrow keys. The ball will bounce off the paddle.

Summary
In this tutorial, we created the player's paddle, and added code so that it can be controlled with the arrow keys. In the next tutorial, we will create the bricks.