WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000 00:00:00.000 --> 00:00:03.458 [MUSIC PLAYING] 00:00:16.993 --> 00:00:19.160 BRIAN YU: Welcome back, everyone, to an introduction 00:00:19.160 --> 00:00:20.510 to programming with Scratch. 00:00:20.510 --> 00:00:24.050 And last time we took a look at Events, things that can happen-- 00:00:24.050 --> 00:00:26.660 like us pressing the green flag, or clicking a button, 00:00:26.660 --> 00:00:28.730 or pressing a key on the keyboard-- and letting 00:00:28.730 --> 00:00:31.670 our Scratch project respond to those events. 00:00:31.670 --> 00:00:32.930 And how do they respond? 00:00:32.930 --> 00:00:34.970 Well, they respond with Functions, those blocks 00:00:34.970 --> 00:00:38.480 that we stacked on top of each other to let our sprites or the stage 00:00:38.480 --> 00:00:40.590 respond in some way. 00:00:40.590 --> 00:00:43.970 And so let's take a closer look today at those Functions-- 00:00:43.970 --> 00:00:47.600 we've seen already that we can have the cat move, for example, 00:00:47.600 --> 00:00:49.040 just by using a block like this-- 00:00:49.040 --> 00:00:52.940 Move 10 Steps-- such that when I click on this block, 00:00:52.940 --> 00:00:55.580 the cat moves by 10 steps. 00:00:55.580 --> 00:00:59.090 And we discussed before that these Functions, these blocks, 00:00:59.090 --> 00:01:00.470 can accept input. 00:01:00.470 --> 00:01:02.750 Input in the form of whatever goes in this oval 00:01:02.750 --> 00:01:05.630 here, which is 10 by default-- but we were able to change that. 00:01:05.630 --> 00:01:09.470 If we wanted the cat to move more steps or fewer steps, 00:01:09.470 --> 00:01:14.450 we were able to change the number here to control how many steps the cat was 00:01:14.450 --> 00:01:15.530 ultimately going to move. 00:01:15.530 --> 00:01:19.730 That, again, was called an input, and the actual information that goes inside 00:01:19.730 --> 00:01:22.160 of that input we might call a value. 00:01:22.160 --> 00:01:24.733 A value is just some piece of information-- in this case, 00:01:24.733 --> 00:01:26.900 it could be something like a number, like the number 00:01:26.900 --> 00:01:28.760 of steps to move-- like the number 10. 00:01:28.760 --> 00:01:31.590 But a value could also be words, for example-- 00:01:31.590 --> 00:01:34.340 you might recall that we were able to make our sprite speak up 00:01:34.340 --> 00:01:36.710 if we go into the Look section and choose 00:01:36.710 --> 00:01:39.830 a block like Say Hello For 2 Seconds. 00:01:39.830 --> 00:01:43.640 Here, the word "Hello" is a value as well-- values are just 00:01:43.640 --> 00:01:48.030 pieces of information that our program might be able to use. 00:01:48.030 --> 00:01:51.950 And it turns out, in addition to us just being able to type values 00:01:51.950 --> 00:01:55.940 into these oval-shaped spaces inside of our Scratch functions, 00:01:55.940 --> 00:02:00.170 there are some blocks in Scratch that are themselves values, 00:02:00.170 --> 00:02:03.030 and today we're going to be exploring those. 00:02:03.030 --> 00:02:07.130 So if you notice here in the Motion section of blocks, most of these blocks 00:02:07.130 --> 00:02:08.690 are all a very similar shape. 00:02:08.690 --> 00:02:12.048 Some might be longer or shorter, but they're all kind of rectangular, 00:02:12.048 --> 00:02:14.090 and they have these indentations so that they can 00:02:14.090 --> 00:02:16.730 stack on top of each other visually. 00:02:16.730 --> 00:02:21.110 But you'll notice that down here, at the bottom of the Motion section, 00:02:21.110 --> 00:02:24.180 we have a few blocks that are a different shape. 00:02:24.180 --> 00:02:26.760 This one's called X Position, this one's called Y Position, 00:02:26.760 --> 00:02:29.390 this one's called Direction-- they're not rectangular, 00:02:29.390 --> 00:02:31.020 they don't stack on top of each other. 00:02:31.020 --> 00:02:32.990 And in fact, we wouldn't be able to just drag 00:02:32.990 --> 00:02:36.590 this underneath the existing blocks and expect them to snap together. 00:02:36.590 --> 00:02:38.660 Scratch is a little bit smart, and it's only 00:02:38.660 --> 00:02:40.890 going to allow blocks to connect to each other 00:02:40.890 --> 00:02:44.510 if it makes logical sense for those blocks to connect to each other. 00:02:44.510 --> 00:02:47.910 Instead, these three blocks are oval shaped-- 00:02:47.910 --> 00:02:50.420 they're not functions anymore, they're values. 00:02:50.420 --> 00:02:54.470 They're pieces of information that represent something about the Scratch 00:02:54.470 --> 00:02:56.060 project that we're currently running. 00:02:56.060 --> 00:02:58.310 This block is a value that represents what 00:02:58.310 --> 00:03:02.300 is the x position of a current sprite-- how far to the left or to the right 00:03:02.300 --> 00:03:03.350 is that sprite. 00:03:03.350 --> 00:03:05.510 This block represents the y position, how far 00:03:05.510 --> 00:03:07.740 up or down that sprite happens to be. 00:03:07.740 --> 00:03:11.540 And this block represents the direction-- what direction that sprite 00:03:11.540 --> 00:03:12.920 happens to be facing. 00:03:12.920 --> 00:03:15.170 And because they're oval-shaped, we can't just 00:03:15.170 --> 00:03:19.490 add them above or below the script, but we can put them inside 00:03:19.490 --> 00:03:21.140 of these oval-shaped inputs. 00:03:21.140 --> 00:03:24.170 Notice the inputs to all of the functions are oval-shaped-- 00:03:24.170 --> 00:03:27.320 and therefore, because these values are also oval-shaped, 00:03:27.320 --> 00:03:29.960 they can snap together that way instead. 00:03:29.960 --> 00:03:34.590 We're going to be putting a block inside of the input to another block, 00:03:34.590 --> 00:03:36.750 so let's see what that looks like. 00:03:36.750 --> 00:03:41.270 I'll go ahead and get rid of the Move 10 Steps-- we'll just keep the Say Hello. 00:03:41.270 --> 00:03:43.910 But instead of saying hello for two seconds, 00:03:43.910 --> 00:03:49.820 let's have the sprite say it's x position for two seconds-- 00:03:49.820 --> 00:03:56.210 meaning how far to the left or to the right is the cat right now. 00:03:56.210 --> 00:03:59.150 And I'll have this happen when the flag is clicked, so add an Event-- 00:03:59.150 --> 00:04:03.980 when the flag is clicked, go ahead and say your x position for two seconds, 00:04:03.980 --> 00:04:06.330 and now when I click the flag to start the program-- 00:04:06.330 --> 00:04:08.360 let's see what happens. 00:04:08.360 --> 00:04:10.100 The cat says 10-- 00:04:10.100 --> 00:04:12.380 it's saying its current x position, which we can 00:04:12.380 --> 00:04:14.840 see down here it's exposition is 10. 00:04:14.840 --> 00:04:18.547 And if we drag the cat far to the right, for example, now click the green flag, 00:04:18.547 --> 00:04:20.630 and you'll see it's going to say a bigger number-- 00:04:20.630 --> 00:04:22.250 175. 00:04:22.250 --> 00:04:24.620 And if we drag the cat far to the left, now it's 00:04:24.620 --> 00:04:26.300 going to say a smaller number-- 00:04:26.300 --> 00:04:30.140 negative 161. 00:04:30.140 --> 00:04:34.610 And if we want, we can have it say both its x position and its y position. 00:04:34.610 --> 00:04:38.870 I'll add another Say block, and instead of saying the x position, 00:04:38.870 --> 00:04:44.390 let's go back into Motion, and let's choose y position. 00:04:44.390 --> 00:04:47.180 Now it's going to say it's x value for two seconds, 00:04:47.180 --> 00:04:49.300 and then it's y value for two seconds. 00:04:49.300 --> 00:04:51.330 I'll click the green flag-- 00:04:51.330 --> 00:04:55.730 it says negative 161, negative 13. 00:04:55.730 --> 00:05:00.140 And so these values, these oval-shaped x position and y position blocks, 00:05:00.140 --> 00:05:02.090 they're no longer functions-- these are just 00:05:02.090 --> 00:05:06.500 values that we can put in any spot inside of our functions 00:05:06.500 --> 00:05:08.210 where they have an oval-shaped input. 00:05:08.210 --> 00:05:11.960 Where we could add something into that oval to describe how what 00:05:11.960 --> 00:05:15.320 we want the cat, in this case, to say. 00:05:15.320 --> 00:05:18.030 And right now, it's saying the x position for two seconds 00:05:18.030 --> 00:05:21.130 and then it's y value for two seconds, which is fine, 00:05:21.130 --> 00:05:23.490 but maybe I'd like the cat to say them together. 00:05:23.490 --> 00:05:26.170 To say, like, here's where I am right now. 00:05:26.170 --> 00:05:29.280 And to do that, I need some way of combining these values-- 00:05:29.280 --> 00:05:33.932 I need to combine the x location and the y location of this cat. 00:05:33.932 --> 00:05:35.640 And here's where we're going to introduce 00:05:35.640 --> 00:05:39.180 another new concept within Scratch, and within programming more generally-- 00:05:39.180 --> 00:05:41.640 and those are Operators. 00:05:41.640 --> 00:05:46.170 An Operator is going to accept values as its own input, 00:05:46.170 --> 00:05:49.590 and the Operator is going to produce a new value for us. 00:05:49.590 --> 00:05:53.580 And so one Operator, for example, this very first one is +. 00:05:53.580 --> 00:05:57.120 It will take two values, maybe like 1 and 2 and we add them together 00:05:57.120 --> 00:05:58.500 and it's going to give us 3. 00:05:58.500 --> 00:05:59.970 So, what would that look like? 00:05:59.970 --> 00:06:03.450 I'll disconnect these for now just so we can try something a little different. 00:06:03.450 --> 00:06:07.140 I'll go into Looks, have it say something for 2 seconds, 00:06:07.140 --> 00:06:12.990 but instead of saying hello for two seconds, let's have it say 1 plus 2 00:06:12.990 --> 00:06:15.240 for two seconds. 00:06:15.240 --> 00:06:19.380 Now I press the green flag to start the program and the cat says 3-- 00:06:19.380 --> 00:06:24.190 this operator, this green block here, is performing the task of addition. 00:06:24.190 --> 00:06:29.640 It accepts two inputs, two values of its own as inputs, the 1 and the 2-- 00:06:29.640 --> 00:06:33.660 and then this entire oval block is going to calculate what is 1 plus 2, 00:06:33.660 --> 00:06:34.920 it's going to be 3. 00:06:34.920 --> 00:06:37.440 And because that's inside of the Say block, 00:06:37.440 --> 00:06:42.820 now the cat is going to say the number 3 for two seconds. 00:06:42.820 --> 00:06:45.730 And so we have blocks to perform math like addition, subtraction, 00:06:45.730 --> 00:06:47.500 multiplication, and division. 00:06:47.500 --> 00:06:50.250 We have other blocks, as well, and the one I'm interested in right 00:06:50.250 --> 00:06:52.920 now is this one down here called Join. 00:06:52.920 --> 00:06:55.710 Right now, it says Join Apple and Banana-- 00:06:55.710 --> 00:06:59.340 you could use this for any characters we might want to join together. 00:06:59.340 --> 00:07:03.960 And what it's going to do is it's going to take two different values, 00:07:03.960 --> 00:07:07.440 and it's just going to combine them together into one. 00:07:07.440 --> 00:07:12.180 And so I would like to combine together the x location of the cat in addition 00:07:12.180 --> 00:07:13.890 to the y location of the cat-- 00:07:13.890 --> 00:07:18.930 and so let's use the Join operator to do just that. 00:07:18.930 --> 00:07:21.630 I'll get rid of this block that was just doing some math 00:07:21.630 --> 00:07:25.980 and instead take this Join block, and I want to join together 00:07:25.980 --> 00:07:29.550 the X Position and the Y Position. 00:07:29.550 --> 00:07:33.450 Notice that these ovals snap into place because the two inputs in the join 00:07:33.450 --> 00:07:36.150 block are also oval-shaped-- 00:07:36.150 --> 00:07:38.640 now I don't need two Says, I only need one. 00:07:38.640 --> 00:07:42.960 And let's take this Join block and put it inside of the Say block-- 00:07:42.960 --> 00:07:46.650 and notice the Say block too will grow in order to make room 00:07:46.650 --> 00:07:48.330 for the block that's inside of it now. 00:07:48.330 --> 00:07:51.690 And now I have a block inside of a block inside of another block-- 00:07:51.690 --> 00:07:54.240 the same block has this Join block in it, 00:07:54.240 --> 00:07:59.680 and inside the Join block are the x and y values as well. 00:07:59.680 --> 00:08:03.810 And so now when I press the green flag, notice what happens-- 00:08:03.810 --> 00:08:09.600 it says negative 161 negative 13, and it was all kind of mashed together, 00:08:09.600 --> 00:08:12.220 and I might not even know exactly where that coordinate is. 00:08:12.220 --> 00:08:18.073 So if I wanted to, I could change the backdrop-- let's give myself a grid, 00:08:18.073 --> 00:08:20.490 I think there's a grid backdrop if I do a search for grid. 00:08:20.490 --> 00:08:22.140 Yeah, here's an xy grid-- 00:08:22.140 --> 00:08:26.910 so I can see on an xy grid kind of where the cat is on this plane. 00:08:26.910 --> 00:08:28.800 I'll press it again-- 00:08:28.800 --> 00:08:30.679 minus 161 minus 13. 00:08:30.679 --> 00:08:32.429 That's fine, but maybe if you've used math 00:08:32.429 --> 00:08:36.502 before, you're more used to x comma y, you see one value comma 00:08:36.502 --> 00:08:39.210 another value instead of them just being right next to each other 00:08:39.210 --> 00:08:41.870 without even a space in between them. 00:08:41.870 --> 00:08:42.870 So how could we do that? 00:08:42.870 --> 00:08:47.160 How could we add a comma just to separate the x value from the y value 00:08:47.160 --> 00:08:48.840 a little bit more? 00:08:48.840 --> 00:08:52.282 Well, I can use another Join-- 00:08:52.282 --> 00:08:54.240 this is going to look a little bit complicated, 00:08:54.240 --> 00:08:55.680 but here's what I'm going to do. 00:08:55.680 --> 00:09:00.030 Instead of joining together the X Position and the Y Position, 00:09:00.030 --> 00:09:06.720 let me first take a comma and a space, and join that with the Y Position. 00:09:06.720 --> 00:09:11.185 And then put that Join inside of the input to the other Join-- 00:09:11.185 --> 00:09:13.560 so this is starting to get a little bit more complicated, 00:09:13.560 --> 00:09:15.480 but let's try and break down exactly what's happening here. 00:09:15.480 --> 00:09:18.300 I've got a lot of blocks nested inside of other blocks, 00:09:18.300 --> 00:09:20.520 but here I'm saying something-- what am I saying? 00:09:20.520 --> 00:09:22.560 Well, I'm joining together these two things-- 00:09:22.560 --> 00:09:27.152 I'm joining the x value of the cat and this block here. 00:09:27.152 --> 00:09:28.110 And what is this block? 00:09:28.110 --> 00:09:32.640 Well, that block is joining together a comma and the y value 00:09:32.640 --> 00:09:34.720 of wherever the cat happens to be. 00:09:34.720 --> 00:09:38.570 So taken all together, what does this big oval now do? 00:09:38.570 --> 00:09:42.330 Well, it's going to take the X Position, and then take a comma, and then 00:09:42.330 --> 00:09:45.600 the Y Position, and that all together is going to be 00:09:45.600 --> 00:09:48.945 what goes inside of the Say block. 00:09:48.945 --> 00:09:51.570 So we're starting to nest a lot of blocks inside of each other, 00:09:51.570 --> 00:09:54.240 but the net result is that, if I take the cat 00:09:54.240 --> 00:09:59.400 and move it to near the middle of the stage, for example-- 00:09:59.400 --> 00:10:00.750 press the green flag-- 00:10:00.750 --> 00:10:05.040 it's going to say 4 comma 3, that's where in the world it happens to be. 00:10:05.040 --> 00:10:08.140 If I move its location somewhere else and press the flag again, 00:10:08.140 --> 00:10:10.720 it's going to give me some different numbers. 00:10:10.720 --> 00:10:15.580 But now it's saying it all at once, one value comma another value. 00:10:15.580 --> 00:10:17.820 And if I wanted to make this a complete sentence, 00:10:17.820 --> 00:10:23.430 I can even add one more Join n-- let me say, I am at-- 00:10:23.430 --> 00:10:27.570 and then take this whole big Join block that Says position, and put that 00:10:27.570 --> 00:10:32.340 on the other side of the Join, and now have the cat say all of this. 00:10:32.340 --> 00:10:39.000 "I am at x comma y," and so now if I drag it down here, it says, 00:10:39.000 --> 00:10:44.700 "I am at negative 30 comma negative 96. " If I drag it here, the cat says, 00:10:44.700 --> 00:10:51.300 "I am at 49 comma 61," and so using these values and operators we can start 00:10:51.300 --> 00:10:56.100 to combine values together and start to make some more interesting projects 00:10:56.100 --> 00:10:57.730 as well. 00:10:57.730 --> 00:11:00.930 And we did this just now with the X Position and the Y Position, 00:11:00.930 --> 00:11:04.140 but there's also this Direction block that we could do this with too. 00:11:04.140 --> 00:11:06.870 If we wanted the cat to, not only say where it is, 00:11:06.870 --> 00:11:10.620 but also what direction it's facing, you could imagine using that Direction 00:11:10.620 --> 00:11:14.260 block-- so the cat could say, "I am pointing at 90 degree angle," 00:11:14.260 --> 00:11:17.230 or "I'm pointing at a 45 degree angle" for example-- 00:11:17.230 --> 00:11:22.983 and we could have it use that value inside of its blocks as well. 00:11:22.983 --> 00:11:24.900 So let's try something a little different now, 00:11:24.900 --> 00:11:27.460 we've taken a look at these motion values-- 00:11:27.460 --> 00:11:29.490 let's look at what other values we might have. 00:11:29.490 --> 00:11:32.340 I'll keep scrolling down inside of Scratch's interface, 00:11:32.340 --> 00:11:35.430 and under the Look section we have a whole bunch of other functions 00:11:35.430 --> 00:11:37.620 that we've used before-- we've used the Say block, 00:11:37.620 --> 00:11:40.920 we've used the Switch Costume, and Switch Backdrop blocks, 00:11:40.920 --> 00:11:42.940 we've used Change Size. 00:11:42.940 --> 00:11:44.910 Notice down below though-- 00:11:44.910 --> 00:11:49.500 we also have a couple of blocks that are not the shape of the normal Functions, 00:11:49.500 --> 00:11:52.770 they're instead oval-shaped meaning they are values. 00:11:52.770 --> 00:11:56.520 We have one for the Costume number, we have one for the Backdrop number, 00:11:56.520 --> 00:11:58.800 and we have one just called Size-- 00:11:58.800 --> 00:12:02.580 and Size in this case is going to represent how big or small 00:12:02.580 --> 00:12:03.360 our sprite is. 00:12:03.360 --> 00:12:06.180 Remember that our sprites start out at 100% size, 00:12:06.180 --> 00:12:08.790 but we have already seen how we can make them smaller 00:12:08.790 --> 00:12:10.720 or we can make them bigger. 00:12:10.720 --> 00:12:13.350 So, when might we want to use something like the Size block 00:12:13.350 --> 00:12:17.220 to know how big or small a particular sprite is? 00:12:17.220 --> 00:12:18.520 Well, let's take an example-- 00:12:18.520 --> 00:12:20.760 let's imagine we're not using the cat anymore, 00:12:20.760 --> 00:12:24.630 I'll go ahead and delete the cat, and I'll change the backdrop back 00:12:24.630 --> 00:12:27.090 to our plain white backdrop. 00:12:27.090 --> 00:12:33.570 Let's pick out a new sprite, go into Animals, and let's grab the hedgehog. 00:12:33.570 --> 00:12:36.060 And if I wanted the hedgehog to move, I want 00:12:36.060 --> 00:12:39.480 the hedgehog to move every time I press the right arrow, for example-- 00:12:39.480 --> 00:12:41.100 we saw last time how we could do that. 00:12:41.100 --> 00:12:44.310 I add an Event saying, not when the Space key is pressed, 00:12:44.310 --> 00:12:46.500 but when the Right arrow is pressed. 00:12:46.500 --> 00:12:51.810 Now I want the hedgehog to move 10 steps, so drag it over to the left, 00:12:51.810 --> 00:12:55.380 I press the right arrow, and it's moving. 00:12:55.380 --> 00:12:59.340 And that seems like a reasonable pace for a hedgehog of about that size, 00:12:59.340 --> 00:13:03.570 but if I made the hedgehog much smaller and sprite 50%. 00:13:03.570 --> 00:13:05.812 And now have it move-- 00:13:05.812 --> 00:13:07.770 notice that it's moving-- because it's smaller, 00:13:07.770 --> 00:13:09.810 it's still moving 10 steps every time. 00:13:09.810 --> 00:13:12.960 It's moving maybe a little bit fast for how big it is-- 00:13:12.960 --> 00:13:15.540 I might like to change this so that my program's 00:13:15.540 --> 00:13:20.010 a little bit more responsive to how big or small a sprite happens to be. 00:13:20.010 --> 00:13:23.070 If a sprite is bigger, then it's probably taking bigger steps, 00:13:23.070 --> 00:13:24.980 so I want it to move more every time. 00:13:24.980 --> 00:13:26.730 And if a sprite a smaller, well, then it's 00:13:26.730 --> 00:13:32.110 probably taking smaller steps, so I want it to move less every time. 00:13:32.110 --> 00:13:34.560 And so instead of moving 10 steps-- 00:13:34.560 --> 00:13:39.360 I'll change the size back to 100, but instead of moving 10 steps, 00:13:39.360 --> 00:13:43.320 let's have it move Size steps. 00:13:43.320 --> 00:13:48.090 Size is a value representing how big or small the hedgehog is, 00:13:48.090 --> 00:13:51.600 and I'll drag it inside this oval shape input-- it snaps right into place 00:13:51.600 --> 00:13:53.310 because it is oval-sized. 00:13:53.310 --> 00:13:55.990 And now, instead of moving 10 steps every time, 00:13:55.990 --> 00:13:59.040 it's going to move a number of steps dependent upon the size 00:13:59.040 --> 00:14:00.300 of the hedgehog. 00:14:00.300 --> 00:14:05.460 And so because this hedgehog is 100% size, now when I press the Right arrow, 00:14:05.460 --> 00:14:08.050 it's moving 100 steps every time. 00:14:08.050 --> 00:14:11.380 That's probably too fast, I want to cut down on this a little bit-- 00:14:11.380 --> 00:14:14.950 and here is where we can start to do maybe a little bit of math. 00:14:14.950 --> 00:14:18.300 Let's go into Operators and let's use Division-- 00:14:18.300 --> 00:14:20.760 Division I can use to cut down on the size of something 00:14:20.760 --> 00:14:22.200 make the numbers smaller. 00:14:22.200 --> 00:14:26.070 Let's do Size divided by 10, you could play around with this 00:14:26.070 --> 00:14:28.122 and decide what you want it to be. 00:14:28.122 --> 00:14:29.580 And so, now what's going to happen? 00:14:29.580 --> 00:14:32.070 Let's try and analyze the math of this a little bit-- 00:14:32.070 --> 00:14:36.540 if the size of the hedgehog is 100, meaning it's full size to begin with, 00:14:36.540 --> 00:14:38.820 then every time I press the Right arrow key-- 00:14:38.820 --> 00:14:41.700 we're going to move Size divided by 10 steps. 00:14:41.700 --> 00:14:45.640 The size is 100 divided by 10 is just 10, 00:14:45.640 --> 00:14:49.013 and so the hedgehog is going to move 10 steps. 00:14:49.013 --> 00:14:50.430 What if the hedgehog were smaller? 00:14:50.430 --> 00:14:52.230 What if the size was 50? 00:14:52.230 --> 00:14:55.677 Well, then the hedgehog is going to move 50 divided by 10 steps, 00:14:55.677 --> 00:14:57.510 meaning it's only going to move five steps-- 00:14:57.510 --> 00:14:59.795 smaller steps for a smaller hedgehog. 00:14:59.795 --> 00:15:01.170 What if the hedgehog were bigger? 00:15:01.170 --> 00:15:03.090 What if it was size of 200? 00:15:03.090 --> 00:15:06.310 Well, then it's going to move at 200 divided by 10 steps-- 00:15:06.310 --> 00:15:09.630 in other words, it's going to move 20 steps every time 00:15:09.630 --> 00:15:10.620 we hit the Right arrow. 00:15:10.620 --> 00:15:14.970 It's moving more steps because it's a bigger animal. 00:15:14.970 --> 00:15:19.640 And so now when I press Right arrow, it's moving 10 steps every time. 00:15:19.640 --> 00:15:23.750 If I change the size of the hedgehog and make it a little bit smaller, size 15, 00:15:23.750 --> 00:15:28.140 now it's moving only five steps every time I press the Right arrow. 00:15:28.140 --> 00:15:31.700 And if I make it bigger, make it size 200, 00:15:31.700 --> 00:15:39.240 now it's going to be moving 20 steps every time I press the arrow as well. 00:15:39.240 --> 00:15:43.220 And so we can start to use these values to get information about our sprite-- 00:15:43.220 --> 00:15:45.020 where it is in the world, how big or small 00:15:45.020 --> 00:15:48.500 it is, and use that to affect the way that our program behaves. 00:15:48.500 --> 00:15:50.840 By using, not just values that we type in, 00:15:50.840 --> 00:15:57.660 but by using values that are given to us as part of Scratch to make it work too. 00:15:57.660 --> 00:16:00.500 And so, I'll go ahead and change the size back to 100. 00:16:00.500 --> 00:16:04.100 I'll go ahead and center the hedgehog again so it's back in the middle, 00:16:04.100 --> 00:16:06.650 and let's try something else fun in these Operators. 00:16:06.650 --> 00:16:09.080 One thing I'm noticing in these Operators 00:16:09.080 --> 00:16:11.750 is that we can do math-- we've seen like addition, and division, 00:16:11.750 --> 00:16:14.390 and there's also subtraction, and multiplication. 00:16:14.390 --> 00:16:17.300 We've seen how we can join two words together 00:16:17.300 --> 00:16:19.973 with this Join Apple and Banana block. 00:16:19.973 --> 00:16:21.890 There's another block that looks interesting-- 00:16:21.890 --> 00:16:25.310 Pick Random 1 To 10. 00:16:25.310 --> 00:16:27.290 Pick Random 1 To 10 is a type of block that's 00:16:27.290 --> 00:16:30.680 going to let us add a little bit of randomness into our project. 00:16:30.680 --> 00:16:34.520 Right now, most of our projects are just doing the same thing every time-- 00:16:34.520 --> 00:16:36.990 unless we use the Random Position movement, 00:16:36.990 --> 00:16:38.540 which we used a couple of times. 00:16:38.540 --> 00:16:41.600 But Pick Random will just pick a random number for us 00:16:41.600 --> 00:16:44.060 so that we can let the project do something 00:16:44.060 --> 00:16:47.280 different each time we run the project. 00:16:47.280 --> 00:16:50.810 And so just for fun I might say, when the flag is clicked-- 00:16:50.810 --> 00:16:53.000 let's go ahead and go to Motion-- 00:16:53.000 --> 00:16:57.020 and have the hedgehog point in a particular direction. 00:16:57.020 --> 00:16:59.970 And instead of always pointing in direction 90, 00:16:59.970 --> 00:17:05.660 let's have the hedgehog pick a random number between 0 and 90. 00:17:05.660 --> 00:17:10.010 And we've put that value inside of this Point In Direction block-- 00:17:10.010 --> 00:17:13.530 and so, now every time I press the green flag, what's going to happen? 00:17:13.530 --> 00:17:16.460 We're going to pick a random number between 0 and 90, 00:17:16.460 --> 00:17:19.832 and the hedgehog is then going to point in that direction. 00:17:19.832 --> 00:17:20.540 So I can try it-- 00:17:20.540 --> 00:17:24.530 I'll press the green flag, and the hedgehog tilts slightly. 00:17:24.530 --> 00:17:27.502 I'll press it again, now it's facing a totally different direction-- 00:17:27.502 --> 00:17:29.960 I'll press it again, now it's facing a different direction. 00:17:29.960 --> 00:17:32.450 And every time I press the flag, it's going 00:17:32.450 --> 00:17:35.330 to calculate a random number between 0 and 90, 00:17:35.330 --> 00:17:40.110 and the hedgehog is then going to point in that direction. 00:17:40.110 --> 00:17:42.737 And so randomness can add a little bit of fun to your projects 00:17:42.737 --> 00:17:44.570 as well so that there's something surprising 00:17:44.570 --> 00:17:47.090 every time you run the project-- where it's not always going 00:17:47.090 --> 00:17:51.650 to be the same thing every single time. 00:17:51.650 --> 00:17:55.520 All right, so we've seen some values that we can use inside of Motion, 00:17:55.520 --> 00:17:57.680 and inside of Looks, and we've seen some Operators 00:17:57.680 --> 00:18:00.320 we can use to do some math to join things together, 00:18:00.320 --> 00:18:01.980 to pick some random numbers. 00:18:01.980 --> 00:18:04.372 If we keep looking around for other values-- 00:18:04.372 --> 00:18:06.080 I'll go ahead and scroll, and I'm looking 00:18:06.080 --> 00:18:08.060 for anything that might be shaped like an oval, 00:18:08.060 --> 00:18:10.530 potentially, that I might want to use. 00:18:10.530 --> 00:18:15.200 One thing that catches my attention is this value here-- 00:18:15.200 --> 00:18:16.478 it's called Timer. 00:18:16.478 --> 00:18:19.520 And you might recall from last time that we have used the Timer already-- 00:18:19.520 --> 00:18:22.130 every Scratch project has a built-in timer that's 00:18:22.130 --> 00:18:27.770 counting how many seconds have passed since we started running the project. 00:18:27.770 --> 00:18:31.040 And we used that before to control the timing of our different sprites 00:18:31.040 --> 00:18:33.240 that existed in this project as well. 00:18:33.240 --> 00:18:36.290 But we can also use this if we just want to know how long it's 00:18:36.290 --> 00:18:38.790 been since we started the project. 00:18:38.790 --> 00:18:42.080 I'll go ahead and get rid of these blocks here, 00:18:42.080 --> 00:18:45.500 let's use the Timer value for something, let's say-- let's go to Events-- 00:18:45.500 --> 00:18:48.770 when the sprite is clicked, when I click on the hedgehog-- let's go ahead 00:18:48.770 --> 00:18:54.080 and have the hedgehog say whatever the value of the Timer is. 00:18:54.080 --> 00:18:57.170 I'll go ahead and change the hedgehog's direction back to 90 00:18:57.170 --> 00:19:00.320 so it's still facing the right again, and now I'll 00:19:00.320 --> 00:19:04.160 press the green flag to start the project-- the project's now started. 00:19:04.160 --> 00:19:06.710 And now when I click on the hedgehog, it says, 00:19:06.710 --> 00:19:10.730 "it's been 4.45 seconds since we started the project. " 00:19:10.730 --> 00:19:15.530 And I'll press it again, "it's been 9.90 seconds since we started the project, " 00:19:15.530 --> 00:19:18.820 and honestly now I don't really need to know that number in all that much 00:19:18.820 --> 00:19:19.320 detail. 00:19:19.320 --> 00:19:24.350 I don't care if it's 9.90 versus 9.91 for example I just want to know has it 00:19:24.350 --> 00:19:27.780 been nine seconds or 10 seconds or 11 seconds, for example. 00:19:27.780 --> 00:19:31.730 And so what I could do in this case is go back to Operators, 00:19:31.730 --> 00:19:35.000 and there's an Operator here called Round. 00:19:35.000 --> 00:19:38.270 And Round, If you're familiar with it for math, just takes a number 00:19:38.270 --> 00:19:40.890 and rounds it to a whole number. 00:19:40.890 --> 00:19:43.280 So if I have 9.9, it's just going to say, 00:19:43.280 --> 00:19:47.480 you know what, let's call that 10-- it rounds up to 10 seconds. 00:19:47.480 --> 00:19:51.230 So we'll take the Timer, put it inside of the Round Operator, 00:19:51.230 --> 00:19:54.590 and we'll take the Round Operator and put it inside of the Say block. 00:19:54.590 --> 00:19:58.490 Now we're saying the rounded version of the timer for two seconds. 00:19:58.490 --> 00:20:03.140 Now I click on it and it's been 58 seconds now, not 58 point something, 00:20:03.140 --> 00:20:03.740 for example. 00:20:03.740 --> 00:20:07.580 I wait a few more seconds, now it's been 63 seconds 00:20:07.580 --> 00:20:09.930 since the start of the project. 00:20:09.930 --> 00:20:12.050 And so these values give you a lot ability 00:20:12.050 --> 00:20:15.050 to know something about what's going on inside of the project. 00:20:15.050 --> 00:20:18.180 And Scratch just figures out what each of these values 00:20:18.180 --> 00:20:21.180 should be-- it knows what the x position and y position 00:20:21.180 --> 00:20:22.960 and size is for any given sprite. 00:20:22.960 --> 00:20:26.140 It knows how long it's been since we started running the project, 00:20:26.140 --> 00:20:28.560 so it knows what the value of the timer should be. 00:20:28.560 --> 00:20:32.760 But there are some other values that the user can have a little bit more control 00:20:32.760 --> 00:20:35.850 over as well, and that's where our projects can get even more 00:20:35.850 --> 00:20:38.500 interactive and even more interesting. 00:20:38.500 --> 00:20:39.990 Let's take a look now-- 00:20:39.990 --> 00:20:45.060 I'll go ahead and get rid of the hedgehog and we'll bring back the cat. 00:20:45.060 --> 00:20:48.510 Let's take a look now at the Sensing section of blocks, which 00:20:48.510 --> 00:20:50.460 we haven't really looked at just yet. 00:20:50.460 --> 00:20:52.470 But notice this block here-- 00:20:52.470 --> 00:20:55.652 Ask What's Your Name And Wait-- 00:20:55.652 --> 00:20:56.860 this is an interesting block. 00:20:56.860 --> 00:21:00.120 It is a function, it's shaped like all of those other function blocks, 00:21:00.120 --> 00:21:02.880 and it seems to be asking the user a question. 00:21:02.880 --> 00:21:05.640 It's asking something like, what's your name? 00:21:05.640 --> 00:21:08.040 And What's Your Name here, this, if you'll recall, 00:21:08.040 --> 00:21:09.570 is the input to the function. 00:21:09.570 --> 00:21:11.880 Anything in this oval shape in a function 00:21:11.880 --> 00:21:13.920 block-- that's an input to the function. 00:21:13.920 --> 00:21:16.800 It's telling this function what question should you ask-- 00:21:16.800 --> 00:21:18.810 by default, the question is, what's your name? 00:21:18.810 --> 00:21:22.860 But the question might be different, we could ask whatever question we want, 00:21:22.860 --> 00:21:25.270 but the user is going to type in an answer. 00:21:25.270 --> 00:21:29.460 And that answer is going to then be the output of this function, 00:21:29.460 --> 00:21:32.340 and we haven't really seen outputs too much just yet. 00:21:32.340 --> 00:21:35.130 These outputs, also called return values, 00:21:35.130 --> 00:21:39.280 are values that come back after we run a function. 00:21:39.280 --> 00:21:41.430 And it turns out this Ask What's Your Name and Wait 00:21:41.430 --> 00:21:43.620 block has a return value-- 00:21:43.620 --> 00:21:48.180 and the return value is this value here, which is called answer. 00:21:48.180 --> 00:21:51.030 This block here, answer, is going to be a value that 00:21:51.030 --> 00:21:57.790 stores whatever it is the user typed in in response to that question. 00:21:57.790 --> 00:22:01.170 So we can ask the user a question, and based on what the user answers, 00:22:01.170 --> 00:22:05.830 we can use that answer later on in our program somewhere. 00:22:05.830 --> 00:22:11.490 So let's give that a try, let's try that out and see what we can do. 00:22:11.490 --> 00:22:14.640 I want to say that, when the green flag is clicked when I first 00:22:14.640 --> 00:22:19.560 start this project, let's go to something 00:22:19.560 --> 00:22:23.970 and choose the Ask What's Your Name and Wait block. 00:22:23.970 --> 00:22:27.870 And when you do that, let's go ahead and just say it back to them. 00:22:27.870 --> 00:22:30.547 I'll go to Say, Say Hello for 2 Seconds. 00:22:30.547 --> 00:22:32.880 I don't want you to say "hello" for two seconds though-- 00:22:32.880 --> 00:22:36.750 I want you to say the answer for two seconds. 00:22:36.750 --> 00:22:40.140 Whatever it is the user typed in when they were asked that question, 00:22:40.140 --> 00:22:43.620 that's going to be this return value called Answer. 00:22:43.620 --> 00:22:46.350 And we're now using Answer later in the project 00:22:46.350 --> 00:22:51.360 to say the answer for two seconds-- so now when I run this program, 00:22:51.360 --> 00:22:54.270 the cat asks the question "what's your name?" 00:22:54.270 --> 00:22:57.420 And we haven't moved on in the program yet-- the program is waiting, 00:22:57.420 --> 00:22:59.430 and it's waiting for me to answer the question. 00:22:59.430 --> 00:23:05.760 It's waiting for me to type in my name, I'll type in Brian, I'll press Return, 00:23:05.760 --> 00:23:08.613 and the cat now says my name back to me. 00:23:08.613 --> 00:23:11.280 And if I wanted to, I could make it a little bit more friendly-- 00:23:11.280 --> 00:23:14.190 I can go into Operators and let's use that Join again. 00:23:14.190 --> 00:23:21.820 Instead of saying answer, let's have it say Hello comma and then the answer. 00:23:21.820 --> 00:23:25.110 So I'm using that Join Operator to combine two values together 00:23:25.110 --> 00:23:27.970 and I'm using that inside of the Say block. 00:23:27.970 --> 00:23:33.270 Now, when I press the green flag, "what's your name? " I say, Brian, 00:23:33.270 --> 00:23:37.210 and the cat says, "hello, Brian," for example. 00:23:37.210 --> 00:23:41.760 And so we can ask the user questions and use their answer inside of the program, 00:23:41.760 --> 00:23:44.070 and that opens up a lot of interesting doors 00:23:44.070 --> 00:23:46.770 for what it is that we can do with our Scratch project. 00:23:46.770 --> 00:23:51.360 Remember, before, we'd been using Move to move a certain number of steps, 00:23:51.360 --> 00:23:54.720 and we can say move 10 steps, or move 20 steps for example. 00:23:54.720 --> 00:24:00.510 Let's ask the user how far we want to move, let's ask, how many steps? 00:24:00.510 --> 00:24:04.410 And then instead of moving 10 steps, we'll go into Sensing, 00:24:04.410 --> 00:24:05.550 we'll grab the answer-- 00:24:05.550 --> 00:24:08.590 that return value, and let's Move Answer Steps. 00:24:08.590 --> 00:24:12.810 Whatever the user typed in, that's how many steps I want to move. 00:24:12.810 --> 00:24:16.600 So I'll drag the cat back to the left hand side, I'll press the flag-- 00:24:16.600 --> 00:24:21.600 "how many steps? " Or if I type in 10, we move 10 steps. 00:24:21.600 --> 00:24:22.690 Let's try it again-- 00:24:22.690 --> 00:24:25.860 "how many steps? " I'll type in 50, something bigger. 00:24:25.860 --> 00:24:27.540 Now we move 50 steps. 00:24:27.540 --> 00:24:30.240 Press it again-- "how many steps? " Let's try 200, 00:24:30.240 --> 00:24:33.340 and the cat moves 200 steps when I type in that number. 00:24:33.340 --> 00:24:37.200 So whatever number I type in, that's the Return value of the Function 00:24:37.200 --> 00:24:38.910 that becomes the Answer. 00:24:38.910 --> 00:24:42.510 And because I'm using the Answer inside of this Move some number of steps 00:24:42.510 --> 00:24:45.150 block, that's going to result in me-- the user-- 00:24:45.150 --> 00:24:48.420 being able to control as by typing it into my keyboard 00:24:48.420 --> 00:24:51.750 how far I actually want the cat to move. 00:24:51.750 --> 00:24:54.210 And I can use this for other blocks too-- 00:24:54.210 --> 00:24:57.030 we've seen before that we have the ability 00:24:57.030 --> 00:25:01.260 to set the x value to something, to make it go somewhere, 00:25:01.260 --> 00:25:03.930 and we've also seen that we can set the y value to something 00:25:03.930 --> 00:25:05.470 to have it go somewhere else. 00:25:05.470 --> 00:25:13.680 So I can ask Pick x, and then let's set x to that answer. 00:25:13.680 --> 00:25:21.910 Then ask Pick y and set y to the new answer-- 00:25:21.910 --> 00:25:25.030 so now I press the green flag, Pick x. 00:25:25.030 --> 00:25:31.290 Let's pick 50, Pick y-- 00:25:31.290 --> 00:25:35.530 let's pick negative 100-- 00:25:35.530 --> 00:25:38.170 and the cat goes to 50 and negative 100. 00:25:38.170 --> 00:25:42.850 If I wanted to go back to the center, Pick x or Pick 0, Pick Y or Pick 0, 00:25:42.850 --> 00:25:45.210 and the cat goes back to the center. 00:25:45.210 --> 00:25:47.620 And so you can ask multiple questions too-- each time 00:25:47.620 --> 00:25:51.970 Answer is whatever the answer was to the most recent question. 00:25:51.970 --> 00:25:54.220 And using the answers to those questions, 00:25:54.220 --> 00:25:57.340 we can start to do some interesting things. 00:25:57.340 --> 00:26:00.580 We can have the cat move around to various different places, 00:26:00.580 --> 00:26:04.940 and we can even use this in other blocks to control our project as well. 00:26:04.940 --> 00:26:07.360 Let's get rid of the cat for now and bring back 00:26:07.360 --> 00:26:10.870 another animal we've used before-- we'll bring back the bear. 00:26:10.870 --> 00:26:14.140 And the bear has a few costumes you might remember-- 00:26:14.140 --> 00:26:17.320 if we go into costumes for the bear, we see that there's this one costume 00:26:17.320 --> 00:26:19.730 called Bear A-- where the bear looks like this-- 00:26:19.730 --> 00:26:25.450 and there's another costume called Bear B where the bear looks like that. 00:26:25.450 --> 00:26:28.420 I'm going to go ahead and rename these costumes-- it turns out 00:26:28.420 --> 00:26:31.120 you can name costumes whatever you want to call them, 00:26:31.120 --> 00:26:34.090 but I'm going to call this costume 4. 00:26:34.090 --> 00:26:37.420 Just the number 4 because the bear is on four legs-- 00:26:37.420 --> 00:26:39.475 and I'll give this costume a name too, I'll 00:26:39.475 --> 00:26:44.260 give it the name 2 because the bear is on two legs. 00:26:44.260 --> 00:26:45.560 Now, why am I doing this? 00:26:45.560 --> 00:26:49.750 Well, it's because of what I'm about to show you, which is let's add an event-- 00:26:49.750 --> 00:26:53.920 when the flag is clicked, I want to ask a question, 00:26:53.920 --> 00:26:59.380 and the question I'm going to ask is, "how many legs? " 00:26:59.380 --> 00:27:04.570 And now what I'll do is I'll go into Looks and I'll say Switch Costume To. 00:27:04.570 --> 00:27:09.610 And notice that Switch Costume To also has an oval-shaped input next to it. 00:27:09.610 --> 00:27:12.820 This oval-shaped input is a dropdown where I could choose which costume I 00:27:12.820 --> 00:27:14.950 want the bear to use-- 00:27:14.950 --> 00:27:20.050 but because it's oval-shaped, I can just put my own value inside of that oval 00:27:20.050 --> 00:27:21.790 if I want to. 00:27:21.790 --> 00:27:24.250 And let's, instead of using the dropdown, 00:27:24.250 --> 00:27:28.250 let's just have it go to the answer. 00:27:28.250 --> 00:27:30.250 And so now I press the green flag-- 00:27:30.250 --> 00:27:36.340 the bear asks "how many legs? " Let's say four legs for the bear-- 00:27:36.340 --> 00:27:38.113 and now the bear's on four legs. 00:27:38.113 --> 00:27:40.780 If I want the bear to stand up, I'll press the green flag again, 00:27:40.780 --> 00:27:45.340 it says "how many legs? " I press 2, and the bear stands up. 00:27:45.340 --> 00:27:48.700 And, again, the reason that's happening is because when it asks how many legs, 00:27:48.700 --> 00:27:51.590 I type in something like 2 as my answer. 00:27:51.590 --> 00:27:55.120 And so this block runs, we're going to Switch Costume To-- 00:27:55.120 --> 00:27:57.070 in this case, 2-- 00:27:57.070 --> 00:28:00.160 and because I named my costumes after those answers-- 00:28:00.160 --> 00:28:02.620 you'll remember that if I go back to the Costumes tab, 00:28:02.620 --> 00:28:05.170 I had given those costumes particular names. 00:28:05.170 --> 00:28:08.230 I called one of the costumes 4, for the four leg version, 00:28:08.230 --> 00:28:11.410 and one of the costumes 2 for the two leg version. 00:28:11.410 --> 00:28:15.610 Well then I can use those blocks to control how many legs the bear is 00:28:15.610 --> 00:28:17.170 ultimately going to be standing on. 00:28:17.170 --> 00:28:18.970 And that works for costumes, and it works 00:28:18.970 --> 00:28:22.460 for other parts of Scratch's interface, like backdrops, for example. 00:28:22.460 --> 00:28:23.710 So we could try that out too-- 00:28:23.710 --> 00:28:26.560 I'll go ahead and get rid of the-- 00:28:26.560 --> 00:28:30.200 we'll keep the bear for now, but I'll get rid of the question. 00:28:30.200 --> 00:28:34.150 Let's add some backdrops, let's add Colorful City. 00:28:34.150 --> 00:28:37.540 I like the look of that, the colorful city during the day, 00:28:37.540 --> 00:28:39.070 and let's add one more backdrop. 00:28:39.070 --> 00:28:45.310 Let's add the Night City, so we have a couple different backdrops 00:28:45.310 --> 00:28:47.800 and I'm going to change their names. 00:28:47.800 --> 00:28:51.490 This Colorful city, I'm going to call it Day-- 00:28:51.490 --> 00:28:55.450 and again, just up at the top here, I can change the name of this backdrop. 00:28:55.450 --> 00:29:00.100 And for the Night City, I'm going to call it Night. 00:29:00.100 --> 00:29:07.540 So I have one backdrop called Day and one backdrop that I've called Night-- 00:29:07.540 --> 00:29:10.270 and let's go back into the code. 00:29:10.270 --> 00:29:12.460 And now for the bear-- 00:29:12.460 --> 00:29:15.460 when the flag is clicked, I want the bear to ask a question. 00:29:19.250 --> 00:29:22.080 "What time? " Like, what time of day is it? 00:29:22.080 --> 00:29:28.752 And let's go ahead and switch the backdrop to whatever the answer is. 00:29:28.752 --> 00:29:30.710 So the bear is going to ask me what time of day 00:29:30.710 --> 00:29:34.280 do I want-- if I say day, that's one possible answer, if I say night, that's 00:29:34.280 --> 00:29:35.672 another possible answer. 00:29:35.672 --> 00:29:36.380 So we'll try it-- 00:29:36.380 --> 00:29:39.380 I'll press the flag, "what time? " I'll say night-- 00:29:39.380 --> 00:29:43.130 I'll press Return, and now it's nighttime. 00:29:43.130 --> 00:29:44.480 I'll try it again-- 00:29:44.480 --> 00:29:49.760 "what time of day? " I'll say day, and now it's daytime. 00:29:49.760 --> 00:29:52.610 And so using this ability now to have Return values, 00:29:52.610 --> 00:29:55.520 we can create much more interactive and much more exciting 00:29:55.520 --> 00:29:58.280 programs by letting the user tell us something 00:29:58.280 --> 00:30:00.080 and using that answer in the project. 00:30:00.080 --> 00:30:02.780 Letting them type in their name and responding to them by name, 00:30:02.780 --> 00:30:05.540 or letting them type in what backdrop or costume they want, 00:30:05.540 --> 00:30:07.670 or letting them type in how they want to control-- 00:30:07.670 --> 00:30:11.150 how sprite moves, or where it's pointing, or in what direction 00:30:11.150 --> 00:30:12.420 it's going for example. 00:30:12.420 --> 00:30:14.570 And so you give the user a lot more control 00:30:14.570 --> 00:30:18.677 over what's happening just by taking advantage of these values. 00:30:18.677 --> 00:30:21.510 That's it for an Introduction to Programming with Scratch for today. 00:30:21.510 --> 00:30:23.240 Next time, we'll continue this discussion 00:30:23.240 --> 00:30:25.130 and to take a look at some other features 00:30:25.130 --> 00:30:28.780 that we get in this world of programming with Scratch.