WEBVTT

00:00.120 --> 00:00.950
Hello again!

00:00.960 --> 00:05.490
In this video, we are going to modify the strength of the bricks in the game.

00:06.000 --> 00:12.000
We have just been through a couple of videos which were, maybe, a bit hard going. So I apologize for that,

00:12.390 --> 00:15.570
and I hope this one will be a bit more enjoyable.

00:17.560 --> 00:22.210
At the moment, when a ball hits the brick, the brick immediately disappears.

00:22.780 --> 00:23.860
We are going to change that.

00:23.860 --> 00:28.270
So the brick needs to be hit a few times before it is finally destroyed.

00:28.720 --> 00:34.270
In the meantime, the colour of the brick will change to indicate that it has been weakened.

00:36.460 --> 00:37.030
To do this,

00:37.030 --> 00:42.370
We are going to add a member to the brick class, which will indicate the strength of the brick.

00:42.610 --> 00:47.410
So every time the ball hits the brick, the brick is weakened. It loses strength.

00:47.830 --> 00:52.000
And if the strength goes down to zero, then the brick is destroyed.

00:54.640 --> 01:01.150
So far, we have only used the pre-defined colours from the SFML library, but we can choose our own.

01:01.720 --> 01:08.020
There is a Color class, with the American spelling. And this has the usual four components that you

01:08.020 --> 01:09.580
get with computer graphics.

01:09.790 --> 01:16.090
So we have the three primary colours: red, green and blue. And we can mix these in different proportions

01:16.090 --> 01:17.710
to get any colour we want.

01:18.550 --> 01:24.670
There is also this "alpha", which will control how much of the background is visible, through the colour.

01:26.890 --> 01:32.830
These components are represented by 8-bit unsigned integers. So they can have values between 0

01:32.830 --> 01:34.330
and 255.

01:34.930 --> 01:40.080
And these indicate the contribution that each component makes to the resulting colour.

01:41.000 --> 01:47.320
0 means there is no contribution and 255 means it is making the maximum possible.

01:48.190 --> 01:54.910
For example, if we have a colour where red is 255, green is 0 and blue is 0, then

01:54.910 --> 01:58.360
that will be a pure red colour, without any green or blue in it.

01:59.410 --> 02:08.080
If we have red 127, green 127 and blue 127, then that represents an equal mixture

02:08.080 --> 02:14.080
of each primary colour, in the middle of its intensity, and that will probably be some shade of grey.

02:16.060 --> 02:21.460
Alpha is the opacity of the colour. Or, if you like, the inverse transparency.

02:22.060 --> 02:28.030
If alpha is 0, then the colour is completely transparent, and we can see the background and nothing

02:28.030 --> 02:28.510
else.

02:29.410 --> 02:35.380
Alpha of 255 represents a completely opaque colour, so there is no contribution from the background at

02:35.380 --> 02:35.800
all.

02:36.760 --> 02:42.340
In cases like these, where we do not specify the alpha value, then the default is 255.

02:42.610 --> 02:46.720
So this red colour will not have any of the background coming through at all.

02:49.180 --> 02:54.250
We need to change the code which handles collisions between a ball and brick.

02:54.760 --> 02:57.910
So at the moment we are just destroying the brick automatically.

02:58.480 --> 03:05.140
Now we need to weaken the brick, and we do not destroy the brick unless the strength is equal to zero.

03:06.460 --> 03:08.820
And if the strength is not 0, we just leave it alone.

03:08.860 --> 03:11.140
So it is going to be drawn next time.

03:11.620 --> 03:16.270
In the update() memory function of brick, where currently we are doing nothing, we need to set the colour

03:16.270 --> 03:19.300
of the brick. And that will depend on its strength.

03:21.740 --> 03:26.870
In the constants file, I have added another parameter: brick_strength.

03:27.290 --> 03:32.090
So this will represent how many times a brick can be hit before it is destroyed.

03:32.480 --> 03:37.550
So we are saying here that a brick needs to be hit 3 times before it disappears.

03:39.770 --> 03:43.730
In the brick header, we have this member for the strength.

03:43.850 --> 03:48.680
So this will tell us how many more impacts the brick can take, before it is destroyed.

03:49.370 --> 03:53.220
Initially, it has this brick_strength value so it can take 3 hits.

03:53.240 --> 03:56.050
If there is a collision, then the strength will go down to 2.

03:56.060 --> 03:57.710
So it can take 2 more hits.

03:58.070 --> 04:03.620
The next collision takes it down to 1, so it can take one more hit. And then the next collision

04:03.620 --> 04:06.650
will take this down to 0. And then it is destroyed.

04:07.970 --> 04:13.430
We also have some helper functions for working with this member. So we can set the member.

04:13.730 --> 04:20.450
We can weaken the brick by decrementing the strength. And we can find out if the brick needs to be destroyed.

04:23.600 --> 04:29.150
In the source code, we create our colour objects of the sf::Color class.

04:29.750 --> 04:33.110
So these are going to represent the different states of the brick.

04:33.470 --> 04:36.710
This one will represent a brick which has not been hit at all.

04:36.740 --> 04:38.600
So it can take 3 impacts.

04:39.710 --> 04:42.890
This will represent a brick which can take 2 impacts.

04:43.190 --> 04:46.400
And this will represent a brick which can take 1 impact.

04:46.640 --> 04:48.740
So it is going to be destroyed next time.

04:49.910 --> 04:52.550
We are going to make all these, different shades of green.

04:53.330 --> 04:55.580
These are all going to be pure green.

04:55.580 --> 04:57.050
So red is 0.

04:57.080 --> 04:59.530
Blue is 0, and green is 255.

04:59.930 --> 05:05.840
But we are going to use different alpha values, to indicate the different states. With a brick which has not

05:05.840 --> 05:06.740
been hit at all,

05:06.770 --> 05:08.290
alpha will be 255.

05:08.300 --> 05:10.940
So that is going to be a bright green with no background.

05:11.690 --> 05:14.240
If it has been hit once, the opacity is a bit lower.

05:14.330 --> 05:17.150
So we are going to get some contribution from the background.

05:17.480 --> 05:19.070
And that will give a medium green.

05:19.640 --> 05:24.140
And then, when it is getting near the end of its life, there is quite a lot of background.

05:24.920 --> 05:26.660
And that will be a dull green.

05:28.040 --> 05:33.230
Then we have our member functions for setting the strength, decrementing it, and comparing it for

05:33.230 --> 05:34.790
equality to 0.

05:36.940 --> 05:43.870
In the update() member function, we set the colour of the brick. And that will depend on its strength.

05:47.870 --> 05:49.460
In the interactions code.

05:49.460 --> 05:51.350
We need to change this.

05:51.350 --> 05:58.250
So first we weaken the block, and then we check if the block needs to be destroyed. And only then do we

05:58.250 --> 05:59.030
destroy it.

06:01.220 --> 06:02.840
So let's try this out.

06:04.250 --> 06:09.650
So you can see the bricks become darker when they are hit for the first time. And then they become much

06:09.650 --> 06:11.330
darker when they are hit for the second time.

06:13.740 --> 06:16.410
And on the third impact, if we can get one.

06:16.980 --> 06:21.210
(Whoops! I'm really not very good at this game.)

06:24.280 --> 06:25.510
(I am trying to talk at the same time.)

06:25.510 --> 06:26.440
(That is my problem!)

06:26.740 --> 06:27.270
There you are.

06:27.280 --> 06:29.410
So you can see on the third hit, it disappears.

06:30.060 --> 06:31.630
Okay, so that is it for this video.

06:31.720 --> 06:32.590
I will see you next time.

06:32.590 --> 06:34.960
But until then, keep coding!
