WEBVTT

00:00.150 --> 00:00.720
Hello again!

00:01.080 --> 00:03.870
In this video, we are going to add a ball to our game.

00:04.920 --> 00:09.810
We are going to do this by writing a sprite which can move between updates.

00:10.260 --> 00:16.620
So in one frame, it is going to have some position on the screen, and in the next frame, it will have a different

00:16.680 --> 00:17.220
position.

00:20.040 --> 00:23.940
There are going to be more than one of these sprites which can move around.

00:24.240 --> 00:26.580
So let's write a class to represent them.

00:27.480 --> 00:33.930
Moving underscore entity will be a subclass of entity, and it represents entities which can move on

00:33.930 --> 00:34.410
the screen.

00:35.250 --> 00:37.560
It has a member for the velocity.

00:37.860 --> 00:39.840
That is the amount and the direction

00:39.900 --> 00:45.240
that the entity will move between each update. And we will store that in a Vector2f.

00:45.900 --> 00:48.150
And this is also going to be an abstract base class.

00:51.120 --> 00:53.940
So the hierarchy, as we have it at the moment, looks like this.

00:54.510 --> 00:56.160
We have the entity base class.

00:56.910 --> 01:00.690
We have the concrete background class, which inherits from entity.

01:01.650 --> 01:05.040
We have moving_entity, which is also an abstract base class.

01:05.760 --> 01:09.690
And then we have the ball, which is a concrete child of moving_entity.

01:11.990 --> 01:14.300
So here is our entity hierarchy.

01:14.300 --> 01:19.130
We have the base class which we had before with the pure virtual member functions.

01:20.570 --> 01:24.290
We have the moving_entity class which inherits from entity.

01:24.980 --> 01:27.350
It has a member for the velocity.

01:28.250 --> 01:30.980
It does not implement any of the pure virtual functions.

01:31.310 --> 01:36.560
So this is itself an abstract base class. And any class which inherits from this has to implement

01:36.560 --> 01:37.910
the virtual member functions.

01:40.740 --> 01:42.030
In the ball class.

01:42.060 --> 01:49.080
This is quite similar to background, except now we inherit from moving_entity, and not just entity. Se have

01:49.080 --> 01:57.390
a static texture member again. We have a constructor and then we implement the virtual member functions.

02:00.300 --> 02:01.320
In the source file.

02:01.320 --> 02:02.610
The constructor is very similar.

02:03.000 --> 02:05.370
We load the image file into the texture.

02:05.700 --> 02:07.500
Then we load the texture into the sprite.

02:08.160 --> 02:13.740
We set the initial position of the sprite, and then we set the initial velocity.

02:15.070 --> 02:21.910
I have added another constant to the constants file. The game parameters. So that controls the speed

02:21.910 --> 02:22.390
of the ball.

02:22.720 --> 02:29.230
So this means the ball is going to move six pixels to the right, and six pixels downwards, on each update.

02:30.550 --> 02:35.950
Then when we update the position of the ball, we need to add this velocity to the present position.

02:36.700 --> 02:38.740
The sprite has a member function, which will do this.

02:39.010 --> 02:45.730
So we call sprite dot move and pass the velocity, and that will correctly calculate the new position. And

02:45.730 --> 02:48.430
then we draw the ball, in the same way that we did for the background.

02:48.760 --> 02:50.220
We get the window to do it for us.

02:52.330 --> 02:54.030
The main function is very similar.

02:54.040 --> 02:56.080
We include the ball header.

02:56.560 --> 03:01.090
The background code is the same as it was before, so I do not need to go over that again.

03:02.380 --> 03:05.230
We create the background object, then the ball object.

03:05.920 --> 03:08.410
We are going to put this in the middle of the screen, to start with.

03:09.820 --> 03:12.670
Then we have our usual set up and loop.

03:14.170 --> 03:19.570
When we get to the update, we update the background and now we also update the ball as well.

03:20.350 --> 03:24.760
Then we draw the background in the buffer, and we draw the ball in the buffer as well.

03:24.910 --> 03:28.210
Then, once we have finished building up the next frame, we can display it.

03:29.320 --> 03:30.580
So let's see what happens.

03:33.570 --> 03:33.780
Hmm.

03:34.170 --> 03:34.770
What happened there?

03:37.600 --> 03:39.310
(Just move myself over a bit)

03:41.390 --> 03:46.340
So the ball starts off in the middle. And it goes down six, and across six. And it keeps on going.

03:47.690 --> 03:52.430
You may need to slow down the video replay rate on your interface, to see this.

03:54.870 --> 03:55.340
So there it is.

03:55.950 --> 04:00.630
The problem is there is nothing to say that, when the ball reaches the edge of the screen, it must stop,

04:00.630 --> 04:01.980
or come back, or whatever.

04:02.550 --> 04:03.810
So that is something we need to add.

04:04.920 --> 04:05.220
Okay.

04:05.220 --> 04:06.300
So that is it for this video.

04:06.600 --> 04:07.410
I will see you next time.

04:07.410 --> 04:09.390
But until then, keep coding!
