WEBVTT

00:00.000 --> 00:28.000
All right. Welcome back. It's time to finish up the scheme by implementing scoring and the resetting of the ball when it passes either one of the platforms. So, let's get on with it.

00:28.000 --> 00:43.000
I'm going to start by resetting the ball in the dead center of the screen and pointing it in a random direction when it passes either the blue bar or the top. So, when it goes upwards up until maybe here or goes down, up until maybe here.

00:44.000 --> 01:01.000
All right. So, we will implement a small if condition that if the y coordinate that is the vertical coordinate of the ball is greater than something 160 or something like that or less than 160, then it will reset to the center of the screen and pointed in a random direction.

01:01.000 --> 01:12.000
So, I'm going to go to control and I'm going to bring in the IFF block and in the diamond-shaped hole for the IFF block, I'm going to go to operators and I'm going to bring in this diamond-shaped greater than.

01:12.000 --> 01:24.000
And in the first space of the diamond-shaped block, I'm going to go to motion and I'm going to bring in the y position. That is the vertical position of the sprite and in the right space, I'm going to put in 140.

01:24.000 --> 01:38.000
So, if the y coordinate of the basketball is greater than 140, so up until here we will reset the ball to its original position, the center of the screen x0 and y0 and point in another random direction.

01:38.000 --> 01:46.000
So, I'm going to detach the forever block, right-click on the first blue block, go to x and y, hit duplicate.

01:46.000 --> 01:54.000
So, this gives me the two blocks to reset the ball and I'm going to put them inside the IFF block, I'm going to reattach the forever block again.

01:54.000 --> 02:05.000
So, if I move the basketball right up the top of the stage, so the coordinate is 150, 155, and click this detached IFF block, the ball will reset to the center of the screen.

02:05.000 --> 02:14.000
See? Now I'm going to right-click on this. If block it, I'm going to duplicate it because I'm going to replicate this condition for the down position as well.

02:14.000 --> 02:27.000
So, if the y position in this case is less than 140, which means down here, something like that, I'm going to do the same thing, reset the ball to the center of the screen and point in a random direction.

02:27.000 --> 02:33.000
So, I'm going to take the second diamond-shaped green block and I'm going to remove it and I'm going to bring in another one.

02:33.000 --> 02:38.000
So, I'm going to go to operators and I'm going to pick in the less than operator.

02:38.000 --> 02:47.000
I'm going to go to motion, scroll down and take the rounded y position and in the second space, I'm going to fill in 140.

02:47.000 --> 02:53.000
Okay? And then I'm going to click on this block to make sure that the ball can reset to its original position.

02:53.000 --> 03:05.000
Alright, now what I'm going to do is I'm going to take these two IFF blocks and I'm going to put them right before if on edge bounce, so I'm going to put them inside the forever loop, so if an edge bounce is going to be the last block.

03:06.000 --> 03:13.000
Alright, so the code here has gotten quite big, so the basketball now has a lot of conditions and moves 10 steps.

03:13.000 --> 03:22.000
It checks if it touches either the down bar or the upper, it also checks if it's too far up or too far down, in which case it will reset to its original position.

03:22.000 --> 03:28.000
So, if I hit the flag again, the ball should reset to the center of the screen on its own, see?

03:28.000 --> 03:33.000
So, it bounces off of the edges of the screen, it also bounces off of the platforms.

03:33.000 --> 03:41.000
But if it's in any case, too far up or too far down on the stage, it will reset to its original position and go in another direction.

03:41.000 --> 03:44.000
Alright, so this is cool.

03:44.000 --> 03:54.000
Alright, so the ball can reset to its original position, it can bounce off of the edges of the stage and it can also bounce off of the platforms, which is very, very nice.

03:54.000 --> 03:58.000
There is one last thing that we need to implement and that is scoring.

03:58.000 --> 04:02.000
For that, we will need to learn something completely new and that is variables.

04:02.000 --> 04:09.000
So, I want you to go to variables, this dark orange section from the code, and I want you to click on this button and make a variable.

04:09.000 --> 04:18.000
I'm going to name this variable read score, check that for all sprites checkbox, and I'm going to make another variable called blue score again for all sprites and click.

04:18.000 --> 04:21.000
Okay, so what is a variable?

04:21.000 --> 04:30.000
Well, a variable is just a piece of information that can be changed in time and we will program this variable to be changed when the basketball either goes too far up or too far down.

04:30.000 --> 04:34.000
If it goes too far up, we will increase the red score.

04:34.000 --> 04:39.000
If it goes too far down, we will increase the blue score and that is basically what a variable is.

04:39.000 --> 04:47.000
So, I will need that when the flag is clicked, we need the blue score to be zero as well as the red score because that's the beginning of the game.

04:47.000 --> 04:52.000
And when the Y position of the basketball goes too far up, we will increase the red score by one.

04:52.000 --> 04:56.000
And if it's too far down, we will increase the blue score by one.

04:56.000 --> 04:58.000
So, here's what I'm going to do.

04:58.000 --> 04:59.000
Okay?

04:59.000 --> 05:04.000
I'm going to take this orange set blue score to zero, and I'm going to pull it into the open space.

05:04.000 --> 05:09.000
I'm going to duplicate it, and in the second block, I'm going to select red score.

05:09.000 --> 05:15.000
So, we are setting both the blue score and the red scores to zero, and I'm going to pull them both below when flag clicked.

05:16.000 --> 05:22.000
So, this is the first thing that the computer will do when the flag is clicked, we will reset the blue and red scores to zero.

05:22.000 --> 05:28.000
Now, when the white position is bigger than 140, that means the basketball is too far up.

05:28.000 --> 05:30.000
We need to increase the red score by one.

05:30.000 --> 05:37.000
So, I'm going to pull this orange change score by one, and I'm going to pull it inside the IFY position greater than 140.

05:37.000 --> 05:41.000
Obviously, I'm going to select red square because that's what's going to change by one.

05:41.000 --> 05:46.000
If the Y position is bigger than 140, then the red score deserves a point.

05:46.000 --> 05:53.000
Then, I'm going to do the same for the blue score by pulling the orange block in the other if the Y position is less than 140,

05:53.000 --> 05:58.000
and this time, changing the blue score by one is correct because the blue scored a point.

05:58.000 --> 06:06.000
And then, just for visual reasons, I'm going to put in these dashboards for the red score to be in the red space, and the blue score to be on top in the blue space.

06:07.000 --> 06:09.000
OK, and that is basically the game.

06:09.000 --> 06:12.000
Let's hit on the green flag, and let's play a little bit.

06:12.000 --> 06:14.000
So, we have blue scoring like crazy.

06:14.000 --> 06:16.000
That's because we aren't actually playing.

06:16.000 --> 06:21.000
So, we have a fully functional game where both players can catch the ball and make it reflect into one another.

06:21.000 --> 06:24.000
All right, so we have to play a game.

06:24.000 --> 06:26.000
They can actually score points now.

06:26.000 --> 06:27.000
All right.

06:27.000 --> 06:31.000
So, I stopped the game, and I have to confess that I did play in the meantime a little bit.

06:31.000 --> 06:33.000
So, it's quite fun.

06:33.000 --> 06:37.000
But there are a couple of things that I would like to improve on this game before we wrap up,

06:37.000 --> 06:40.000
because there are a number of things that I'm annoyed about.

06:40.000 --> 06:45.000
First of all, notice that when the ball starts in the middle of the screen and scores a point for one of the players,

06:45.000 --> 06:50.000
it usually scores multiple points because the players don't have the time to adjust.

06:50.000 --> 06:52.000
Let me show you.

06:52.000 --> 06:58.000
So, if I hit the flag and one of the players, let's say scores a point, then the ball restarts so, so quickly,

06:58.000 --> 07:01.000
see that the players don't have the time to adjust.

07:01.000 --> 07:07.000
So, I would like to make the ball wait for a couple of seconds here in the center of the screen so that the players have the time to prepare.

07:07.000 --> 07:08.000
All right.

07:08.000 --> 07:13.000
So, one of the things that I would like to improve here is to make the ball wait for a couple of seconds.

07:13.000 --> 07:16.000
So, here's what I'm going to do in the basketball sprite.

07:16.000 --> 07:21.000
Of course, I am going to add a weight block when the ball resets to the original position.

07:21.000 --> 07:27.000
So, I'm going to go to control the orange section, and I'm going to bring this very first wait for one second.

07:28.000 --> 07:34.000
Block. I'm going to pull it right before the forever loop, and I'm going to make the wait time two two seconds.

07:34.000 --> 07:41.000
So, when the game starts, the ball will stop in the center of the screen for two seconds, and give the players time to adjust.

07:41.000 --> 07:45.000
And I'm going to do the same when the ball restarts in its original position.

07:45.000 --> 07:49.000
So, here in this F condition, and in the last F condition as well.

07:49.000 --> 07:57.000
So, I'm going to pull this wait one second block right here in the if I position greater than 140, make the wait time two seconds as well,

07:57.000 --> 08:02.000
and bring it again in the last if condition, and as before, wait for two seconds.

08:02.000 --> 08:04.000
So, let's see the difference now.

08:04.000 --> 08:09.000
So, if I hit the flag again, notice that the ball waits for a couple of seconds before it starts shooting,

08:09.000 --> 08:13.000
and again, when one of the players scores a point, which is exactly what we wanted.

08:14.000 --> 08:20.000
The second thing that I would like to add to this game is a little bit of challenge because if the ball moves at the same speed,

08:20.000 --> 08:24.000
it can get pretty boring after you play with your friends for five minutes or something.

08:24.000 --> 08:26.000
So, let's add a little bit of fun.

08:26.000 --> 08:35.000
Let's say that when the ball touches one of the platforms, it might increase its speed, so go a little faster, which makes it an extra challenge for the other player to catch it.

08:35.000 --> 08:42.000
If they do catch it, then it becomes a greater challenge for the other player to catch the ball because it starts rolling at ever greater speeds.

08:42.000 --> 08:53.000
So, I would like to add this challenge by implementing an extra variable for the ball, and make it go ever faster when it touches one of the platforms until it goes so fast that one of the players has to lose.

08:53.000 --> 08:56.000
Alright, so here's what I'm going to do.

08:56.000 --> 09:03.000
I'm going to go to this variable section, the dark orange, and I'm going to make an extra variable, and I'm going to name speed.

09:03.000 --> 09:06.000
And this will be a private variable for this sprite only.

09:06.000 --> 09:09.000
This is for the basketball sprite I'm going to hit.

09:09.000 --> 09:15.000
Okay, and I'm going to set the speed of the basketball to be the number of steps that we now move the ball by, which is 10.

09:15.000 --> 09:21.000
So, I'm going to bring this orange set block, and instead of a blue score, I'm going to set the speed to be 10.

09:21.000 --> 09:28.000
Alright, and for the ball, instead of moving 10 steps, we're going to move it by speed steps.

09:28.000 --> 09:32.000
So, speed will be the number of steps that the ball will move on the stage.

09:32.000 --> 09:36.000
So, until now, nothing has actually changed.

09:36.000 --> 09:43.000
We created a variable whose value is 10, and we're moving the ball by speed steps, which is 10 steps as it was before.

09:43.000 --> 09:47.000
But the nice thing about this variable is that we can change it with time.

09:47.000 --> 09:52.000
So, when the ball touches one of the platforms, we might want to increase the speed.

09:52.000 --> 10:00.000
So, let's say that in these if blocks in, if touching down bar, and if touching upper, we might want to increase the speed.

10:00.000 --> 10:07.000
So, I'm going to add this orange change block. So, instead of a blue score, I'm going to select speed.

10:07.000 --> 10:14.000
So, change speed by 1. That is, we're increasing the speed of the ball, and I'm going to pull it again in the other.

10:14.000 --> 10:18.000
If touching upper condition, and again, we're going to select speed.

10:18.000 --> 10:25.000
So, if the ball touches either the down bar or the upper, it will change its speed by 1, which will increase speed.

10:25.000 --> 10:31.000
So, the next time it touches one of the bars, instead of speed equals 10, speed will become 11.

10:31.000 --> 10:34.000
So, the ball will move even faster on the screen.

10:34.000 --> 10:40.000
Before we test, let's hide this speed variable for the basketball, because I don't really want it on the stage.

10:40.000 --> 10:42.000
I only care about the scores.

10:42.000 --> 10:50.000
So, let's bring this orange hide variable, and I'm going to select speed, and I'm going to pull it right after the other orange blocks below.

10:50.000 --> 10:55.000
When I click all right, so when we hit the green flag, the speed variable will be hidden from the stage.

10:55.000 --> 10:57.000
All right, let's test it.

10:57.000 --> 11:00.000
All right, we have the ball, and we're playing.

11:00.000 --> 11:04.000
And if you notice, the boys accelerating, so it's moving ever faster.

11:04.000 --> 11:07.000
So, this is the fun of the game.

11:07.000 --> 11:12.000
All right, it's going so fast until red has lost, but I'm not sure if you notice.

11:12.000 --> 11:19.000
But blue scored two times, because the ball shot toward the red zone so fast that the red player did not have time to adjust.

11:19.000 --> 11:25.000
That is because we are not resetting the ball speed back to 10, when one of the players scores a point.

11:25.000 --> 11:27.000
So, we need to fix that.

11:27.000 --> 11:37.000
So, again, in the basketball sprites code, I'm going to in the orange set, and I'm going to select speed to 10, and I'm going to pull it where the ball resets to its original position.

11:37.000 --> 11:42.000
When the ball resets to its original position, it also needs to adjust its initial speed.

11:42.000 --> 11:52.000
So, I'm going to pull it right here under the IFY position greater than 140, and I'm going to pull it again under the IFY position less than 140.

11:52.000 --> 11:54.000
So, set the speed to 10.

11:54.000 --> 12:01.000
All right, so this makes sure that the ball resets to its original speed when it restarts the round of the game.

12:01.000 --> 12:11.000
So, if I hit the flag again and try to play a couple of times, let me actually try to hit the ball, but it goes so fast that I don't have time to adjust.

12:11.000 --> 12:18.000
Okay, so we see the ball accelerating, and when the new run starts, it restarts with its original speed, which is cool.

12:18.000 --> 12:20.000
This is exactly what we wanted.

12:20.000 --> 12:27.000
All right, so, at this point, we have a fully functional game, and I have to say I am so proud of you.

12:27.000 --> 12:32.000
You created your first full game that you can play with your friends now, and you've learned a lot.

12:32.000 --> 12:38.000
We've learned about coordinates and directions degrees in motion and variables, and so much more.

12:38.000 --> 12:44.000
And I'm really excited to show you more games and applications in this coursey based on what we learn here.

12:44.000 --> 12:52.000
In the meantime, have fun with the game that you created and modify it as you like, and I will be waiting for you in the next video.

