WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000 00:00:00.000 --> 00:00:04.455 [MUSIC PLAYING] 00:00:17.033 --> 00:00:19.200 BRIAN YU: Welcome back, everyone, to an introduction 00:00:19.200 --> 00:00:20.520 to programming with Scratch. 00:00:20.520 --> 00:00:24.030 And last time, we took a look at values, pieces of information 00:00:24.030 --> 00:00:26.460 that are stored inside of our Scratch programs 00:00:26.460 --> 00:00:29.190 that we could use inside of those programs. 00:00:29.190 --> 00:00:31.950 Today we'll take that idea one step further and take a look 00:00:31.950 --> 00:00:35.280 at how we can use information inside of our Scratch programs 00:00:35.280 --> 00:00:39.360 to make decisions by asking questions, and then deciding what to do 00:00:39.360 --> 00:00:42.000 based on the answer to those questions. 00:00:42.000 --> 00:00:45.300 And people do this type of thing all the time, asking a question 00:00:45.300 --> 00:00:46.560 and then making a decision. 00:00:46.560 --> 00:00:48.977 When you're deciding when you're trying to leave the home, 00:00:48.977 --> 00:00:51.270 you might look outside and ask, is it cold outside? 00:00:51.270 --> 00:00:53.353 And if it's cold outside, you might wear a jacket. 00:00:53.353 --> 00:00:55.090 Or you might ask, is it raining today? 00:00:55.090 --> 00:00:56.923 And if it's raining, well, then you're going 00:00:56.923 --> 00:00:59.710 to take the action of bringing an umbrella with you, for example. 00:00:59.710 --> 00:01:03.170 And you can think of this type of logic as having two parts. 00:01:03.170 --> 00:01:04.800 There's a question that you're asking. 00:01:04.800 --> 00:01:05.850 Is it raining outside? 00:01:05.850 --> 00:01:07.020 Is it cold outside? 00:01:07.020 --> 00:01:09.450 And then there's the logic, the decision that you make 00:01:09.450 --> 00:01:11.830 based on the answer to that question. 00:01:11.830 --> 00:01:13.320 And computers do the same thing. 00:01:13.320 --> 00:01:16.710 Computers ask questions like, is the computer low on battery? 00:01:16.710 --> 00:01:18.480 Is the computer connected to the internet? 00:01:18.480 --> 00:01:20.920 And based on the answers to those questions, 00:01:20.920 --> 00:01:22.380 the computer makes a decision. 00:01:22.380 --> 00:01:25.950 And today we'll try to do exactly that sort of thing inside of our Scratch 00:01:25.950 --> 00:01:28.290 programs as well-- asking a question and then 00:01:28.290 --> 00:01:32.200 making a decision based on the answer to that question. 00:01:32.200 --> 00:01:35.760 So let's go ahead and open up Scratch, and we see the familiar Scratch cat 00:01:35.760 --> 00:01:36.690 here. 00:01:36.690 --> 00:01:41.490 And let's make something happen whenever the Space key is pressed. 00:01:41.490 --> 00:01:42.580 That's one of our events. 00:01:42.580 --> 00:01:47.640 So I'll drag that when Space key press event out into my code editor. 00:01:47.640 --> 00:01:50.940 And when the Space is pressed, let's play a sound. 00:01:50.940 --> 00:01:53.330 And we'll play the meow sound. 00:01:53.330 --> 00:01:55.080 So now, every time I press the Space key-- 00:01:55.080 --> 00:01:57.540 [MEOWING] 00:01:57.540 --> 00:01:59.790 You'll hear the cat meow. 00:01:59.790 --> 00:02:02.350 But now, to make this a little bit more interesting, 00:02:02.350 --> 00:02:06.120 let's introduce a new block-- which you can find under the Control section 00:02:06.120 --> 00:02:07.380 of the blocks-- 00:02:07.380 --> 00:02:10.169 and that's this if block. 00:02:10.169 --> 00:02:12.300 And what you'll notice in this if block is 00:02:12.300 --> 00:02:16.330 that there are two places where we can add additional blocks into this if 00:02:16.330 --> 00:02:16.980 block. 00:02:16.980 --> 00:02:22.680 You'll notice that next to the word if is a little hexagonal shaped region 00:02:22.680 --> 00:02:24.502 where I could drop a block in there. 00:02:24.502 --> 00:02:25.960 And we'll do that in just a moment. 00:02:25.960 --> 00:02:29.490 And this hexagon is going to represent the question that we're asking. 00:02:29.490 --> 00:02:32.640 We're going to ask a question inside of our Scratch program. 00:02:32.640 --> 00:02:34.140 It's going to be a yes/no question. 00:02:34.140 --> 00:02:36.630 Is the answer to the question yes or no? 00:02:36.630 --> 00:02:40.410 Or equivalently, is this question true or false? 00:02:40.410 --> 00:02:42.390 And based on the answer to that question, 00:02:42.390 --> 00:02:44.880 we might run some additional blocks that are 00:02:44.880 --> 00:02:48.630 contained inside of this if block here. 00:02:48.630 --> 00:02:50.820 You see this block has a top and a bottom, 00:02:50.820 --> 00:02:54.510 and what that's going to allow us to do is enclose other blocks 00:02:54.510 --> 00:02:58.190 inside of this if block right here. 00:02:58.190 --> 00:02:59.940 So let's take a look at an example of that 00:02:59.940 --> 00:03:04.350 and see how we can fill in those two different parts of the if block. 00:03:04.350 --> 00:03:07.080 The first thing we need is a question, and for that, we 00:03:07.080 --> 00:03:10.230 can go down into the Sensing section of blocks. 00:03:10.230 --> 00:03:14.350 And you'll notice there are a couple of blocks that have this hexagon shape. 00:03:14.350 --> 00:03:16.050 These are questions that we can ask. 00:03:16.050 --> 00:03:20.040 In computer science, we often call these questions Boolean expressions. 00:03:20.040 --> 00:03:24.180 A Boolean expression is a fancy way of saying anything that is either true-- 00:03:24.180 --> 00:03:27.690 meaning yes-- or false, meaning no. 00:03:27.690 --> 00:03:30.660 And the very first one here in the Sensing category 00:03:30.660 --> 00:03:34.650 is a Boolean expression called touching mouse pointer. 00:03:34.650 --> 00:03:38.258 So I'll go ahead and drag that into the hexagon here, in the if statement. 00:03:38.258 --> 00:03:40.050 And you'll notice that it snaps into place. 00:03:40.050 --> 00:03:42.510 It fits right inside of that if statement, 00:03:42.510 --> 00:03:45.760 which grows to fit the size of the hexagon block. 00:03:45.760 --> 00:03:49.240 And then I'll take the play sound meow until done block, 00:03:49.240 --> 00:03:54.850 and drag it inside of the if statement, and connect all of that 00:03:54.850 --> 00:03:58.300 to the event when the Space key is pressed. 00:03:58.300 --> 00:03:59.920 So now the logic is this. 00:03:59.920 --> 00:04:03.760 When I press the space key, the if block is going to ask that question. 00:04:03.760 --> 00:04:07.880 Is the cat touching the mouse pointer, the cursor on the screen? 00:04:07.880 --> 00:04:10.360 And if the answer is yes, then we're going 00:04:10.360 --> 00:04:12.040 to play the sound meow until done. 00:04:12.040 --> 00:04:16.779 But that's only going to happen if this condition is true, if we are actually 00:04:16.779 --> 00:04:18.430 touching the mouse pointer. 00:04:18.430 --> 00:04:21.339 So if I put my mouse pointer somewhere other than the cat 00:04:21.339 --> 00:04:26.620 and I press the Spacebar, you'll notice that I don't hear anything. 00:04:26.620 --> 00:04:29.880 But if I move my cursor over the cat and now press the Spacebar-- 00:04:29.880 --> 00:04:32.772 [MEOWING] 00:04:34.220 --> 00:04:39.320 Now, every time I press the Spacebar, you do, in fact, hear the cat meow. 00:04:39.320 --> 00:04:43.730 So our programs using these conditions now have the ability to make decisions, 00:04:43.730 --> 00:04:47.730 to ask a question and then make a decision based on the answer. 00:04:47.730 --> 00:04:50.090 And this touching mouse pointer block can 00:04:50.090 --> 00:04:53.240 be used to check if the sprite is touching the mouse pointer, 00:04:53.240 --> 00:04:56.120 but also if it's touching another sprite, for example. 00:04:56.120 --> 00:04:58.820 Let's say the cat is playing with a balloon. 00:04:58.820 --> 00:05:00.410 I can drag a balloon out. 00:05:00.410 --> 00:05:02.360 So we've got the cat and the balloon. 00:05:02.360 --> 00:05:07.460 And I'll say, for the cat, now, if you're touching the balloon, 00:05:07.460 --> 00:05:09.170 then play the meow sound. 00:05:09.170 --> 00:05:14.505 So right now I press the Spacebar, and nothing happens, 00:05:14.505 --> 00:05:16.880 but if I move the cat so that it's touching the balloon-- 00:05:16.880 --> 00:05:18.680 [MEOWING] 00:05:18.680 --> 00:05:23.230 Well, now we're able to detect that. 00:05:23.230 --> 00:05:26.090 We're able to sense that the cat is touching the balloon, 00:05:26.090 --> 00:05:29.350 and because of that we're able to make some decisions. 00:05:29.350 --> 00:05:31.630 And these sensing blocks give you the ability 00:05:31.630 --> 00:05:35.440 to allow your sprites to sense the things around it, figure out how close 00:05:35.440 --> 00:05:37.960 or how far away are other rights [INAUDIBLE] the cursor, 00:05:37.960 --> 00:05:41.290 figure out what it's touching, whether it's touching a particular sprite 00:05:41.290 --> 00:05:44.020 or even touching a particular color. 00:05:44.020 --> 00:05:47.680 This block here lets you check if the sprite is touching something 00:05:47.680 --> 00:05:50.050 that is a particular color, and you can use 00:05:50.050 --> 00:05:54.178 that to build some interesting interfaces as well. 00:05:54.178 --> 00:05:55.220 So let's give that a try. 00:05:55.220 --> 00:05:59.440 I'll get rid of the balloon for now, and let's add a backdrop 00:05:59.440 --> 00:06:01.150 for our cat to play around in. 00:06:01.150 --> 00:06:05.060 And I want to try the winter backdrop here. 00:06:05.060 --> 00:06:06.580 So you've got a winter backdrop. 00:06:06.580 --> 00:06:08.710 And now the code I'm going to write is this. 00:06:08.710 --> 00:06:10.900 We're no longer going to respond to the Space key, 00:06:10.900 --> 00:06:14.360 so I'll delete that by dragging it off. 00:06:14.360 --> 00:06:18.110 But now, when the right arrow key is pressed, 00:06:18.110 --> 00:06:20.000 I'd like the cat to move to the right. 00:06:20.000 --> 00:06:21.500 This is something we've seen before. 00:06:21.500 --> 00:06:24.470 Moving is controlled by a motion block, and we 00:06:24.470 --> 00:06:27.290 are going to change the x value by 10. 00:06:27.290 --> 00:06:30.050 Remember, the x is, how far to the left or to the right 00:06:30.050 --> 00:06:32.030 is this particular sprite? 00:06:32.030 --> 00:06:35.600 So when the right arrow's pressed, we're going to change the x value by 10, 00:06:35.600 --> 00:06:39.200 but now what I'd like to do is get the cat to detect somehow 00:06:39.200 --> 00:06:41.070 when it's touching one of the trees. 00:06:41.070 --> 00:06:42.470 There's a tree off to the right. 00:06:42.470 --> 00:06:43.762 There's a tree off to the left. 00:06:43.762 --> 00:06:46.850 If the cat is ever touching a tree, I'd like the cat to know, 00:06:46.850 --> 00:06:48.360 so we can report as much. 00:06:48.360 --> 00:06:51.200 It can say that it found a tree, for instance. 00:06:51.200 --> 00:06:52.728 How could we do that? 00:06:52.728 --> 00:06:54.020 Well, the tree is not a sprite. 00:06:54.020 --> 00:06:57.120 It's not one of these sprites that I see down below. 00:06:57.120 --> 00:06:58.520 It's just part of the backdrop. 00:06:58.520 --> 00:06:59.850 It's just there. 00:06:59.850 --> 00:07:02.360 And so if I want to detect it, I can detect it 00:07:02.360 --> 00:07:04.790 by looking for a particular color. 00:07:04.790 --> 00:07:08.100 So I might say, if-- 00:07:08.100 --> 00:07:09.960 and then going to Sensing-- 00:07:09.960 --> 00:07:12.290 touching color. 00:07:12.290 --> 00:07:15.070 And if I click on this little color well here in this oval, 00:07:15.070 --> 00:07:18.960 I can control what color I'd like to check to see if I'm touching. 00:07:18.960 --> 00:07:22.210 And I could try to come up with a green that looks like the green of the tree, 00:07:22.210 --> 00:07:24.550 but it'll be difficult for me to get it exactly. 00:07:24.550 --> 00:07:27.580 And for that reason, there's a great tool down below. 00:07:27.580 --> 00:07:30.880 If I click on this eyedropper button, that 00:07:30.880 --> 00:07:32.800 lets me pick a color from the stage. 00:07:32.800 --> 00:07:35.590 You'll notice I can drag my cursor over the stage. 00:07:35.590 --> 00:07:37.660 It's picking out individual colors. 00:07:37.660 --> 00:07:41.710 And I can say, I want you to detect this color right here, this green 00:07:41.710 --> 00:07:43.880 that my cursor's hovering over now. 00:07:43.880 --> 00:07:45.950 And you'll notice that, when I click there, 00:07:45.950 --> 00:07:48.040 now the colors automatically update. 00:07:48.040 --> 00:07:51.070 Now my cat is going to be checking if it's ever touching 00:07:51.070 --> 00:07:54.050 that green that makes up those trees. 00:07:54.050 --> 00:07:56.440 And if it is touching the green that makes up the trees, 00:07:56.440 --> 00:08:00.490 well, then let's go ahead and have the cat say something. 00:08:00.490 --> 00:08:05.300 And the cat's going to say, I found a tree. 00:08:05.300 --> 00:08:09.970 So now the cat can move to the right every time I press the right key. 00:08:09.970 --> 00:08:10.900 And it just moves. 00:08:10.900 --> 00:08:12.012 That's all it's doing. 00:08:12.012 --> 00:08:12.970 But it's also checking. 00:08:12.970 --> 00:08:16.802 It's asking that question, is the cat touching the green? 00:08:16.802 --> 00:08:19.510 And it's not touching the green, so nothing's happening just yet. 00:08:19.510 --> 00:08:23.110 Every time I press the arrow key, we're just changing the x value. 00:08:23.110 --> 00:08:24.130 But watch what happens. 00:08:24.130 --> 00:08:28.030 Eventually-- and it'll probably happen right there-- 00:08:28.030 --> 00:08:33.530 the cat touches the green in the tree, and the cat says, I found a tree. 00:08:33.530 --> 00:08:36.652 So these sensing blocks allow us to let our sprites 00:08:36.652 --> 00:08:38.860 be a little more intelligent about what's around it-- 00:08:38.860 --> 00:08:42.020 what's in the backdrop, what colors does it happen to be touching, 00:08:42.020 --> 00:08:44.360 and what spritz does it happen to be touching. 00:08:44.360 --> 00:08:49.190 And we can use that to create some interesting programs as well. 00:08:49.190 --> 00:08:52.243 But let's now revisit one of the programs that we already made 00:08:52.243 --> 00:08:54.160 and see if we can make it a little better now. 00:08:54.160 --> 00:08:57.040 I'll go ahead and delete the cat and change the backdrop. 00:08:57.040 --> 00:09:00.970 We'll go back to just the plain white backdrop. 00:09:00.970 --> 00:09:03.520 And let's bring back the balloon. 00:09:03.520 --> 00:09:06.280 You might remember from when we were working with the balloon 00:09:06.280 --> 00:09:09.160 a little bit earlier that we were making the balloon bigger. 00:09:09.160 --> 00:09:11.368 Every time we blew, for example, into the microphone, 00:09:11.368 --> 00:09:13.577 the balloon was getting bigger and bigger and bigger. 00:09:13.577 --> 00:09:15.830 In reality, you can't make a balloon bigger forever. 00:09:15.830 --> 00:09:18.910 Eventually, that balloon is going to pop, and so let's 00:09:18.910 --> 00:09:20.620 get this balloon to eventually pop. 00:09:20.620 --> 00:09:24.280 I'll go ahead and center the balloon by changing the x and the y 00:09:24.280 --> 00:09:26.450 values both to 0. 00:09:26.450 --> 00:09:30.920 And now I'll have the balloon respond to me pressing the Space key. 00:09:30.920 --> 00:09:34.340 The Space key is how I'm going to inflate this balloon. 00:09:34.340 --> 00:09:43.020 And when the Space is pressed, I would like to change the size by 10. 00:09:43.020 --> 00:09:46.110 This is similar to what we had before where, whenever something happens-- 00:09:46.110 --> 00:09:48.623 in this case, I'm pressing the Space key-- the balloon just 00:09:48.623 --> 00:09:50.040 gets bigger and bigger and bigger. 00:09:50.040 --> 00:09:52.590 And you can see the size down below grow. 00:09:52.590 --> 00:09:53.860 It's 170 now. 00:09:53.860 --> 00:09:54.880 Now it's 180. 00:09:54.880 --> 00:09:55.780 Now it's 190. 00:09:55.780 --> 00:09:57.720 Now it's 200, for example. 00:09:57.720 --> 00:10:00.180 And this maybe feels big enough. 00:10:00.180 --> 00:10:05.510 Once it gets to size 200, then I want to have the balloon pop, for example. 00:10:05.510 --> 00:10:10.050 So let's bring the size back to 100, back to the original size. 00:10:10.050 --> 00:10:11.820 I can ask a question. 00:10:11.820 --> 00:10:15.312 I can go to Control and bring out an if statement. 00:10:15.312 --> 00:10:18.270 And this time, the question I want to ask is not whether the balloon is 00:10:18.270 --> 00:10:22.320 touching something-- it's whether the size is 100-- 00:10:22.320 --> 00:10:23.040 or 200. 00:10:23.040 --> 00:10:27.120 And how could I check to see if the size is 200? 00:10:27.120 --> 00:10:30.730 Well, down here in Operators-- we've seen a couple of operators before. 00:10:30.730 --> 00:10:33.930 We've seen the operators to do math, like addition, multiplication, 00:10:33.930 --> 00:10:35.340 subtraction, and division. 00:10:35.340 --> 00:10:37.740 We've seen the operator that picks a random number. 00:10:37.740 --> 00:10:40.530 And these were all oval-shaped operators. 00:10:40.530 --> 00:10:44.650 But now let's take a look at some of these hexagon-shaped operators. 00:10:44.650 --> 00:10:48.090 These hexagon-shaped operators are all Boolean expressions, 00:10:48.090 --> 00:10:52.830 things that can be either true or false, answered yes or no. 00:10:52.830 --> 00:10:58.800 And one of them here is whether something is equal to something else. 00:10:58.800 --> 00:11:02.650 So if I have two values and I want to know, are they equal to each other, 00:11:02.650 --> 00:11:06.300 I can use this check here-- this hexagon-shaped block-- 00:11:06.300 --> 00:11:09.503 to check to see if those two values are equal to each other. 00:11:09.503 --> 00:11:10.920 And that's what I want to do here. 00:11:10.920 --> 00:11:13.770 I want to take my sprite, the balloon, and check 00:11:13.770 --> 00:11:17.640 to see if the size is equal to 200. 00:11:17.640 --> 00:11:21.210 So I'll take this block, drag it into the hexagon area 00:11:21.210 --> 00:11:23.220 next to the if statement. 00:11:23.220 --> 00:11:29.340 And let me grab the size value, which is under the Looks category. 00:11:29.340 --> 00:11:36.790 Here's my size value, and I want to check if the size is equal to 200. 00:11:36.790 --> 00:11:39.730 And if the size is equal to 200, what do I want to do? 00:11:39.730 --> 00:11:42.630 Well, the first thing I'll have this program do is hide. 00:11:42.630 --> 00:11:44.550 The balloon's going to hide. 00:11:44.550 --> 00:11:46.710 And then, just for fun, let's go into sound 00:11:46.710 --> 00:11:51.092 and have the balloon play the sound pop until it's done. 00:11:51.092 --> 00:11:52.800 So now, what is the logic of the program? 00:11:52.800 --> 00:11:56.610 Well, every time I press the Space key, we're going to run this line-- 00:11:56.610 --> 00:11:58.260 change the size by 10. 00:11:58.260 --> 00:12:00.450 But we're also going to ask that question-- 00:12:00.450 --> 00:12:02.970 check if the size is equal to 200. 00:12:02.970 --> 00:12:06.990 And only if the answer to that question is yes, then we hide the balloon, 00:12:06.990 --> 00:12:10.240 and then we play the sound pop until it's done. 00:12:10.240 --> 00:12:13.020 So let's give the program a try and see what happens 00:12:13.020 --> 00:12:15.270 once I start to inflate this balloon. 00:12:15.270 --> 00:12:16.980 It's at size 100 right now. 00:12:16.980 --> 00:12:21.240 And remember, you can check that just by looking down below here at the size. 00:12:21.240 --> 00:12:22.950 And let's press the Space. 00:12:22.950 --> 00:12:23.880 It inflates a little. 00:12:23.880 --> 00:12:28.740 Its size is 110, 120, 130, 140, 150. 00:12:28.740 --> 00:12:31.110 And now it's at 190, and watch what happens 00:12:31.110 --> 00:12:33.720 the next time I try and inflate the balloon one more time, 00:12:33.720 --> 00:12:37.400 as by pressing the Space key. 00:12:37.400 --> 00:12:39.740 It goes ahead and it hides. 00:12:39.740 --> 00:12:42.640 And I could try that again. 00:12:42.640 --> 00:12:44.920 We'll go ahead and show the balloon again, 00:12:44.920 --> 00:12:47.840 and I can do that by using this Show block here. 00:12:47.840 --> 00:12:50.560 I'll just click on Show, and it will show the balloon. 00:12:50.560 --> 00:13:00.600 And the balloon inflates, and then it disappears with a pop. 00:13:00.600 --> 00:13:02.540 So that's how we can use conditions to check 00:13:02.540 --> 00:13:04.430 if two things are equal to each other. 00:13:04.430 --> 00:13:06.557 But you probably noticed down in Operators 00:13:06.557 --> 00:13:08.390 that we don't just have the ability to check 00:13:08.390 --> 00:13:10.200 if two things are equal to each other. 00:13:10.200 --> 00:13:14.270 We can check if one number is less than another number or greater than another 00:13:14.270 --> 00:13:15.660 number, for example. 00:13:15.660 --> 00:13:18.080 And let's give that a try. 00:13:18.080 --> 00:13:20.810 Instead of using the balloon, I'll bring out another sprite. 00:13:20.810 --> 00:13:27.380 Let's choose the duck this time. 00:13:27.380 --> 00:13:32.060 I'll center the duck by moving it to 0 for x and 0 for y. 00:13:32.060 --> 00:13:35.810 And when I press the green flag to start this program, 00:13:35.810 --> 00:13:38.030 the duck is going to ask a question. 00:13:38.030 --> 00:13:42.040 And the question might be, type a number. 00:13:42.040 --> 00:13:44.120 So it's asking me to type in a number. 00:13:44.120 --> 00:13:47.440 So now, when I press the green flag, the duck asks me to type in a number, 00:13:47.440 --> 00:13:48.820 and I could type in a number. 00:13:48.820 --> 00:13:52.750 I could type in the number 1, for example, and press Return. 00:13:52.750 --> 00:13:57.193 So the duck now has a number, and now we could ask questions about that number. 00:13:57.193 --> 00:13:59.110 So if I want to check to see if that number is 00:13:59.110 --> 00:14:03.220 positive or negative, for example, I could add a condition. 00:14:03.220 --> 00:14:10.430 I could say, if, and then take the greater than block out-- 00:14:10.430 --> 00:14:12.230 which is under Operators. 00:14:12.230 --> 00:14:14.900 And now I want to say, if the answer-- 00:14:14.900 --> 00:14:19.190 which is in the Sensing category-- if the answer is greater than 0, 00:14:19.190 --> 00:14:22.800 well, then whatever number the user typed in-- that's a positive number. 00:14:22.800 --> 00:14:25.640 And so we'll go into the Looks section, and let's go ahead 00:14:25.640 --> 00:14:30.312 and say positive for two seconds. 00:14:30.312 --> 00:14:33.520 So we ask the user for a number, and if the user types in a positive number-- 00:14:33.520 --> 00:14:35.740 something like the number 2-- 00:14:35.740 --> 00:14:37.260 well, the duck says positive-- 00:14:37.260 --> 00:14:37.760 great. 00:14:37.760 --> 00:14:42.070 And instead, if we typed in a negative number-- let's say negative 2-- 00:14:42.070 --> 00:14:43.990 the duck doesn't say anything at all. 00:14:43.990 --> 00:14:46.790 So we're able to check to see if their answer is positive, 00:14:46.790 --> 00:14:50.290 and if what the user typed in was positive-- if that's true, then 00:14:50.290 --> 00:14:52.780 we're going to say positive for two seconds. 00:14:52.780 --> 00:14:56.500 But oftentimes, we want to be able to make a decision based 00:14:56.500 --> 00:14:58.490 on either answer to the question. 00:14:58.490 --> 00:15:01.660 So far, with our if blocks, we've been asking a question, 00:15:01.660 --> 00:15:03.500 is the answer to this question yes? 00:15:03.500 --> 00:15:04.360 Is it true? 00:15:04.360 --> 00:15:07.240 And if so, then we're going to run some code. 00:15:07.240 --> 00:15:09.560 There's one block of code that we're running, 00:15:09.560 --> 00:15:12.880 if it's the case that the answer to the question is yes. 00:15:12.880 --> 00:15:15.100 But sometimes we want to handle both situations. 00:15:15.100 --> 00:15:17.830 If the answer to a question is yes, we want to do one thing, 00:15:17.830 --> 00:15:22.850 and if the answer to a question is no, then we want to do something else. 00:15:22.850 --> 00:15:25.160 And so let's give that a try. 00:15:25.160 --> 00:15:29.500 Turns out there's another block that we can use-- not just the if then block, 00:15:29.500 --> 00:15:34.400 but another block under Control called if then else. 00:15:34.400 --> 00:15:38.260 And the if then else block is structured differently than the other blocks 00:15:38.260 --> 00:15:39.220 we've seen so far. 00:15:39.220 --> 00:15:43.030 The top part looks very much like the if block we were working with before. 00:15:43.030 --> 00:15:47.350 There's a hexagon-shaped space for a question, and then an area beneath it 00:15:47.350 --> 00:15:51.280 where we could add a stack of blocks for what should happen 00:15:51.280 --> 00:15:53.920 if the answer to that question is yes. 00:15:53.920 --> 00:15:57.340 But this block now has another section called else, 00:15:57.340 --> 00:16:01.900 which has what appears to be an area for yet another stack of blocks. 00:16:01.900 --> 00:16:05.560 So inside of this block can be two separate stacks of blocks-- 00:16:05.560 --> 00:16:09.820 one stack of blocks that run if the answer to the question is yes, 00:16:09.820 --> 00:16:14.750 and another stack of blocks that run if the answer to the question is no. 00:16:14.750 --> 00:16:16.840 So regardless of the answer to the question, 00:16:16.840 --> 00:16:20.330 we're going to make a decision about which of these two blocks 00:16:20.330 --> 00:16:21.820 we're ultimately going to run. 00:16:21.820 --> 00:16:25.750 And we can use that now to handle both possibilities-- either the number 00:16:25.750 --> 00:16:29.570 is greater than 0 or it's not. 00:16:29.570 --> 00:16:32.470 And so here I'll take this answer greater than 0 block, 00:16:32.470 --> 00:16:34.610 drag it into this condition. 00:16:34.610 --> 00:16:36.730 And in that case, we're going to say positive. 00:16:36.730 --> 00:16:39.580 I'll get rid of this if statement, because I no longer need it. 00:16:39.580 --> 00:16:42.550 Now I'm going to replace it with this if else. 00:16:42.550 --> 00:16:46.870 If the answer is greater than 0, we're going to say positive for two seconds. 00:16:46.870 --> 00:16:49.840 Otherwise-- let's go into Looks-- 00:16:49.840 --> 00:16:55.630 let's say negative for two seconds. 00:16:55.630 --> 00:16:59.370 So now we're making a decision-- running some block of code either time, 00:16:59.370 --> 00:17:03.030 but making a decision about which block of code we would like to run. 00:17:03.030 --> 00:17:04.290 I'll start the program. 00:17:04.290 --> 00:17:05.609 It's asking me for a number. 00:17:05.609 --> 00:17:08.550 I'll type in a positive number, like the number 2. 00:17:08.550 --> 00:17:10.980 And the duck says positive. 00:17:10.980 --> 00:17:12.170 We'll try it again. 00:17:12.170 --> 00:17:13.170 Asking me for a number-- 00:17:13.170 --> 00:17:15.300 I'll type in a negative number-- negative 2. 00:17:15.300 --> 00:17:17.260 And the duck says negative. 00:17:17.260 --> 00:17:20.290 So if the answer to the question is yes, we run one block. 00:17:20.290 --> 00:17:22.829 Otherwise, we run another block. 00:17:22.829 --> 00:17:24.780 And now, there's a slight bug in this program. 00:17:24.780 --> 00:17:27.089 You might have noticed that it does well for positive numbers 00:17:27.089 --> 00:17:29.280 and it does well for negative numbers, but there 00:17:29.280 --> 00:17:33.240 is one number that's not really positive and it's not really negative, 00:17:33.240 --> 00:17:34.710 and that's a 0. 00:17:34.710 --> 00:17:39.210 If I type in 0 in press Return, the duck says negative. 00:17:39.210 --> 00:17:40.180 Now, why is that? 00:17:40.180 --> 00:17:43.230 Well, it's asking, is the answer greater than 0? 00:17:43.230 --> 00:17:44.560 Is 0 greater than 0? 00:17:44.560 --> 00:17:45.060 Well, no. 00:17:45.060 --> 00:17:47.280 0 is not greater than 0. 00:17:47.280 --> 00:17:49.560 So instead of running the if block, we instead 00:17:49.560 --> 00:17:54.793 run this else block, where we just say negative every time. 00:17:54.793 --> 00:17:56.460 And that's maybe not quite what we want. 00:17:56.460 --> 00:18:00.300 What I probably want is to ask yet another question-- to ask again, 00:18:00.300 --> 00:18:02.580 is the answer less than 0? 00:18:02.580 --> 00:18:03.900 If so, then it's negative. 00:18:03.900 --> 00:18:07.050 And otherwise, if it's not more than 0 and it's not less than 0, 00:18:07.050 --> 00:18:09.750 then it must just be 0. 00:18:09.750 --> 00:18:15.390 And to do that, I could add yet another condition inside of this stack here. 00:18:15.390 --> 00:18:19.210 I can add anything if else inside of another if else. 00:18:19.210 --> 00:18:24.450 And to do that, I'll take another if else, drag it here underneath the else. 00:18:24.450 --> 00:18:26.280 And so now, if the number's not positive, 00:18:26.280 --> 00:18:29.940 I can ask a second question-- ask yet another question. 00:18:29.940 --> 00:18:35.520 Now the question I want to ask is, is my answer-- 00:18:35.520 --> 00:18:37.710 the answer's in the Sensing category-- 00:18:37.710 --> 00:18:40.320 is the answer to less than 0? 00:18:40.320 --> 00:18:44.460 If the answer is less than 0, then I want to say negative for two seconds, 00:18:44.460 --> 00:18:46.020 but otherwise-- 00:18:46.020 --> 00:18:47.860 let's go into Looks-- 00:18:47.860 --> 00:18:51.810 I'm just going to say 0. 00:18:51.810 --> 00:18:54.690 So this is starting to get more complex I have blocks inside 00:18:54.690 --> 00:18:56.460 of blocks inside of other blocks. 00:18:56.460 --> 00:19:00.010 And the way to read this now is, what happens if the user types in the number 00:19:00.010 --> 00:19:00.690 0? 00:19:00.690 --> 00:19:02.970 Well, we start by asking this question here-- 00:19:02.970 --> 00:19:05.850 is the answer 0 greater than 0? 00:19:05.850 --> 00:19:09.900 No, 0 is not greater than 0, so we ignore the if block 00:19:09.900 --> 00:19:12.150 and we just look to the else section. 00:19:12.150 --> 00:19:13.710 And now we ask another question. 00:19:13.710 --> 00:19:17.070 Is the answer 0 less than 0? 00:19:17.070 --> 00:19:18.940 Well, no, that's not true either. 00:19:18.940 --> 00:19:21.850 So we don't run this block, and we instead go to the else, 00:19:21.850 --> 00:19:26.340 and we run this block, which says, say 0 for two seconds. 00:19:26.340 --> 00:19:29.170 And that's exactly the behavior we want for the duck. 00:19:29.170 --> 00:19:33.310 So we can verify now that it works by pressing the green flag. 00:19:33.310 --> 00:19:36.450 And if I type in 0, the duck reports that I 00:19:36.450 --> 00:19:38.490 did, in fact, type in the number 0. 00:19:38.490 --> 00:19:42.880 If I instead typed a positive number, the duck's is going to say positive. 00:19:42.880 --> 00:19:45.870 And if I type the negative number, well, then the duck 00:19:45.870 --> 00:19:48.730 is going to say negative instead. 00:19:48.730 --> 00:19:51.960 And so using that, I have the ability to ask multiple questions 00:19:51.960 --> 00:19:56.050 and make decisions based on the answers to those questions. 00:19:56.050 --> 00:19:59.610 So let's use this ability now to build some interesting games that 00:19:59.610 --> 00:20:02.870 use our ability to ask questions and make decisions. 00:20:02.870 --> 00:20:05.100 And for that, I'll bring back one of our animals. 00:20:05.100 --> 00:20:09.180 We'll go to Animals, and let's bring back the hedgehog. 00:20:09.180 --> 00:20:12.840 And what I'd like for the hedgehog to do is to race across the stage-- 00:20:12.840 --> 00:20:15.300 start at one point in the stage, try and get all the way 00:20:15.300 --> 00:20:19.480 to the right edge of the stage, and see how quickly we can do that. 00:20:19.480 --> 00:20:22.120 So how would I go about creating this game? 00:20:22.120 --> 00:20:25.410 Well, when the green flag is clicked, when I start the program, 00:20:25.410 --> 00:20:30.180 I would like the hedgehog to move to the left side of the stage 00:20:30.180 --> 00:20:33.790 as a place to begin this particular race. 00:20:33.790 --> 00:20:36.860 So we'll have it go to a particular location. 00:20:36.860 --> 00:20:37.860 And this is about right. 00:20:37.860 --> 00:20:41.097 Maybe I'll say negative 150 and negative 25 for y. 00:20:41.097 --> 00:20:43.930 And I could play around with that to figure out exactly what I want, 00:20:43.930 --> 00:20:47.250 but I'm just choosing nice looking numbers for now. 00:20:47.250 --> 00:20:49.050 And then every time-- 00:20:49.050 --> 00:20:52.150 not the Space key, but maybe the right key is pressed, 00:20:52.150 --> 00:20:55.050 I would like for the hedgehog to move a little bit closer 00:20:55.050 --> 00:20:57.600 towards the edge of the stage. 00:20:57.600 --> 00:21:02.560 So I could have the hedgehog move 10 steps, for example. 00:21:02.560 --> 00:21:05.340 And what I would like for the hedgehog to do 00:21:05.340 --> 00:21:08.757 is, once it reaches the edge of the stage, 00:21:08.757 --> 00:21:10.840 it should report back to me, how long did it take? 00:21:10.840 --> 00:21:12.990 How many seconds did it take for the hedgehog 00:21:12.990 --> 00:21:16.630 to get from one side of the stage to the other? 00:21:16.630 --> 00:21:19.740 And so for that, I can ask a question. 00:21:19.740 --> 00:21:20.910 We'll go into Control. 00:21:20.910 --> 00:21:24.480 I'll drag an if block out, and the question I want to ask-- 00:21:24.480 --> 00:21:26.400 which will be located under Sensing-- 00:21:26.400 --> 00:21:29.550 is not whether the hedgehog is touching the mouse pointer, 00:21:29.550 --> 00:21:31.740 but if the hedgehog is touching the edge-- 00:21:31.740 --> 00:21:33.382 meaning the edge of the stage. 00:21:33.382 --> 00:21:36.090 And if the hedgehog is touching the edge of the stage, what would 00:21:36.090 --> 00:21:38.010 I like for the hedgehog to do? 00:21:38.010 --> 00:21:40.650 Well, I want the hedgehog to say something. 00:21:40.650 --> 00:21:42.630 And what do I wanted to say? 00:21:42.630 --> 00:21:47.070 Well, I want the hedgehog to say whatever this timer value is. 00:21:47.070 --> 00:21:49.200 Remember, the timer is a value that keeps 00:21:49.200 --> 00:21:53.880 track of how many seconds have passed since the beginning of my program. 00:21:53.880 --> 00:21:56.700 And I could have it say just the timer, but I'm actually 00:21:56.700 --> 00:21:58.200 going to use an operator first. 00:21:58.200 --> 00:22:04.200 I'm going to have us first round the timer, and then say that result. Why 00:22:04.200 --> 00:22:04.950 am I doing that? 00:22:04.950 --> 00:22:07.575 Well, the timer normally has something after the decimal point. 00:22:07.575 --> 00:22:10.148 Maybe like 5.28 seconds have passed or something like that, 00:22:10.148 --> 00:22:12.690 and I don't really care about what's after the decimal point. 00:22:12.690 --> 00:22:16.090 I just want to know, has it been five seconds, or six, or seven seconds, 00:22:16.090 --> 00:22:17.440 for example? 00:22:17.440 --> 00:22:21.460 So now we're going to move the hedgehog every time I press the right arrow key, 00:22:21.460 --> 00:22:23.430 and if we're touching the edge, we're going 00:22:23.430 --> 00:22:27.850 to say whatever the value of the timer is for two seconds. 00:22:27.850 --> 00:22:30.430 So let's give that a try. 00:22:30.430 --> 00:22:32.190 We'll start the program, and every time I 00:22:32.190 --> 00:22:35.460 press the right arrow, the hedgehog moves a little bit. 00:22:35.460 --> 00:22:41.080 And when it reaches the edge, it's going to report how many seconds it took. 00:22:41.080 --> 00:22:44.050 In this case, it took nine seconds for the hedgehog 00:22:44.050 --> 00:22:47.980 to get all the way to the rightmost edge of the stage. 00:22:47.980 --> 00:22:51.478 But we can keep asking more questions here, adding more blocks to the program 00:22:51.478 --> 00:22:53.020 to make it a little more interesting. 00:22:53.020 --> 00:22:57.850 I'll get rid of this, say, for now, and instead replace it with an if else-- 00:22:57.850 --> 00:23:00.610 an opportunity to ask another question. 00:23:00.610 --> 00:23:03.880 And this time the question I want to ask is this. 00:23:03.880 --> 00:23:09.140 Under Operators, I'm going to take the less than block out, and I want to ask, 00:23:09.140 --> 00:23:13.170 is the timer less than five? 00:23:13.170 --> 00:23:15.870 Remember, this question is only ask one time at the edge. 00:23:15.870 --> 00:23:19.480 Once the hedgehog has reached the edge of the stage, we're going to ask, 00:23:19.480 --> 00:23:21.840 has it been fewer than five seconds? 00:23:21.840 --> 00:23:25.860 And if so, that was pretty fast, so let's go ahead and have the hedgehog 00:23:25.860 --> 00:23:30.210 say fast for two seconds. 00:23:30.210 --> 00:23:35.587 And otherwise, if the timer was not less than five, then 00:23:35.587 --> 00:23:37.920 we were going kind of slowly, so we'll have the hedgehog 00:23:37.920 --> 00:23:41.040 say slow for two seconds instead. 00:23:41.040 --> 00:23:43.760 So now, if I press the green flag and move quickly-- 00:23:43.760 --> 00:23:47.000 very quickly try and move the hedgehog across the stage, 00:23:47.000 --> 00:23:48.500 well, then the hedgehog says fast. 00:23:48.500 --> 00:23:51.600 I managed to make it across in fewer than five seconds. 00:23:51.600 --> 00:23:53.030 But if I'm going more slowly-- 00:23:53.030 --> 00:23:59.520 I'll try it again, and more slowly press the right arrow key. 00:23:59.520 --> 00:24:03.840 Well, then it's going to take a little bit longer, and at the end, 00:24:03.840 --> 00:24:06.600 the hedgehog does report that I was slow, that I was not 00:24:06.600 --> 00:24:08.760 very fast at moving across. 00:24:08.760 --> 00:24:10.470 So that's one possible game you could try 00:24:10.470 --> 00:24:13.380 to build by asking questions-- questions about whether we're touching 00:24:13.380 --> 00:24:17.730 the edge, questions about whether the timer is a particular value-- less 00:24:17.730 --> 00:24:19.870 than a particular value, for example. 00:24:19.870 --> 00:24:23.850 And let's try one more example of a game we could build with the hedgehog. 00:24:23.850 --> 00:24:26.460 Go ahead and get rid of these blocks for now. 00:24:26.460 --> 00:24:29.790 Let's try and build a maze for the hedgehog to try and navigate around, 00:24:29.790 --> 00:24:33.550 where you're not supposed to hit any of the walls of the maze, for example. 00:24:33.550 --> 00:24:36.120 And I could do that by changing the backdrop. 00:24:36.120 --> 00:24:39.360 Let's use the backdrop to build a maze for our hedgehog. 00:24:39.360 --> 00:24:44.430 I want the walls to be red maybe, and I'll 00:24:44.430 --> 00:24:46.650 go ahead and click on this line tool that's going 00:24:46.650 --> 00:24:50.640 to let me draw lines on the stage. 00:24:50.640 --> 00:24:54.430 Oh, and I should really be changing the outline to make the outline red. 00:24:54.430 --> 00:24:57.880 So let me delete this line for now, and try it again. 00:24:57.880 --> 00:25:00.970 I want the outline of this to be red. 00:25:00.970 --> 00:25:03.430 That's going to give me some red lines. 00:25:03.430 --> 00:25:11.650 And I'll go ahead and just try and build a little maze for our hedgehog 00:25:11.650 --> 00:25:15.210 to try to navigate around. 00:25:15.210 --> 00:25:17.370 And that looks all right. 00:25:17.370 --> 00:25:19.620 I'll go back to the code. 00:25:19.620 --> 00:25:20.597 Here's our hedgehog. 00:25:20.597 --> 00:25:22.680 It's a bit big for this maze right now, so I might 00:25:22.680 --> 00:25:24.222 want to make it a little bit smaller. 00:25:24.222 --> 00:25:27.660 I can go in the Size here, change the size to-- let's try 40. 00:25:27.660 --> 00:25:30.150 Yeah, that's probably a good size for our hedgehog. 00:25:30.150 --> 00:25:32.290 And now, how do I want this maze to work? 00:25:32.290 --> 00:25:34.980 Well, I'll have it move via the arrow keys. 00:25:34.980 --> 00:25:37.950 So whenever the right arrow key is pressed, 00:25:37.950 --> 00:25:42.840 our hedgehog is going to change its x position by, let's say, five. 00:25:42.840 --> 00:25:44.730 10 is maybe a lot for this. 00:25:44.730 --> 00:25:47.340 So it's going to move five spaces to the right. 00:25:47.340 --> 00:25:48.600 And what do I want to do? 00:25:48.600 --> 00:25:51.750 I want to check now, is the hedgehog touching a wall? 00:25:51.750 --> 00:25:55.240 Because if it ends up touching a wall, that's no good. 00:25:55.240 --> 00:26:02.190 So I'll ask, if the hedgehog is touching a color-- 00:26:02.190 --> 00:26:04.230 because the walls are all red. 00:26:04.230 --> 00:26:06.330 So I'll grab a color using this eyedropper tool, 00:26:06.330 --> 00:26:08.100 grabbing it from the stage. 00:26:08.100 --> 00:26:11.280 Let's grab this red right here. 00:26:11.280 --> 00:26:14.330 So if the hedgehog is touching the color red, 00:26:14.330 --> 00:26:18.170 well, then I want the hedgehog to just, for now, let's say, go 00:26:18.170 --> 00:26:19.760 to a random position on the stage. 00:26:19.760 --> 00:26:23.690 It's going to somehow magically disappear and reappear somewhere else. 00:26:23.690 --> 00:26:26.720 We'll have it go to a random position. 00:26:26.720 --> 00:26:29.000 And that's what happens when I press the right arrow. 00:26:29.000 --> 00:26:32.330 I'm going to duplicate this whole thing by Control clicking and pressing 00:26:32.330 --> 00:26:36.800 Duplicate, and do the same thing for the left arrow. 00:26:36.800 --> 00:26:40.610 But this time, we're going to change x by negative five. 00:26:40.610 --> 00:26:45.020 And I also want the up and down arrows to work too, so I'll duplicate once. 00:26:45.020 --> 00:26:47.060 Let's do this for the up arrow. 00:26:47.060 --> 00:26:51.800 When the up arrow happens, I want to not change the x value by something, 00:26:51.800 --> 00:26:57.630 but change the y value by negative five. 00:26:57.630 --> 00:27:01.350 And one more arrow key-- let's duplicate this one more time and use the same 00:27:01.350 --> 00:27:04.300 code for the down arrow-- 00:27:04.300 --> 00:27:07.508 this time, changing the y by-- 00:27:07.508 --> 00:27:09.300 actually, here, it should be negative five, 00:27:09.300 --> 00:27:11.700 and for up, it should be five-- because when we're going up the, 00:27:11.700 --> 00:27:14.820 y value's increasing, when we're going down, the y value's decreasing. 00:27:14.820 --> 00:27:17.430 And I could test that out by pressing the up arrow, 00:27:17.430 --> 00:27:18.930 and the hedgehog moves up. 00:27:18.930 --> 00:27:20.440 I press the down arrow-- 00:27:20.440 --> 00:27:21.552 the hedgehog moves down. 00:27:21.552 --> 00:27:23.010 I press left-- it goes to the left. 00:27:23.010 --> 00:27:24.552 I press right-- it goes to the right. 00:27:24.552 --> 00:27:29.030 But if ever I hit-- watch what happens if I hit a red wall. 00:27:29.030 --> 00:27:32.250 The hedgehog magically vanishes from where it was, 00:27:32.250 --> 00:27:33.600 and it reappears somewhere else. 00:27:33.600 --> 00:27:36.740 And you could decide on your own what happens when the hedgehog hits a wall. 00:27:36.740 --> 00:27:37.990 Maybe it plays a sound effect. 00:27:37.990 --> 00:27:39.978 Maybe it does something else. 00:27:39.978 --> 00:27:42.020 But you could make this game however you like it. 00:27:42.020 --> 00:27:44.812 And here I just have a hedgehog that can navigate through the maze, 00:27:44.812 --> 00:27:49.000 and whenever it hits a wall, it reappears somewhere else. 00:27:49.000 --> 00:27:51.500 And so these conditions give you a lot of power, the ability 00:27:51.500 --> 00:27:55.160 to ask questions and make decisions inside of your programs 00:27:55.160 --> 00:27:57.530 based on the answers to those questions. 00:27:57.530 --> 00:28:00.430 That's it for today for an introduction to programming with Scratch. 00:28:00.430 --> 00:28:02.180 Next time, we'll pick up where we left off 00:28:02.180 --> 00:28:05.480 and see what else we can do in the world of putting together these Scratch 00:28:05.480 --> 00:28:06.080 blocks. 00:28:06.080 --> 00:28:07.990 We'll see you then.