WEBVTT

00:00.150 --> 00:06.960
Hello again! In this video, we are going to add the paddle to our game. The paddle is this thing. Or you

00:06.960 --> 00:10.830
could call it a bat, perhaps. This sits at the bottom of the screen.

00:11.310 --> 00:12.290
It moves to the left

00:12.390 --> 00:18.630
and the right, when the user presses one of the keys. And if the ball hits it, it changes the action. So

00:18.630 --> 00:22.500
we can move this paddle around, and use it to change the direction of the ball.

00:23.880 --> 00:29.010
The code to make the paddle appear on the screen is very similar to the code that we initially write for

00:29.010 --> 00:30.990
making the ball appear on the screen.

00:33.320 --> 00:37.400
The only potentially tricky part is getting the coordinates correct.

00:38.150 --> 00:40.820
So we want the paddle to appear at the bottom of the screen.

00:41.420 --> 00:45.350
So y equals zero at the top and y equals window height at the bottom.

00:46.310 --> 00:51.020
The Sprite constructor takes the coordinates of the origin of the bounding rectangle.

00:51.620 --> 00:57.290
So if we use y equals window height, then the top left hand corner of the paddle will be on the bottom

00:57.290 --> 01:02.570
of the window. And the rest of the paddle will be below the bottom of the window, so it will not be visible.

01:04.130 --> 01:05.950
So we need to correct for that.

01:06.020 --> 01:11.720
So we need to subtract the height of the paddle from the height of the window, and that will put the

01:11.720 --> 01:14.150
origin of this window in the correct place.

01:15.290 --> 01:17.360
So you may want to have a go at doing this yourself.

01:17.750 --> 01:21.710
The code is very similar to the code that we had originally for making the ball appear on the screen.

01:22.130 --> 01:27.020
Do not worry about making the paddle move, or interact with the ball or the keyboard, just yet.

01:27.350 --> 01:28.250
We will do that later on.

01:28.970 --> 01:32.960
So I will give you an opportunity to pause the video and try that for yourselves.

01:35.400 --> 01:36.930
So here is my solution.

01:37.380 --> 01:44.160
I have added two more constants, for the height and the width of the paddle. A paddle class which inherits

01:44.160 --> 01:48.420
from moving_entity. And it is very, very similar to the ball class.

01:50.580 --> 01:56.970
Obviously we load up the paddle image, and not the ball image. And also, we set the initial velocity

01:56.970 --> 02:01.680
to be zero, because the paddle does not move unless the user presses one of the keys.

02:03.560 --> 02:05.180
And for update(),

02:05.180 --> 02:09.590
we are not doing anything special, just yet. And draw() is the same again.

02:11.870 --> 02:15.230
In the main source code, we #include "paddle.h".

02:15.230 --> 02:18.470
We create an object of the paddle.

02:19.010 --> 02:26.120
The x coordinate is half the width of the window, and the y coordinate is the window height minus the paddle

02:26.120 --> 02:26.450
height,

02:26.870 --> 02:28.550
as I explained in the slide.

02:30.530 --> 02:32.510
Then we do all the usual things.

02:37.050 --> 02:40.230
And when we update the graphics, we update the paddle as well.

02:40.590 --> 02:44.790
And for displaying the graphics, we need to draw the paddle before we display the window.

02:47.470 --> 02:48.100
So there it is.

02:48.160 --> 02:49.030
We have our paddle.

02:49.720 --> 02:52.380
And so as you can see, it does not interact with the ball just yet.

02:52.390 --> 02:56.260
And if I press the arrow keys, then nothing happens.

02:56.500 --> 02:57.550
But we can add that later on.

02:58.210 --> 02:59.490
Okay, so that is it for this feature.

02:59.890 --> 03:00.700
I will see you next time.

03:00.700 --> 03:01.630
But until then,

03:02.600 --> 03:03.200
keep coding!
