Hello again! In this video, we are going to add the paddle to our game. The paddle is this thing. Or you
could call it a bat, perhaps. This sits at the bottom of the screen.
It moves to the left
and the right, when the user presses one of the keys. And if the ball hits it, it changes the action. So
we can move this paddle around, and use it to change the direction of the ball.
The code to make the paddle appear on the screen is very similar to the code that we initially write for
making the ball appear on the screen.
The only potentially tricky part is getting the coordinates correct.
So we want the paddle to appear at the bottom of the screen.
So y equals zero at the top and y equals window height at the bottom.
The Sprite constructor takes the coordinates of the origin of the bounding rectangle.
So if we use y equals window height, then the top left hand corner of the paddle will be on the bottom
of the window. And the rest of the paddle will be below the bottom of the window, so it will not be visible.
So we need to correct for that.
So we need to subtract the height of the paddle from the height of the window, and that will put the
origin of this window in the correct place.
So you may want to have a go at doing this yourself.
The code is very similar to the code that we had originally for making the ball appear on the screen.
Do not worry about making the paddle move, or interact with the ball or the keyboard, just yet.
We will do that later on.
So I will give you an opportunity to pause the video and try that for yourselves.
So here is my solution.
I have added two more constants, for the height and the width of the paddle. A paddle class which inherits
from moving_entity. And it is very, very similar to the ball class.
Obviously we load up the paddle image, and not the ball image. And also, we set the initial velocity
to be zero, because the paddle does not move unless the user presses one of the keys.
And for update(),
we are not doing anything special, just yet. And draw() is the same again.
In the main source code, we #include "paddle.h".
We create an object of the paddle.
The x coordinate is half the width of the window, and the y coordinate is the window height minus the paddle
height,
as I explained in the slide.
Then we do all the usual things.
And when we update the graphics, we update the paddle as well.
And for displaying the graphics, we need to draw the paddle before we display the window.
So there it is.
We have our paddle.
And so as you can see, it does not interact with the ball just yet.
And if I press the arrow keys, then nothing happens.
But we can add that later on.
Okay, so that is it for this feature.
I will see you next time.
But until then,
keep coding!