WEBVTT X-TIMESTAMP-MAP=LOCAL:00:00:00.000,MPEGTS:900000 00:00:00.000 --> 00:00:01.976 [MUSIC PLAYING] 00:00:17.065 --> 00:00:19.190 SPEAKER: Welcome back, everyone, to an introduction 00:00:19.190 --> 00:00:20.510 to Programming with Scratch. 00:00:20.510 --> 00:00:24.170 And so far we've seen a variety of different types of Scratch blocks. 00:00:24.170 --> 00:00:27.020 We've seen functions that perform some sort of task. 00:00:27.020 --> 00:00:30.290 We've seen values that hold information, like what direction the sprite is 00:00:30.290 --> 00:00:34.350 facing, or what position it is, or how big or how small it is, for example. 00:00:34.350 --> 00:00:38.090 And last time we took a look at conditions, the ability for our Scratch 00:00:38.090 --> 00:00:41.510 programs to ask questions and then make decisions based 00:00:41.510 --> 00:00:43.340 on the answers to those questions. 00:00:43.340 --> 00:00:45.380 But through all of this, every time we run 00:00:45.380 --> 00:00:47.450 the code, every time we press the green flag 00:00:47.450 --> 00:00:50.330 or press the space bar inside of our Scratch program, 00:00:50.330 --> 00:00:53.660 everything, every block of code, is just running one time. 00:00:53.660 --> 00:00:56.490 We're running through the code, top to bottom, one time. 00:00:56.490 --> 00:00:58.490 And if we want to see the program run again, 00:00:58.490 --> 00:01:01.700 we have to press the green flag again, or press the space bar again, 00:01:01.700 --> 00:01:05.507 or make something else to trigger the rest of the blocks to run. 00:01:05.507 --> 00:01:07.340 This time what we're going to take a look at 00:01:07.340 --> 00:01:10.070 is the ability of Scratch programs to loop, 00:01:10.070 --> 00:01:15.070 to repeat some blocks multiple times, as computers will do quite often. 00:01:15.070 --> 00:01:17.060 And so let's take a simple example. 00:01:17.060 --> 00:01:19.800 Right now we have our usual Scratch cat. 00:01:19.800 --> 00:01:22.940 And if I say, when the green flag is clicked, 00:01:22.940 --> 00:01:26.720 let's have the Scratch cat move 10 steps. 00:01:26.720 --> 00:01:28.250 This program we've seen before. 00:01:28.250 --> 00:01:31.160 When I press the green flag the cat moves. 00:01:31.160 --> 00:01:33.140 And if I want the cat to move again, I have 00:01:33.140 --> 00:01:36.890 to press the green flag again, and then move again, and then move again, 00:01:36.890 --> 00:01:39.500 and then move again, and I have to be the one controlling 00:01:39.500 --> 00:01:41.560 every single time, the cat's movement. 00:01:41.560 --> 00:01:44.240 So if I want the cat to keep moving continuously, 00:01:44.240 --> 00:01:48.770 I have to keep pressing the green flag again and again. 00:01:48.770 --> 00:01:52.760 Ideally, I'd like for the cat to be able to do all of this on its own. 00:01:52.760 --> 00:01:56.460 I'll move the cat back to its original position. 00:01:56.460 --> 00:01:58.470 And I'm going to introduce a new block now. 00:01:58.470 --> 00:02:02.370 It's also under Control, where we saw the if-thens last time, 00:02:02.370 --> 00:02:07.200 and this time the block we're going to use is this block called "forever". 00:02:07.200 --> 00:02:10.320 The forever block looks similar, in terms of its shape, 00:02:10.320 --> 00:02:11.700 to the if-then block. 00:02:11.700 --> 00:02:14.910 The forever block also has a container inside of it 00:02:14.910 --> 00:02:18.540 where I could add another stack of blocks inside of the forever, 00:02:18.540 --> 00:02:20.460 but there's no question to ask. 00:02:20.460 --> 00:02:24.450 The forever block's role is just to run the blocks of code that 00:02:24.450 --> 00:02:27.810 are inside of it and then, as this little arrow suggests, 00:02:27.810 --> 00:02:31.410 go back to the beginning of the forever block and run it all again. 00:02:31.410 --> 00:02:36.160 And then run it all again, over and over forever, until I stop the program. 00:02:36.160 --> 00:02:38.160 This is an example of what, in computer science, 00:02:38.160 --> 00:02:41.550 we would call a loop, something that allows for multiple blocks 00:02:41.550 --> 00:02:45.580 to repeat some number of times, in this case, forever. 00:02:45.580 --> 00:02:50.220 And so what I'm going to do is I'm going to take the move 10 steps block, 00:02:50.220 --> 00:02:55.570 and drag it inside of the forever block, and connect that 00:02:55.570 --> 00:02:57.790 to when the green flag is clicked. 00:02:57.790 --> 00:02:59.710 Now the behavior of this program is going 00:02:59.710 --> 00:03:03.858 to be when I press the green flag, we're going to see the cat move 10 steps, 00:03:03.858 --> 00:03:05.650 but then the loop is going to happen again. 00:03:05.650 --> 00:03:07.330 It's going to move 10 steps again, and then 00:03:07.330 --> 00:03:10.540 move 10 steps again, and then again and again, and you'll see what happens. 00:03:10.540 --> 00:03:13.970 I press the green flag, and the cat moves 00:03:13.970 --> 00:03:16.280 until it hits the edge, at which point it's stuck, 00:03:16.280 --> 00:03:19.040 and it can't move any further than that. 00:03:19.040 --> 00:03:22.280 I can drag it back to the beginning, and we can watch it go again. 00:03:22.280 --> 00:03:27.460 I'll press the flag, the cat moves, and I only had to press the flag once, 00:03:27.460 --> 00:03:31.000 and the cat went all the way to the edge of the stage. 00:03:31.000 --> 00:03:32.440 It appears to be stopping. 00:03:32.440 --> 00:03:33.700 It's not really stopping. 00:03:33.700 --> 00:03:36.220 What's happening is that it's trying to move 10 steps, 00:03:36.220 --> 00:03:38.470 but because it's already at the edge of the stage, 00:03:38.470 --> 00:03:41.320 there's nowhere left for the cat to move. 00:03:41.320 --> 00:03:45.380 And so I could fix this by adding yet another block. 00:03:45.380 --> 00:03:47.290 I stop the program by pressing the stop sign, 00:03:47.290 --> 00:03:48.970 otherwise the cat would keep moving. 00:03:48.970 --> 00:03:51.190 I'm dragging the cat back out. 00:03:51.190 --> 00:03:57.660 And under the Motion section of blocks there is this block here, 00:03:57.660 --> 00:04:00.510 "if on edge, bounce". 00:04:00.510 --> 00:04:04.240 And the idea of "if on edge, bounce" is exactly as the name might suggest. 00:04:04.240 --> 00:04:07.930 If the cat reaches the edge, it's going to bounce off the edge. 00:04:07.930 --> 00:04:11.730 It's going to turn around and face the other direction. 00:04:11.730 --> 00:04:17.329 So I'll press the green flag now, and now notice what's happening. 00:04:17.329 --> 00:04:19.279 The cat's moving continuously. 00:04:19.279 --> 00:04:21.320 I only press the green flag once, but we're 00:04:21.320 --> 00:04:24.450 running this loop where the loop is saying move 10 steps, 00:04:24.450 --> 00:04:28.185 and if it's at the edge of the stage, then bounce and go the other way. 00:04:28.185 --> 00:04:31.310 And now you might recall when we were talking about directions that sprites 00:04:31.310 --> 00:04:32.150 can be facing. 00:04:32.150 --> 00:04:35.810 By default, sprites in Scratch have all-around rotation, 00:04:35.810 --> 00:04:37.910 meaning that if you tell it to face the other way, 00:04:37.910 --> 00:04:40.202 it's going to end up upside down as if it had just been 00:04:40.202 --> 00:04:42.380 rotated to face the other direction. 00:04:42.380 --> 00:04:44.510 There is a fix to that though, and the fix to that 00:04:44.510 --> 00:04:48.320 is to change the rotation style of the cat, that we've seen a couple of times 00:04:48.320 --> 00:04:49.520 now. 00:04:49.520 --> 00:04:53.630 I can do that by going to direction here, changing the rotation 00:04:53.630 --> 00:04:56.120 from all-around, which is the first option, 00:04:56.120 --> 00:05:00.460 to just left and right, which is the second option. 00:05:00.460 --> 00:05:04.470 And now when I run this program, you'll notice the cat bounce a little bit 00:05:04.470 --> 00:05:07.380 more nicely, going back and forth forever, 00:05:07.380 --> 00:05:13.470 at least until I press the red stop sign to stop the program from running. 00:05:13.470 --> 00:05:15.840 And now if you're really paying attention to this, 00:05:15.840 --> 00:05:18.660 trying to see if it looks at all realistic, 00:05:18.660 --> 00:05:22.260 you'll notice the Scratch cat, its legs aren't moving. 00:05:22.260 --> 00:05:26.820 Its legs are just straight, and it's effectively gliding along the stage. 00:05:26.820 --> 00:05:30.880 Doesn't really look like it's walking or running across the stage, for example. 00:05:30.880 --> 00:05:33.570 And we can fix that with a little bit of animation. 00:05:33.570 --> 00:05:37.290 Animation the same way you might see a television show or a movie be animated. 00:05:37.290 --> 00:05:38.910 And how does animation work? 00:05:38.910 --> 00:05:41.177 Well, animation, when you're watching anything on TV, 00:05:41.177 --> 00:05:43.260 or watching anything on a movie or on your screen, 00:05:43.260 --> 00:05:46.890 it's really just a whole bunch of images one image after another image. 00:05:46.890 --> 00:05:49.710 And the images are moving so quickly by your eyes 00:05:49.710 --> 00:05:53.665 that your eyes just process that and think of it as actual movement. 00:05:53.665 --> 00:05:55.290 And that's what we can try and do here. 00:05:55.290 --> 00:05:58.510 Have a couple different images that look very similar to each other, 00:05:58.510 --> 00:06:01.620 but are slightly different, and by moving back and forth between them 00:06:01.620 --> 00:06:04.980 very quickly we can create the illusion of motion the same way 00:06:04.980 --> 00:06:09.170 you might do so if you're animating something like a movie. 00:06:09.170 --> 00:06:12.230 So if we go into Costumes here, you'll notice that the Scratch 00:06:12.230 --> 00:06:14.340 cat does have two costumes. 00:06:14.340 --> 00:06:17.700 It has one costume here where the legs are straight. 00:06:17.700 --> 00:06:20.150 And it has a second costume here, costume2, 00:06:20.150 --> 00:06:22.400 where the cat's legs are bent. 00:06:22.400 --> 00:06:24.620 And if we switch back and forth between them, 00:06:24.620 --> 00:06:29.240 you notice it kind of looks like the cat's walking. 00:06:29.240 --> 00:06:32.390 And so we can achieve this this way. 00:06:32.390 --> 00:06:37.650 I'll go into Looks, and I'll just add a "next costume" block 00:06:37.650 --> 00:06:39.300 into the forever loop. 00:06:39.300 --> 00:06:42.480 We're going to move 10 steps, if we're on the edge, we're going to bounce, 00:06:42.480 --> 00:06:46.670 and then we're going to switch the cat to the next costume. 00:06:46.670 --> 00:06:50.390 Now watch what happens when I press the green flag. 00:06:50.390 --> 00:06:53.790 Now the cat's legs appear to be moving. 00:06:53.790 --> 00:06:57.170 They're moving very quickly because the forever loop is happening very quickly, 00:06:57.170 --> 00:06:59.630 going very quickly through the movement, through the bouncing, 00:06:59.630 --> 00:07:01.588 and then through the switching of the costumes. 00:07:01.588 --> 00:07:04.760 But we've animated this cat just by moving it back and forth 00:07:04.760 --> 00:07:07.070 between these two different costumes. 00:07:07.070 --> 00:07:08.750 Now its legs are moving rather quickly. 00:07:08.750 --> 00:07:10.760 Maybe I want to slow this down a little bit. 00:07:10.760 --> 00:07:14.420 And I can do that just by going into Control and waiting a little bit. 00:07:14.420 --> 00:07:16.430 One second is maybe a long time to wait. 00:07:16.430 --> 00:07:20.390 Let's wait 0.1 seconds, just a fraction of a second, 00:07:20.390 --> 00:07:23.520 to slow down the cat just a little bit. 00:07:23.520 --> 00:07:26.490 And now the cat's walking in a little more of a leisurely pace. 00:07:26.490 --> 00:07:28.500 It's repeating this process over and over, 00:07:28.500 --> 00:07:31.560 moving 10 steps, if it's on the edge, it's going to bounce, 00:07:31.560 --> 00:07:35.670 switching to the next costume between whether its legs are straight or bent, 00:07:35.670 --> 00:07:38.460 and then waiting just a fraction of a second so that we 00:07:38.460 --> 00:07:40.440 can slow the cat down a little bit. 00:07:40.440 --> 00:07:43.320 And you could play around with this value determining 00:07:43.320 --> 00:07:47.280 how long you want the cat to wait for to determine how quickly 00:07:47.280 --> 00:07:51.360 or how slowly the cat's going to move across the screen. 00:07:51.360 --> 00:07:53.640 But using that loop we now have the ability 00:07:53.640 --> 00:07:57.270 to create some effect that's happening inside of our program forever, 00:07:57.270 --> 00:08:00.083 that keeps going on and on. 00:08:00.083 --> 00:08:01.500 So what's another example of this? 00:08:01.500 --> 00:08:04.320 What else could we do with this idea? 00:08:04.320 --> 00:08:06.390 Well, let's get rid of the cat for now. 00:08:06.390 --> 00:08:09.270 And let's bring back our underwater backdrop. 00:08:09.270 --> 00:08:11.010 Here's Underwater 1. 00:08:11.010 --> 00:08:17.050 And let's add the fish back into our program. 00:08:17.050 --> 00:08:18.750 Here's that fish. 00:08:18.750 --> 00:08:20.310 And let's try something else. 00:08:20.310 --> 00:08:24.390 When the green flag is clicked, I also want something to happen forever. 00:08:24.390 --> 00:08:25.840 What do I want to have happen? 00:08:25.840 --> 00:08:30.820 Well, let's go to Motion, and let's have it point in a particular direction. 00:08:30.820 --> 00:08:33.520 Now if it were always pointing in the same direction, 00:08:33.520 --> 00:08:35.700 then it wouldn't really matter if it was inside of a loop or not. 00:08:35.700 --> 00:08:38.100 It's just always going to face the right, face the right, face the right, 00:08:38.100 --> 00:08:40.049 face the right, again and again. 00:08:40.049 --> 00:08:43.140 But I could have it point in a particular direction, 00:08:43.140 --> 00:08:48.080 and I'll go ahead and choose a block like this instead. 00:08:48.080 --> 00:08:50.630 Instead of pointing in a direction with a number, 00:08:50.630 --> 00:08:53.330 let's have the fish point towards something. 00:08:53.330 --> 00:08:55.190 Let's have it point towards the mouse. 00:08:55.190 --> 00:08:58.530 It's pointing towards the mouse pointer. 00:08:58.530 --> 00:09:02.140 So now watch what happens when I press the flag. 00:09:02.140 --> 00:09:05.480 You'll notice that immediately when I press the flag the fish turned. 00:09:05.480 --> 00:09:10.600 It's now facing my cursor, which is up in the upper left corner of the stage. 00:09:10.600 --> 00:09:14.870 But watch what happens as I move the cursor. 00:09:14.870 --> 00:09:18.800 Every time I move the cursor you'll notice the fish is moving, as well. 00:09:18.800 --> 00:09:23.570 Because it's constantly, over and over forever, following this instruction 00:09:23.570 --> 00:09:26.510 to point towards the mouse pointer. 00:09:26.510 --> 00:09:31.030 So wherever the cursor goes, the fish is going to be looking in that direction 00:09:31.030 --> 00:09:33.490 because it's pointing towards the mouse pointer. 00:09:33.490 --> 00:09:37.010 Now it's looking at the cursor, though it's not really moving yet. 00:09:37.010 --> 00:09:39.310 So let's add one more thing to this program. 00:09:39.310 --> 00:09:41.800 In addition to pointing towards the mouse pointer, 00:09:41.800 --> 00:09:47.042 let's then have the fish move five steps. 00:09:47.042 --> 00:09:48.750 So now what's going to happen is wherever 00:09:48.750 --> 00:09:51.870 the cursor is, the fish is going to look at the cursor 00:09:51.870 --> 00:09:54.430 and then move a little bit closer to that cursor. 00:09:54.430 --> 00:09:57.360 So using the cursor will effectively be able to control 00:09:57.360 --> 00:10:00.010 the movement of the fish. 00:10:00.010 --> 00:10:03.660 I'll press the green flag again, and you'll notice the fish start 00:10:03.660 --> 00:10:06.450 to move after the cursor. 00:10:06.450 --> 00:10:09.840 Wherever I move the cursor, the fish is pointing in that direction 00:10:09.840 --> 00:10:12.797 and then swimming in my direction. 00:10:16.132 --> 00:10:18.090 And we can use "forever" for other things, too. 00:10:18.090 --> 00:10:21.890 Any time I want something to continue to happen during my program, 00:10:21.890 --> 00:10:24.710 a forever loop is often the way that I want to achieve that. 00:10:24.710 --> 00:10:27.350 Right now, my backdrop, I can click on the backdrop, 00:10:27.350 --> 00:10:30.770 is just showing me this underwater scene. 00:10:30.770 --> 00:10:33.110 But I can add some blocks to the backdrop, 00:10:33.110 --> 00:10:35.270 as well, telling the backdrop what to do. 00:10:35.270 --> 00:10:38.930 And maybe I'll have the backdrop say, when the green flag is clicked, 00:10:38.930 --> 00:10:42.735 let's forever play a sound. 00:10:45.400 --> 00:10:48.960 The default sound is "pop", but I can change those sounds. 00:10:48.960 --> 00:10:52.870 Let's find a sound that's appropriate for being underwater. 00:10:52.870 --> 00:10:57.310 And if I scroll down through this long list of sounds, I can try them out, 00:10:57.310 --> 00:11:01.736 but I think there's one called-- 00:11:01.736 --> 00:11:04.030 Let's search for water maybe? 00:11:04.030 --> 00:11:04.757 "Ocean Wave". 00:11:04.757 --> 00:11:06.340 Here's "Ocean Wave", related to water. 00:11:09.840 --> 00:11:11.670 I can sample hear what that sounds like. 00:11:11.670 --> 00:11:13.570 I like that one. 00:11:13.570 --> 00:11:18.250 And so let's have the backdrop play this "Ocean Wave" sound forever. 00:11:18.250 --> 00:11:21.740 It's going to play the sound until it's done, and then play it again. 00:11:21.740 --> 00:11:25.690 And so now when I run the program, notice what you see and hear. 00:11:33.480 --> 00:11:36.570 I have the fish following the cursor, and I just stopped the program. 00:11:36.570 --> 00:11:39.990 But because I had multiple forever loops, I had a loop on the backdrop, 00:11:39.990 --> 00:11:44.108 as well, we were just playing this "Ocean Wave" sound over and over again. 00:11:44.108 --> 00:11:46.650 And you could use this if you wanted a sound to play forever. 00:11:46.650 --> 00:11:48.567 You could add music to your programs, as well. 00:11:48.567 --> 00:11:52.080 If you wanted music to be playing in the background of your Scratch program, 00:11:52.080 --> 00:11:55.060 you might have that music sound play until it's done, 00:11:55.060 --> 00:11:58.350 and then the song starts right back up again as soon as the song is over. 00:11:58.350 --> 00:12:02.070 So you can continue to have music in the background of your Scratch program 00:12:02.070 --> 00:12:04.170 going over and over forever. 00:12:04.170 --> 00:12:06.660 So these forever loops can be quite useful for helping 00:12:06.660 --> 00:12:10.590 to create this sense of continuity, this continued 00:12:10.590 --> 00:12:16.770 presence of sounds or movement that are happening inside of our applications. 00:12:16.770 --> 00:12:20.630 And so let's try now to create another program. 00:12:20.630 --> 00:12:24.860 I'll get rid of the fish, and we'll get rid of the underwater backdrop. 00:12:24.860 --> 00:12:28.160 Let's go back to this plain backdrop. 00:12:28.160 --> 00:12:30.760 And I'll get rid of this code, too. 00:12:30.760 --> 00:12:32.140 Let's bring back the cat. 00:12:34.870 --> 00:12:38.620 And what I want the cat to do now, just for fun, 00:12:38.620 --> 00:12:41.290 is to not let me get too close to the cat. 00:12:41.290 --> 00:12:44.380 If my cursor gets too close to the cat, I would like for the cat 00:12:44.380 --> 00:12:47.380 to meow or something, or otherwise indicate that it doesn't 00:12:47.380 --> 00:12:50.330 want me to get too close, for example. 00:12:50.330 --> 00:12:52.130 And so how might this work? 00:12:52.130 --> 00:12:53.260 Well, let's try this. 00:12:53.260 --> 00:12:55.300 When the green flag is clicked, my instinct 00:12:55.300 --> 00:12:57.220 might be to say, well, if I want to check 00:12:57.220 --> 00:13:01.420 to make sure that I'm not too close to the cat, that sounds like a condition. 00:13:01.420 --> 00:13:02.710 It sounds like an if-then. 00:13:02.710 --> 00:13:05.710 If I'm getting too close to the cat, then have the cat meow 00:13:05.710 --> 00:13:07.760 because it's unhappy, for example. 00:13:07.760 --> 00:13:11.040 And so I might add an if-then. 00:13:11.040 --> 00:13:13.080 And here I can use an operator. 00:13:13.080 --> 00:13:15.180 I can use the less than operator, because I 00:13:15.180 --> 00:13:19.230 want to check if the distance between the cat and the mouse pointer 00:13:19.230 --> 00:13:22.230 is less than some value, we'll say 100. 00:13:22.230 --> 00:13:23.550 And how do I find the distance? 00:13:23.550 --> 00:13:25.410 That's under the Sensing category. 00:13:25.410 --> 00:13:29.220 It turns out there's a block here called "distance to mouse pointer", 00:13:29.220 --> 00:13:29.970 so I can check. 00:13:29.970 --> 00:13:35.000 Is the distance between the cat and the mouse less than 100? 00:13:35.000 --> 00:13:42.630 And if so, let's go ahead and play the meow sound until it's done. 00:13:42.630 --> 00:13:45.950 So the idea of this code now, what I would like for this code to do, 00:13:45.950 --> 00:13:51.260 is to say if I'm less than 100 spaces away from the cat, 00:13:51.260 --> 00:13:53.480 then go ahead and play the meow sound. 00:13:53.480 --> 00:13:55.760 And that's what I want the cat to do, but watch what 00:13:55.760 --> 00:13:58.880 happens if I try and run this program. 00:13:58.880 --> 00:14:00.200 I'll run the program. 00:14:00.200 --> 00:14:04.890 And my cursor starts out pretty far away from the cat, but as I bring the cursor 00:14:04.890 --> 00:14:08.800 closer nothing seems to be happening. 00:14:08.800 --> 00:14:11.980 I'm pretty close to the cat right now, I'm even touching the cat now, 00:14:11.980 --> 00:14:15.640 and yet still, I'm not hearing the cat meow. 00:14:15.640 --> 00:14:16.360 Why is that? 00:14:16.360 --> 00:14:18.430 What's going on? 00:14:18.430 --> 00:14:20.530 Well, it has to do with the fact that by default, 00:14:20.530 --> 00:14:22.900 when you're running code inside of a program, 00:14:22.900 --> 00:14:25.810 that code is only going to run once. 00:14:25.810 --> 00:14:27.730 What happened when I pressed the green flag? 00:14:27.730 --> 00:14:31.870 When I took my cursor, and I dragged my cursor over the green flag right there, 00:14:31.870 --> 00:14:34.810 and then I clicked on it, what was happening inside of the program? 00:14:34.810 --> 00:14:37.240 Well, immediately, Scratch checks. 00:14:37.240 --> 00:14:38.560 It checks this question. 00:14:38.560 --> 00:14:41.680 Is the distance to the mouse pointer less than 100? 00:14:41.680 --> 00:14:42.430 Well, no it's not. 00:14:42.430 --> 00:14:46.810 This is more than 100 spaces in the world of Scatch's coordinate system, 00:14:46.810 --> 00:14:48.740 so we didn't play the meow sound. 00:14:48.740 --> 00:14:51.880 And at that point, this stack of blocks was over. 00:14:51.880 --> 00:14:55.150 Nothing else happened because we ran through all of these blocks 00:14:55.150 --> 00:14:59.320 from top to bottom, and that was the end of the stack of blocks. 00:14:59.320 --> 00:15:02.950 What I really wanted to have happen is for this check for this question 00:15:02.950 --> 00:15:05.590 to be asked not once, but for the question 00:15:05.590 --> 00:15:08.020 to constantly be asked all the time. 00:15:08.020 --> 00:15:10.660 I want the cat to be checking, is the distance 00:15:10.660 --> 00:15:12.640 of the mouse pointer less than 100? 00:15:12.640 --> 00:15:15.370 And any time it turns out that I get to close, 00:15:15.370 --> 00:15:18.100 then I want for the cat to play the meow sound. 00:15:18.100 --> 00:15:21.050 And that's another case where a forever loop is quite helpful. 00:15:21.050 --> 00:15:23.620 I don't just want to ask this question one time. 00:15:23.620 --> 00:15:28.100 I want to ask the question forever as long as this program's running. 00:15:28.100 --> 00:15:31.120 And so I can go into Control, grab a forever block 00:15:31.120 --> 00:15:34.420 and put it around the condition just like that. 00:15:34.420 --> 00:15:37.720 And now this question, am I getting too close to the cat, 00:15:37.720 --> 00:15:40.120 it's going to be asked not once, but over and over again. 00:15:40.120 --> 00:15:43.550 Every time we ask the question, we'll then ask it again. 00:15:43.550 --> 00:15:44.950 I'll click on the flag. 00:15:44.950 --> 00:15:46.070 The program's now started. 00:15:46.070 --> 00:15:48.243 We still don't hear anything, but now watch 00:15:48.243 --> 00:15:50.410 what happens as my cursor gets too close to the cat. 00:15:55.920 --> 00:15:57.810 So it's further away, we hear nothing. 00:15:57.810 --> 00:16:03.540 But when I get too close, then you start to hear the cat meow. 00:16:03.540 --> 00:16:07.920 And you hear that happen because this condition is now inside of a loop. 00:16:07.920 --> 00:16:10.350 It's repeating over and over again forever, 00:16:10.350 --> 00:16:15.620 checking to see if the distance is less than 100. 00:16:15.620 --> 00:16:17.470 And so that then is a forever loop, a loop 00:16:17.470 --> 00:16:20.110 that's going to run some code over and over again, forever, 00:16:20.110 --> 00:16:21.357 until the end of the program. 00:16:21.357 --> 00:16:24.190 And you'll often hear this also called an infinite loop, a loop that 00:16:24.190 --> 00:16:26.990 repeats infinitely again and again. 00:16:26.990 --> 00:16:29.050 But when we repeat code inside of our programs 00:16:29.050 --> 00:16:31.880 we don't necessarily always want for it to happen infinitely. 00:16:31.880 --> 00:16:34.810 We don't always want for code to run forever. 00:16:34.810 --> 00:16:38.740 Sometimes we do want the code to repeat, but not infinitely, many times. 00:16:38.740 --> 00:16:42.680 Just five times, or 10 times, or 20 times, for example. 00:16:42.680 --> 00:16:45.400 And so how could we do that instead? 00:16:45.400 --> 00:16:47.620 Well, for that we're going to need another block. 00:16:47.620 --> 00:16:53.330 Not the forever block, which I'll get rid of now, but this block here. 00:16:53.330 --> 00:16:57.310 This block is "repeat" and then 10, and this is also a loop. 00:16:57.310 --> 00:16:59.060 It's going to take some code and repeat it 00:16:59.060 --> 00:17:03.020 multiple times, which is implied by this arrow here at the bottom right. 00:17:03.020 --> 00:17:07.640 But we get to specify, by typing in here, how many times I 00:17:07.640 --> 00:17:10.287 would like for that code to repeat. 00:17:10.287 --> 00:17:11.579 And so when can this be useful? 00:17:11.579 --> 00:17:14.180 When might I want to use a repeat block? 00:17:14.180 --> 00:17:18.060 Well, imagine I wanted the cat to meow three times, for example. 00:17:18.060 --> 00:17:21.230 I could go to Sounds, and just play sound meow until done, 00:17:21.230 --> 00:17:24.740 and then play sound meow until done, and then play sound meow until done. 00:17:24.740 --> 00:17:31.640 And now when I press the flag I do hear three meows, 00:17:31.640 --> 00:17:33.830 but this is repetitive code. 00:17:33.830 --> 00:17:37.340 I find myself repeating the same blocks again and again and again. 00:17:37.340 --> 00:17:42.500 And I could arguably improve the design of my program by using a loop instead. 00:17:42.500 --> 00:17:46.370 Instead of repeating myself again and again, I'll just have a loop. 00:17:46.370 --> 00:17:49.160 Not repeat 10, but repeat three. 00:17:49.160 --> 00:17:51.650 And what am I repeating three times? 00:17:51.650 --> 00:17:54.560 Let's play the sound meow until done. 00:17:54.560 --> 00:18:00.870 Now I press the flag, and it still meows three times, 00:18:00.870 --> 00:18:03.420 but this is better than the program I had before. 00:18:03.420 --> 00:18:05.320 It's better for a couple of reasons. 00:18:05.320 --> 00:18:08.070 One, I'm being more efficient with my use of blocks. 00:18:08.070 --> 00:18:11.310 Before, I needed three blocks to say meow three times. 00:18:11.310 --> 00:18:13.830 Now I just have one block that says meow, 00:18:13.830 --> 00:18:16.540 plus one block that's handling the repeating. 00:18:16.540 --> 00:18:18.900 So I'm being more efficient with my use of blocks. 00:18:18.900 --> 00:18:21.600 And also, it's going to be easier later for me 00:18:21.600 --> 00:18:25.110 to modify this program if I want to change the way it behaves. 00:18:25.110 --> 00:18:29.670 Say that I later wanted this to say meow not three times, but 30 times. 00:18:29.670 --> 00:18:32.010 In my old version I would have to just drag out 00:18:32.010 --> 00:18:34.890 an additional 27 additional play meow blocks 00:18:34.890 --> 00:18:36.750 until we were able to get to that point. 00:18:36.750 --> 00:18:40.560 Now I just change the number that appears inside of that oval. 00:18:40.560 --> 00:18:43.860 And by changing that value I change the number of times 00:18:43.860 --> 00:18:46.290 that the cat is going to meow. 00:18:46.290 --> 00:18:49.110 And this doesn't just have to be a number that I type in. 00:18:49.110 --> 00:18:52.360 I can drag in any value into that oval, as well, 00:18:52.360 --> 00:18:55.740 which means I could do something like ask the user how many times 00:18:55.740 --> 00:18:58.450 the user wants something to happen. 00:18:58.450 --> 00:19:00.010 So let's give that a try. 00:19:00.010 --> 00:19:04.300 I can go into the Sensing category of blocks and ask a question. 00:19:04.300 --> 00:19:07.810 The question I'll ask is, type in a number. 00:19:07.810 --> 00:19:13.780 And then instead of repeating three times, let's repeat answer times. 00:19:13.780 --> 00:19:17.650 Whatever the answer is that's how many times I want the cat to meow. 00:19:17.650 --> 00:19:20.150 I press the flag and I type 3, for example. 00:19:20.150 --> 00:19:25.270 And then you'll hear three meows. 00:19:25.270 --> 00:19:35.220 But if I press the flag and this time try 5, for example, 00:19:35.220 --> 00:19:36.820 then you hear it five times. 00:19:36.820 --> 00:19:39.600 So the user controls how many times this happens 00:19:39.600 --> 00:19:43.770 because the answer block I've plugged into that open space 00:19:43.770 --> 00:19:46.372 inside of the repeat block. 00:19:46.372 --> 00:19:47.830 And so the repeat block is helpful. 00:19:47.830 --> 00:19:50.970 Now, any time I want something not to happen forever, 00:19:50.970 --> 00:19:53.790 but still to happen multiple times. 00:19:53.790 --> 00:19:55.390 What's another example of that? 00:19:55.390 --> 00:19:57.840 Well, way back when we first introduced motion 00:19:57.840 --> 00:20:00.750 I showed you how you can get the cat to move in a circle. 00:20:00.750 --> 00:20:05.220 I said you could do something like move, let's say, 30 steps. 00:20:05.220 --> 00:20:08.370 And then turn 15 degrees, just turn a little bit. 00:20:08.370 --> 00:20:10.920 And that had the effect, if I bring the cat up to the top, 00:20:10.920 --> 00:20:14.430 where whenever I press the flag you'll see the cat move 00:20:14.430 --> 00:20:19.620 and rotate a little bit, and the cat moves in a circle. 00:20:19.620 --> 00:20:24.840 But in order for that to work I had to keep pressing the green flag every time 00:20:24.840 --> 00:20:26.960 I want the cat to move. 00:20:26.960 --> 00:20:30.950 And every time it moved 30 steps and then turned 15 degrees. 00:20:30.950 --> 00:20:33.560 This is a great opportunity to use a loop so that I don't 00:20:33.560 --> 00:20:35.120 have to keep doing all the clicking. 00:20:35.120 --> 00:20:38.630 The loop will just take care of repeating this process again 00:20:38.630 --> 00:20:39.710 and again and again. 00:20:39.710 --> 00:20:43.130 So I can go to Control, repeat a certain number of times 00:20:43.130 --> 00:20:47.190 and, I counted, to get all the way around I had to click 24 times. 00:20:47.190 --> 00:20:50.270 So go ahead and repeat this code 24 times 00:20:50.270 --> 00:20:54.230 where now when I press the green flag, you'll see the cat 00:20:54.230 --> 00:20:57.120 move all the way around in a circle. 00:20:57.120 --> 00:20:59.480 Very quickly, it's moving all the way around in a circle 00:20:59.480 --> 00:21:01.010 every time I click on the flag. 00:21:01.010 --> 00:21:03.110 And the reason for that is because it's taking 00:21:03.110 --> 00:21:07.310 every small movement, that movement of just moving 30 steps and then rotating, 00:21:07.310 --> 00:21:09.560 and it's repeating that again and again and again very 00:21:09.560 --> 00:21:13.790 quickly, faster than I could ever click on buttons on the Scratch interface. 00:21:13.790 --> 00:21:16.580 And the effect of that is that you're able to see this cat now 00:21:16.580 --> 00:21:20.890 move in a circle, as well. 00:21:20.890 --> 00:21:24.840 And so with that in mind, let's revisit one final program 00:21:24.840 --> 00:21:26.040 that we've seen before. 00:21:26.040 --> 00:21:28.830 And that's the balloon program we've seen a couple of times now. 00:21:28.830 --> 00:21:31.170 Where before, last time we saw the balloon, 00:21:31.170 --> 00:21:33.900 I was pressing the space bar to try and inflate 00:21:33.900 --> 00:21:36.960 the balloon until eventually it popped. 00:21:36.960 --> 00:21:40.740 But now that we have these loops to repeat something again and again, 00:21:40.740 --> 00:21:43.500 I can have the balloon inflate itself without me 00:21:43.500 --> 00:21:47.900 having to press buttons again and again to get the balloon to inflate. 00:21:47.900 --> 00:21:49.590 I'll center the balloon. 00:21:49.590 --> 00:21:51.820 And let's try this. 00:21:51.820 --> 00:21:55.920 When the flag is clicked, let's go ahead and repeat 00:21:55.920 --> 00:22:00.230 something some number of times. 00:22:00.230 --> 00:22:03.670 And what I want to do is I want to change the size 00:22:03.670 --> 00:22:05.740 by 10, which is what we did before. 00:22:05.740 --> 00:22:08.830 And then just to make this happen a little more slowly, let's 00:22:08.830 --> 00:22:14.870 wait, not a full second, but maybe 0.2 seconds, just a fraction of a second. 00:22:14.870 --> 00:22:17.620 So it's going to grow a little then wait, grow a little 00:22:17.620 --> 00:22:20.110 and then wait, and then after we repeat 10 times, 00:22:20.110 --> 00:22:27.250 after we're done with all of that looping, let's hide the balloon. 00:22:27.250 --> 00:22:29.380 And we'll play a sound. 00:22:29.380 --> 00:22:32.750 We'll play this pop sound. 00:22:32.750 --> 00:22:35.630 So now I'll press the flag. 00:22:35.630 --> 00:22:41.048 It gets bigger and bigger, and then it disappears with a pop. 00:22:41.048 --> 00:22:41.840 Let's try it again. 00:22:41.840 --> 00:22:44.380 I'll press the flag. 00:22:44.380 --> 00:22:45.610 OK, nothing happened. 00:22:45.610 --> 00:22:49.750 The balloon didn't come back, which is a little bit strange. 00:22:49.750 --> 00:22:52.720 But if you pay attention to my code, you'll see why that's happening. 00:22:52.720 --> 00:22:56.860 I repeated 10 times this process of change the size by 10 00:22:56.860 --> 00:22:58.820 then wait 0.2 seconds. 00:22:58.820 --> 00:23:02.620 And then after that repeated we decided to hide the balloon 00:23:02.620 --> 00:23:04.510 and play the pop sound. 00:23:04.510 --> 00:23:07.450 But because we use this hide block to hide the balloon, 00:23:07.450 --> 00:23:11.630 the balloon's disappeared, and then it never really comes back. 00:23:11.630 --> 00:23:15.280 So if ever I want something to reset whenever I start the program again, 00:23:15.280 --> 00:23:19.600 I often need to add blocks immediately after the green flag is clicked to say, 00:23:19.600 --> 00:23:23.170 when you press the green flag let's be sure to show the balloon. 00:23:23.170 --> 00:23:26.770 Because otherwise, the balloon is going to stay hidden from the last time 00:23:26.770 --> 00:23:29.720 I hid the balloon when I ran the program. 00:23:29.720 --> 00:23:32.410 And so I can use the "show" block for that. 00:23:32.410 --> 00:23:38.480 Going into Looks, dragging out this block that says "show", 00:23:38.480 --> 00:23:45.640 and because I've been changing the size I better change the size back to 100%. 00:23:45.640 --> 00:23:47.890 So now I see the balloon. 00:23:47.890 --> 00:23:50.420 It gets bigger and then it disappears. 00:23:50.420 --> 00:23:55.030 And I can press the green flag, and I see the same thing happen again. 00:23:55.030 --> 00:23:57.820 Every time I click the green flag, we show the balloon again, 00:23:57.820 --> 00:24:01.750 set its size back to 100%, then we let it repeat, growing and growing 00:24:01.750 --> 00:24:05.410 and growing before we hide the balloon and play the sound "pop" 00:24:05.410 --> 00:24:08.320 until that sound is done. 00:24:08.320 --> 00:24:12.170 And this allows me to repeat the process of growing the balloon. 00:24:12.170 --> 00:24:14.230 And to what size does it grow? 00:24:14.230 --> 00:24:17.350 Well, we're changing the size by 10 every time, 00:24:17.350 --> 00:24:20.013 and we're repeating that process 10 times, 00:24:20.013 --> 00:24:22.180 so I could do the math in my head and say, OK, we're 00:24:22.180 --> 00:24:24.220 going to get up to a size of 200. 00:24:24.220 --> 00:24:26.110 What if I wanted the size to be bigger? 00:24:26.110 --> 00:24:31.000 What if I wanted to let the balloon grow up to size 250, for example? 00:24:31.000 --> 00:24:34.720 Well, I could do the math again and say, all right, we're starting at size 100. 00:24:34.720 --> 00:24:36.970 I want to get up to 250. 00:24:36.970 --> 00:24:39.700 And if we're changing the size by 10 every time, 00:24:39.700 --> 00:24:43.240 then I'm going to calculate that's 15 repetitions to get it up there. 00:24:43.240 --> 00:24:44.050 And I can try that. 00:24:47.750 --> 00:24:52.220 And yeah, it does seem to get to 250, but I had to do a lot of math on my own 00:24:52.220 --> 00:24:54.860 to figure that out, to figure out, OK, 15, that's 00:24:54.860 --> 00:24:57.890 the number of times I would like for this code to repeat. 00:24:57.890 --> 00:25:00.620 There is one other type of loop that I'll introduce finally 00:25:00.620 --> 00:25:03.440 as one final block for today that can sometimes 00:25:03.440 --> 00:25:05.330 make our logic a little bit easier. 00:25:05.330 --> 00:25:08.750 And that's this block here, "repeat until", 00:25:08.750 --> 00:25:14.090 and this block effectively tries to combine a loop and a condition. 00:25:14.090 --> 00:25:16.850 We're going to repeat some blocks, not a fixed number of times, 00:25:16.850 --> 00:25:22.850 not five times, 10 times, 20 times, but until some question is answered true. 00:25:22.850 --> 00:25:26.600 And notice this hexagon-shaped opening is the same as the shape that we saw 00:25:26.600 --> 00:25:29.820 in those "if" blocks and the "if-else" blocks, for example. 00:25:29.820 --> 00:25:32.750 So what this is going to do is repeat some code 00:25:32.750 --> 00:25:36.660 until the answer to some question is true. 00:25:36.660 --> 00:25:41.480 So if I want the balloon to keep growing until it gets to a size of 250, 00:25:41.480 --> 00:25:44.630 then I don't have to do the math and say, OK, that's going to be 15. 00:25:44.630 --> 00:25:49.280 I can just say repeat until, drag the equals sign out, 00:25:49.280 --> 00:25:58.950 and I'm going to repeat until the size is equal to 250. 00:25:58.950 --> 00:26:02.200 And now I'll take this code that was in the old loop, bring it into the repeat 00:26:02.200 --> 00:26:06.660 until block, get rid of my old repeat. 00:26:06.660 --> 00:26:09.170 And this is my new code. 00:26:09.170 --> 00:26:12.680 I show the balloon, set its size to 100%, 00:26:12.680 --> 00:26:15.470 we're going to keep repeating until the size is 250. 00:26:15.470 --> 00:26:18.620 I don't know in advance necessarily how long that's going to take, 00:26:18.620 --> 00:26:21.950 but we're going to just keep repeating the code until that's true. 00:26:21.950 --> 00:26:26.300 We're going to change the size, wait 0.2 seconds, and then at the very end 00:26:26.300 --> 00:26:30.390 hide our balloon and play that pop sound. 00:26:30.390 --> 00:26:37.660 And so I can press the flag, and you see the balloon grow and grow, 00:26:37.660 --> 00:26:42.080 and once it reaches 250, at that point the balloon disappears. 00:26:42.080 --> 00:26:45.890 So three different types of loops that we've been able to explore today. 00:26:45.890 --> 00:26:49.510 One is the forever loop, that takes some blocks and repeats them over and over, 00:26:49.510 --> 00:26:51.940 infinitely, until we stop the program. 00:26:51.940 --> 00:26:55.150 One is the repeat block, which repeats a fixed number of times. 00:26:55.150 --> 00:26:57.910 I can say do this five times or 10 times or 20 times. 00:26:57.910 --> 00:27:01.987 And then, finally, the repeat until block that combines some of the ideas 00:27:01.987 --> 00:27:04.570 that we've been taking a look at today, looking at the ability 00:27:04.570 --> 00:27:08.470 to repeat until a particular question is answered true. 00:27:08.470 --> 00:27:10.480 And using those loops, we have the ability 00:27:10.480 --> 00:27:12.730 to make our programs even more interesting. 00:27:12.730 --> 00:27:15.010 Rather than have to click a button multiple times 00:27:15.010 --> 00:27:17.080 and repeat ourselves with code multiple times, 00:27:17.080 --> 00:27:21.537 we can let the code repeat itself just via the use of these loops. 00:27:21.537 --> 00:27:24.370 That's it for an introduction to Programming with Scratch for today. 00:27:24.370 --> 00:27:26.995 We have a couple more opportunities to take a look at what else 00:27:26.995 --> 00:27:28.150 we can do with Scratch. 00:27:28.150 --> 00:27:30.450 So we'll see you next time.