WEBVTT

00:00.060 --> 00:00.630
Hello again!

00:00.930 --> 00:05.310
In this video, we are going to look at how to make our paddle move across the bottom of the screen.

00:07.660 --> 00:09.820
So let's try and specify what we want.

00:10.390 --> 00:16.780
When the user presses the left arrow key, the paddle will move to the left, when the screen is next updated.

00:17.650 --> 00:22.270
And if the user presses the right arrow key, the paddle will move to the right.

00:23.110 --> 00:26.170
If the user presses any other key, we just ignore it.

00:26.920 --> 00:29.680
And this is only for left or right movement.

00:30.760 --> 00:33.010
The paddle is not going to move up or down.

00:35.120 --> 00:38.210
The obvious place to make these changes is in the update()

00:38.500 --> 00:43.850
member function of the paddle class, because that determines how the paddle will appear, the next time that the

00:43.850 --> 00:45.050
frame is displayed.

00:45.890 --> 00:50.770
So in this function, we are going to check for the key press, and then we are going to set the "x" member

00:50.960 --> 00:54.350
of the velocity, to make the paddle move left or right.

00:55.370 --> 00:58.520
And in fact, I think you could actually do this yourselves.

00:59.060 --> 01:04.340
There is code in the main() function, which shows how to check for a key press. If you go to the SFML

01:04.550 --> 01:10.220
website and look through the documentation, you should be able to find which keys correspond to the

01:10.580 --> 01:15.410
left and right arrow keys. And then you can make your changes in the update() member function.

01:15.950 --> 01:20.320
So I will let you pause the video, if you are intending to do that.

01:25.070 --> 01:29.330
I have added another constant, which is going to be the paddle speed.

01:29.840 --> 01:33.740
So the paddle will move 8 pixels between updates, when it is moving.

01:36.470 --> 01:41.450
I have actually added a new member function, to respond to the input from the player.

01:42.140 --> 01:44.830
This is a private member function of the class.

01:45.680 --> 01:52.580
You could argue that this is something which could be useful for any moving entity. So it should perhaps

01:52.580 --> 01:57.560
be a protected member function of the moving entity_class. In this game,

01:58.220 --> 02:03.410
the paddle is going to be the only thing which responds to keyboard input, so it makes sense to have

02:03.410 --> 02:04.460
it as a private member.

02:05.090 --> 02:09.620
If we are watching a different game where different entities can respond to key put input,

02:09.980 --> 02:11.930
then perhaps we could organize the code differently.

02:15.830 --> 02:22.700
I have also made one other small change, which is to change the origin of the sprite. By default,

02:22.790 --> 02:25.820
this will be the top left hand corner of the bounding rectangle.

02:26.510 --> 02:29.540
I find it easier to think in terms of the centre of the sprite.

02:30.170 --> 02:31.910
So I have made this change.

02:32.510 --> 02:36.410
But if you do not like that, you can just ignore that, or remove it from your code.

02:38.930 --> 02:45.860
And then in the update() member function, I just call this new member function, which will deal with keyboard input.

02:48.250 --> 02:49.750
The draw() function is the same.

02:50.470 --> 02:52.120
And then here is this

02:52.120 --> 02:53.650
process_player_input() member function.

02:56.010 --> 03:03.750
We start by checking for the left elbow. And then if, it has been pressed, we set the "x" member of the

03:03.900 --> 03:06.960
velocity to be minus the paddle speed.

03:07.590 --> 03:12.270
So that is going to move 8 pixels to the left, the next time the display is updated.

03:13.710 --> 03:20.460
If the user presses the right key, then we set the "x" member to be plus 8 pixels.

03:21.930 --> 03:27.840
We also need to check that we have not reached the edge of the window. With the ball,

03:27.840 --> 03:31.090
we made the ball bounce off when it reached the edge. With the paddle,

03:31.120 --> 03:32.250
we do not want to do that.

03:32.610 --> 03:38.190
Instead, the paddle is just going to stop. So it will sit there and wait, until the user presses the

03:38.190 --> 03:39.750
key to make it go in the other direction.

03:40.170 --> 03:45.630
So if we reach the left hand edge of the screen, then the paddle will stop until the user presses the

03:45.660 --> 03:46.430
right arrow.

03:48.380 --> 03:52.430
And if there is no key press, then we just set the velocity to be zero.

03:56.740 --> 03:59.560
And now, if I press the left key, it moves to the left.

04:00.280 --> 04:02.470
If I press the right key, it moves to the right.

04:03.310 --> 04:06.610
If I hold the left key, it keeps on going until it reaches the edge.

04:07.840 --> 04:13.630
And then for the right. I am actually using the centre, so the paddle can go halfway into the edge.

04:14.530 --> 04:19.660
The reason for that is, there are some variations, where the way that the ball bounces depends on which

04:19.660 --> 04:20.950
part of the pebble it hits.

04:21.820 --> 04:23.350
Okay, that is it for this video.

04:23.770 --> 04:24.610
I will see you next time.

04:24.610 --> 04:26.860
But until then, keep coding!
