WEBVTT

00:00.000 --> 00:22.600
All right. Welcome back. So, you've created your scratch account, and we've learned how

00:22.600 --> 00:28.320
the scratch dashboard on the whole screen here is organized. Now, in this video, I'm

00:28.320 --> 00:32.080
going to teach you how the scratch blocks work, and we're going to program this

00:32.080 --> 00:37.080
little cat on the screen to do various things. By the end of this chapter, you'll

00:37.080 --> 00:41.040
have a pretty good idea of how scratch blocks work, so that later in the course

00:41.040 --> 00:48.240
we'll go on to create more complicated things like apps and games. So go ahead and

00:48.240 --> 00:52.200
select the sprite here if it's not selected already, and then go to the events

00:52.200 --> 00:56.520
section, which is this yellow section here in the code, and then click on this

00:56.520 --> 01:03.920
one flag, click block, and drag it all the way to the empty space. So, this is how

01:03.920 --> 01:08.280
we will program by dragging various blocks here from the left hand side, all the

01:08.280 --> 01:14.080
way to the empty space where our code is going to be for the sprite. Then, I want

01:14.080 --> 01:18.960
you to go to the sensing section, which is this light blue section, and drag the

01:18.960 --> 01:23.880
ask, what's your name and weight, and snap it right below the window, fly clicked,

01:23.880 --> 01:29.880
so notice that various code blocks will fit into one another like puzzles or

01:29.880 --> 01:36.520
Lego pieces. Then, I want you to go to the look section, which is this purple

01:36.520 --> 01:43.720
section, and drag this say hello for two seconds block, and snap it right below.

01:44.520 --> 01:49.320
So this will be our first script, which says the following. When the green flag is

01:49.320 --> 01:53.960
clicked, the cat will ask for our name, and then wait, and after we've completed

01:53.960 --> 01:59.560
our name, the cat will say hello for two seconds. So, this is how code works, these

01:59.560 --> 02:04.560
code blocks are executed one after another. So, let me show you, I'm going to click

02:04.560 --> 02:07.960
on the green flag, and the cat's going to do exactly what the script is going to

02:07.960 --> 02:13.640
say. So, when the fake clicked, the cat will ask for my name, I'm going to type

02:13.640 --> 02:18.960
in my name here, and I say Daniel, and then I'm going to hit enter, then the cat will

02:18.960 --> 02:23.960
say hello for two seconds. So, the cat says hello, and after two seconds, the bubble

02:23.960 --> 02:29.440
disappears. Alright, so this was our first script. Let's make the cat do something a little

02:29.440 --> 02:35.880
bit smarter this time. So, let's go to sensing here in the light blue section, drag this

02:35.880 --> 02:41.280
rounded answer block and watch where I'm putting it. I'm putting it right where hello

02:41.280 --> 02:47.200
is, see how hello lit up. That basically is a signal that you can snap in this answer

02:47.200 --> 02:54.360
block, where hello is so snap ID like I do. So, the cat, instead of saying hello, is

02:54.360 --> 02:59.680
going to see the answer, whatever I type here, back to me. So, if I click on the green flag

02:59.680 --> 03:05.080
now, the cat will ask for my name, and wait. I'm going to type in my name, Daniel, and

03:05.080 --> 03:12.640
watch what a cat does. It said Daniel back to me. Right, so, at this point, we programmed

03:12.640 --> 03:17.840
the cat to echo back whatever we typed. It's question, what's your name? Let's make

03:17.840 --> 03:22.880
the cat do a proper greeting by saying hello, Daniel, instead of saying basically Daniel

03:22.880 --> 03:32.000
back to me. So, go to this operator's green section, and pull in the join block. Notice

03:32.000 --> 03:38.200
that this is also a rounded block. So, pulling into the empty space. So, I'm going to take

03:38.200 --> 03:42.880
the answer from the same block, and I'm going to snap it here at the second hole of the

03:42.880 --> 03:50.480
join block. And in the first hole of the join block, I'm going to say hello, and then

03:50.480 --> 03:55.160
I'm going to put in a comma here. Then I'm going to take this whole green block, and

03:55.160 --> 04:01.680
I'm going to snap it where hello is. So, at this time, the cat will do something a little

04:01.720 --> 04:07.360
bit differently. It will still ask for my name and wait, and after I'm done typing, instead

04:07.360 --> 04:12.760
of seeing the answer back to me, it will join. Hello, comma answer, and then save that

04:12.760 --> 04:18.180
back to me for two seconds. Let me show you. So, I'm going to click on the flag. The cat

04:18.180 --> 04:23.360
asks for my name, I'm going to type it back. Daniel, and after I hit enter, the cat

04:23.360 --> 04:31.000
will say hello, comma Daniel. All right, so we programmed the cat to do a proper greeting,

04:31.000 --> 04:36.560
depending on our name, the cat is now polite enough to say hello to our name back. Now

04:36.560 --> 04:40.640
what I'm going to do is I'm going to drag these two blocks away from the window, far

04:40.640 --> 04:46.560
clicked so that when the flag is clicked, these two blocks will not get executed anymore.

04:46.560 --> 04:51.040
And I'm going to make this cat move a bit on the screen, so I'm going to go to this top

04:51.040 --> 04:57.520
motion section, and I'm going to drag in the move 10 steps block and snap it below the

04:57.520 --> 05:03.480
window. Flag clicked, so now when I click the green flag, the cat will move 10 steps in

05:03.480 --> 05:09.920
its natural direction, which is right in this case. So let me click the flag and watch

05:09.920 --> 05:16.000
the cat very closely. Click. So, notice that the cat moved slightly to the right a little

05:16.000 --> 05:21.760
bit. If I click on the green flag again, the cat will move again. So, I'm going to click

05:21.760 --> 05:25.880
it a bunch of times, and you see that the cat is still moving. Now I'm going to play

05:25.920 --> 05:30.880
around with the cat a little bit. I'm going to add some rotation to the cat as well.

05:30.880 --> 05:35.480
I'm going to drag in this turn right 15 degrees, and I'm going to snap it below the move

05:35.480 --> 05:40.280
block, and I'm going to change the number of steps it moves to the right instead of 10.

05:40.280 --> 05:46.880
I'm going to do 30. So now, when the flag is clicked, the cat will move 30 steps in

05:46.880 --> 05:52.080
its direction, and then it will turn right by 15 degrees. Now, if you don't know what

05:52.080 --> 05:57.680
degrees are, we're going to learn everything about them later on in the course. Alright,

05:57.680 --> 06:02.960
so don't worry, 15 degrees means slightly to the right. Okay, so now when I click the

06:02.960 --> 06:07.240
green flag, the cat will move a bit, and it will turn a bit. So you see it move to the

06:07.240 --> 06:11.040
right, and then it turned to the right a little bit. And if I click the flag a bunch of

06:11.040 --> 06:16.200
times, you see that the cat is turning around in circles. Now I'm going to do something

06:16.200 --> 06:20.840
pretty interesting. Instead of me clicking the flag a bunch of times, and the cat rotating

06:20.840 --> 06:25.640
in a little bit at every one of my clicks, I'm going to make the cat do these things

06:25.640 --> 06:31.440
a number of times on its own. Watch what I'm doing. So I'm going to go to control this

06:31.440 --> 06:36.240
orange section over here, and I'm going to pull in the repeat 10 block and watch how

06:36.240 --> 06:47.240
I'm dragging it. I'm going to drag it right here to wrap around these two blue blocks.

06:47.240 --> 06:51.840
So if you're reading the script, it does exactly what it says when the flag is clicked.

06:51.840 --> 06:56.640
These two blue blocks will be repeated 10 times. So if I click on the green flag now, watch

06:56.640 --> 07:02.240
what the cat does. See, it turns around in circles on its own, and the

07:02.240 --> 07:10.160
computer is fast enough so that the cat seems to be in motion. Now of course, you can play

07:10.160 --> 07:14.960
around with these values instead of 10 times. You can do 20 times, and what you'll end

07:15.000 --> 07:22.520
up with is more rotation. So notice that the cat will listen to what the script is saying.

07:22.520 --> 07:27.600
If you say repeat 20, the cat will repeat 20 times. If you say it a million times, the

07:27.600 --> 07:32.360
cat will simply rotate in circles a million times. You can even go as far as to notice

07:32.360 --> 07:36.960
what I'm doing. I'm dragging these two blocks outside. I'm going to put this repeat block

07:36.960 --> 07:42.280
right where it came from. So I'm going to make it disappear. And instead, I'm going

07:42.280 --> 07:49.640
to pull in this forever block. I'm going to snap it right below. When flat clicked,

07:49.640 --> 07:54.720
I'm going to pull these two blue blocks inside the forever block. So you probably have

07:54.720 --> 08:00.200
a good intuition of what this does when the flag is clicked, the cat will rotate in circles

08:00.200 --> 08:05.520
endlessly. So if I click on the flag, the cat just rotates around in circles. This

08:05.520 --> 08:12.920
is so nice. All right, now let me show you another trick. Go to the costume section

08:12.920 --> 08:19.760
right over here, the second tab over here. And notice that this cat has two costumes.

08:19.760 --> 08:25.680
So it has a costume, one in costume. Two. If I switch between these two costumes fast

08:25.680 --> 08:31.200
enough, you'll get the impression that the cat actually walks. So I'm going to go back

08:31.200 --> 08:37.160
to the code to this purple look section. And I'm going to pull in this very small next

08:37.160 --> 08:43.240
costume block. And I'm going to pull it right below the blue blocks inside the forever loop.

08:43.240 --> 08:51.200
So in the forever loop, we are moving 30 steps forward. We're turning 15 degrees to the

08:51.200 --> 08:56.160
right and we're switching to the next costume because the computer is so fast. These blocks

08:56.160 --> 09:02.680
will execute very quickly and will have the impression that the cat actually walks.

09:02.680 --> 09:09.680
So I'm going to click the green flag and show you how this works. So click and you see

09:09.680 --> 09:15.640
that besides walking in circles, the cat actually does appear to be walking, which is awesome.

09:15.640 --> 09:19.760
If you pay very close attention to this little sprite over here, the costumes are being

09:19.760 --> 09:25.760
switched so, so, so fast by the computer. And the impression is that the cat is animating

09:25.760 --> 09:31.080
its walk. So I'm going to stop this little script. All right, let's do something else

09:31.080 --> 09:37.360
now. So right now, when we run this program, when we click on the green flag, the cat never

09:37.360 --> 09:42.480
stops running around in circles. Let's make it controllable so that when we press space,

09:42.480 --> 09:49.840
the cat will stop and say hello to us. So here's how I'm going to go about doing it.

09:49.840 --> 09:54.600
I'm going to go to this orange control section and I'm going to pull in this if then else

09:54.600 --> 10:00.920
block and I'm going to put it into the open space for now. Notice that this block has

10:00.920 --> 10:05.040
a diamond-shaped hole in it. This will be the condition that we will fill in by going

10:05.040 --> 10:13.000
to the sensing light blue section. And I'm going to drag this diamond-shaped key space

10:13.000 --> 10:21.200
pressed and I'm going to snap it into the diamond-shaped hole here. So what this block

10:21.200 --> 10:26.760
does is, if the key space is pressed, then it's going to execute whatever code we put

10:26.760 --> 10:33.040
here into this big hole. It's going to execute the other bit of code that we put into the

10:33.040 --> 10:41.560
other hole. So when the key space is pressed, we want the cat to say hello. So we all go

10:41.560 --> 10:49.560
to looks and we'll drag in to say hello and snap it inside the big hole here. Otherwise,

10:49.560 --> 10:54.720
the cat will so continue to run around in circles, which means we're going to put these

10:54.720 --> 11:01.280
three blocks inside the LS section in this block. So notice that this block has gotten

11:01.280 --> 11:07.440
quite big. Now, I'm going to snap this if then else block inside the forever loop. So

11:07.440 --> 11:12.920
now the script has gotten a little bit complicated. Now, when the flag is clicked forever, the cat

11:12.920 --> 11:20.440
is going to check the key space first. If so, then it will say hello and it will not

11:20.440 --> 11:26.960
do anything. Otherwise, it will continue to move 30 steps, turn a bit to the right and

11:26.960 --> 11:33.120
switch to the next costume. So, let's test it and click on the green flag. And because

11:33.120 --> 11:37.160
we're not pressing anything, the cat will still carry on going around in circles because

11:37.160 --> 11:42.640
the key space is not pressed. But notice what happens when I press the space bar. So three

11:42.640 --> 11:48.840
to one space. So, when I hold on and press the space bar, the cat stops in. It's running

11:48.840 --> 11:56.200
around in circles and stops to say hello. But when I lift my finger off the space bar,

11:56.200 --> 12:04.120
watch what happens. The cat will continue to walk around in circles. This may not be

12:04.120 --> 12:09.640
exactly what we want if we press the space bar. I'd like the cat to stop completely, not

12:09.640 --> 12:13.600
to continue running around in circles like crazy when I lift my finger off the space

12:13.600 --> 12:20.080
bar. So, here's what I'm going to do. I'm going to go to the control section, this orange

12:20.080 --> 12:29.200
section. And I'm going to pull in, stop and I'm going to snap it right below to say hello.

12:29.200 --> 12:35.120
And from the drop down, I'm going to select this script. So, if the space bar is pressed,

12:35.120 --> 12:42.160
then the cat will not move. It will say hello and it will stop this script. So, the script

12:42.160 --> 12:47.680
will not execute anymore. Let me go ahead and test this. So, I'm going to go to click

12:47.680 --> 12:52.720
the green flag. The cat is spinning around in circles like crazy and now I'm going to

12:52.720 --> 13:00.560
simply tap the space bar and not hold it. So, three to one go. So now the cat has fully

13:00.560 --> 13:05.060
stopped because of this little yellow block, the stop the script has executed, which

13:05.060 --> 13:08.320
stopped this whole complicated script altogether.

13:08.320 --> 13:14.640
Alright, so we've programmed a cat to do a lot of things in this video. I'm really

13:14.640 --> 13:19.000
proud of you of what we've done in just 10 minutes. Join me in the next video where

13:19.000 --> 13:22.160
I'm going to teach you some more tricks that we're going to use throughout the rest

13:22.160 --> 13:22.880
of this course.

