Hello again!
In this video, we are going to look at how to make our paddle move across the bottom of the screen.
So let's try and specify what we want.
When the user presses the left arrow key, the paddle will move to the left, when the screen is next updated.
And if the user presses the right arrow key, the paddle will move to the right.
If the user presses any other key, we just ignore it.
And this is only for left or right movement.
The paddle is not going to move up or down.
The obvious place to make these changes is in the update()
member function of the paddle class, because that determines how the paddle will appear, the next time that the
frame is displayed.
So in this function, we are going to check for the key press, and then we are going to set the "x" member
of the velocity, to make the paddle move left or right.
And in fact, I think you could actually do this yourselves.
There is code in the main() function, which shows how to check for a key press. If you go to the SFML
website and look through the documentation, you should be able to find which keys correspond to the
left and right arrow keys. And then you can make your changes in the update() member function.
So I will let you pause the video, if you are intending to do that.
I have added another constant, which is going to be the paddle speed.
So the paddle will move 8 pixels between updates, when it is moving.
I have actually added a new member function, to respond to the input from the player.
This is a private member function of the class.
You could argue that this is something which could be useful for any moving entity. So it should perhaps
be a protected member function of the moving entity_class. In this game,
the paddle is going to be the only thing which responds to keyboard input, so it makes sense to have
it as a private member.
If we are watching a different game where different entities can respond to key put input,
then perhaps we could organize the code differently.
I have also made one other small change, which is to change the origin of the sprite. By default,
this will be the top left hand corner of the bounding rectangle.
I find it easier to think in terms of the centre of the sprite.
So I have made this change.
But if you do not like that, you can just ignore that, or remove it from your code.
And then in the update() member function, I just call this new member function, which will deal with keyboard input.
The draw() function is the same.
And then here is this
process_player_input() member function.
We start by checking for the left elbow. And then if, it has been pressed, we set the "x" member of the
velocity to be minus the paddle speed.
So that is going to move 8 pixels to the left, the next time the display is updated.
If the user presses the right key, then we set the "x" member to be plus 8 pixels.
We also need to check that we have not reached the edge of the window. With the ball,
we made the ball bounce off when it reached the edge. With the paddle,
we do not want to do that.
Instead, the paddle is just going to stop. So it will sit there and wait, until the user presses the
key to make it go in the other direction.
So if we reach the left hand edge of the screen, then the paddle will stop until the user presses the
right arrow.
And if there is no key press, then we just set the velocity to be zero.
And now, if I press the left key, it moves to the left.
If I press the right key, it moves to the right.
If I hold the left key, it keeps on going until it reaches the edge.
And then for the right. I am actually using the centre, so the paddle can go halfway into the edge.
The reason for that is, there are some variations, where the way that the ball bounces depends on which
part of the pebble it hits.
Okay, that is it for this video.
I will see you next time.
But until then, keep coding!