First Step Pascal - Sprites and Movement

Now the Ball image and the Paddle image both move, and any object like this that will move or perform some action will be saved as a "Sprite". So we need to declare both these things as "Sprites" and also while we are at it, set the x and y coordinates of the objects. To do so open up the GameLogic file and the following variables, then underneath LoadResources add the following code:

Now to make these appear we have to draw them within the loop. We can add this below where we draw the background picture. Add the following 2 lines of text:

If you compile and run the game now to view the program the paddle and the ball in front of the background should appear but will not be moving yet.

Getting the ball moving
In order to get our ball moving we need to add the following code below the y coordinate for the ball like this:

Remember you already have the first line of code from above. This code sets the speed at which the ball moves, it will increase it's x and y position on the screen by 1 pixel every time it runs.

Then we need to add something to our loop so every time the program goes through the loop the ball moves a bit and not just once when the program starts. Add this line below SwinGame.Graphics.ClearScreen in the Do loop:

Now when you run the program (remember to compile it first) you should see the ball move off to the bottom right hand side of the screen... into nowhere....really fast...but we can fix that next by adding some tests to our game loop and changing the speed from 1 to 0.5 as well if it is too fast.

Testing for the outside border
To get the ball bouncing off the outside of the screen we need to add some tests to our game loop. We will use four "If" statements to test for the four different walls. Remember that our screen is 800x600 with the top left corner coordinates equaling 0,0 and bottom right 800,600.

When an outside border is hit we want the ball to bounce off. In game programming terms this means reversing the y and x movement depending on whether it has hit a top/bottom or side border. Top and bottom borders we reverse the Y movement, side border we reverse the X movement. Add the following If statement to your game loop below "MoveSprite(ball)".

This code tests the right hand side border, you still need to test for the other 3 borders which means adding 3 more If statements below that one.

Now you do this...
See if you can figure out what the other 3 if statements should be. If you can't figure it out have a look at the solution here.