Hello again!
In this video, we are going to modify the strength of the bricks in the game.
We have just been through a couple of videos which were, maybe, a bit hard going. So I apologize for that,
and I hope this one will be a bit more enjoyable.
At the moment, when a ball hits the brick, the brick immediately disappears.
We are going to change that.
So the brick needs to be hit a few times before it is finally destroyed.
In the meantime, the colour of the brick will change to indicate that it has been weakened.
To do this,
We are going to add a member to the brick class, which will indicate the strength of the brick.
So every time the ball hits the brick, the brick is weakened. It loses strength.
And if the strength goes down to zero, then the brick is destroyed.
So far, we have only used the pre-defined colours from the SFML library, but we can choose our own.
There is a Color class, with the American spelling. And this has the usual four components that you
get with computer graphics.
So we have the three primary colours: red, green and blue. And we can mix these in different proportions
to get any colour we want.
There is also this "alpha", which will control how much of the background is visible, through the colour.
These components are represented by 8-bit unsigned integers. So they can have values between 0
and 255.
And these indicate the contribution that each component makes to the resulting colour.
0 means there is no contribution and 255 means it is making the maximum possible.
For example, if we have a colour where red is 255, green is 0 and blue is 0, then
that will be a pure red colour, without any green or blue in it.
If we have red 127, green 127 and blue 127, then that represents an equal mixture
of each primary colour, in the middle of its intensity, and that will probably be some shade of grey.
Alpha is the opacity of the colour. Or, if you like, the inverse transparency.
If alpha is 0, then the colour is completely transparent, and we can see the background and nothing
else.
Alpha of 255 represents a completely opaque colour, so there is no contribution from the background at
all.
In cases like these, where we do not specify the alpha value, then the default is 255.
So this red colour will not have any of the background coming through at all.
We need to change the code which handles collisions between a ball and brick.
So at the moment we are just destroying the brick automatically.
Now we need to weaken the brick, and we do not destroy the brick unless the strength is equal to zero.
And if the strength is not 0, we just leave it alone.
So it is going to be drawn next time.
In the update() memory function of brick, where currently we are doing nothing, we need to set the colour
of the brick. And that will depend on its strength.
In the constants file, I have added another parameter: brick_strength.
So this will represent how many times a brick can be hit before it is destroyed.
So we are saying here that a brick needs to be hit 3 times before it disappears.
In the brick header, we have this member for the strength.
So this will tell us how many more impacts the brick can take, before it is destroyed.
Initially, it has this brick_strength value so it can take 3 hits.
If there is a collision, then the strength will go down to 2.
So it can take 2 more hits.
The next collision takes it down to 1, so it can take one more hit. And then the next collision
will take this down to 0. And then it is destroyed.
We also have some helper functions for working with this member. So we can set the member.
We can weaken the brick by decrementing the strength. And we can find out if the brick needs to be destroyed.
In the source code, we create our colour objects of the sf::Color class.
So these are going to represent the different states of the brick.
This one will represent a brick which has not been hit at all.
So it can take 3 impacts.
This will represent a brick which can take 2 impacts.
And this will represent a brick which can take 1 impact.
So it is going to be destroyed next time.
We are going to make all these, different shades of green.
These are all going to be pure green.
So red is 0.
Blue is 0, and green is 255.
But we are going to use different alpha values, to indicate the different states. With a brick which has not
been hit at all,
alpha will be 255.
So that is going to be a bright green with no background.
If it has been hit once, the opacity is a bit lower.
So we are going to get some contribution from the background.
And that will give a medium green.
And then, when it is getting near the end of its life, there is quite a lot of background.
And that will be a dull green.
Then we have our member functions for setting the strength, decrementing it, and comparing it for
equality to 0.
In the update() member function, we set the colour of the brick. And that will depend on its strength.
In the interactions code.
We need to change this.
So first we weaken the block, and then we check if the block needs to be destroyed. And only then do we
destroy it.
So let's try this out.
So you can see the bricks become darker when they are hit for the first time. And then they become much
darker when they are hit for the second time.
And on the third impact, if we can get one.
(Whoops! I'm really not very good at this game.)
(I am trying to talk at the same time.)
(That is my problem!)
There you are.
So you can see on the third hit, it disappears.
Okay, so that is it for this video.
I will see you next time.
But until then, keep coding!