Hello again!
In this video, we are going to add a ball to our game.
We are going to do this by writing a sprite which can move between updates.
So in one frame, it is going to have some position on the screen, and in the next frame, it will have a different
position.
There are going to be more than one of these sprites which can move around.
So let's write a class to represent them.
Moving underscore entity will be a subclass of entity, and it represents entities which can move on
the screen.
It has a member for the velocity.
That is the amount and the direction
that the entity will move between each update. And we will store that in a Vector2f.
And this is also going to be an abstract base class.
So the hierarchy, as we have it at the moment, looks like this.
We have the entity base class.
We have the concrete background class, which inherits from entity.
We have moving_entity, which is also an abstract base class.
And then we have the ball, which is a concrete child of moving_entity.
So here is our entity hierarchy.
We have the base class which we had before with the pure virtual member functions.
We have the moving_entity class which inherits from entity.
It has a member for the velocity.
It does not implement any of the pure virtual functions.
So this is itself an abstract base class. And any class which inherits from this has to implement
the virtual member functions.
In the ball class.
This is quite similar to background, except now we inherit from moving_entity, and not just entity. Se have
a static texture member again. We have a constructor and then we implement the virtual member functions.
In the source file.
The constructor is very similar.
We load the image file into the texture.
Then we load the texture into the sprite.
We set the initial position of the sprite, and then we set the initial velocity.
I have added another constant to the constants file. The game parameters. So that controls the speed
of the ball.
So this means the ball is going to move six pixels to the right, and six pixels downwards, on each update.
Then when we update the position of the ball, we need to add this velocity to the present position.
The sprite has a member function, which will do this.
So we call sprite dot move and pass the velocity, and that will correctly calculate the new position. And
then we draw the ball, in the same way that we did for the background.
We get the window to do it for us.
The main function is very similar.
We include the ball header.
The background code is the same as it was before, so I do not need to go over that again.
We create the background object, then the ball object.
We are going to put this in the middle of the screen, to start with.
Then we have our usual set up and loop.
When we get to the update, we update the background and now we also update the ball as well.
Then we draw the background in the buffer, and we draw the ball in the buffer as well.
Then, once we have finished building up the next frame, we can display it.
So let's see what happens.
Hmm.
What happened there?
(Just move myself over a bit)
So the ball starts off in the middle. And it goes down six, and across six. And it keeps on going.
You may need to slow down the video replay rate on your interface, to see this.
So there it is.
The problem is there is nothing to say that, when the ball reaches the edge of the screen, it must stop,
or come back, or whatever.
So that is something we need to add.
Okay.
So that is it for this video.
I will see you next time.
But until then, keep coding!