1 00:00:00,000 --> 00:00:01,976 [MUSIC PLAYING] 2 00:00:01,976 --> 00:00:17,065 3 00:00:17,065 --> 00:00:19,190 SPEAKER: Welcome back, everyone, to an introduction 4 00:00:19,190 --> 00:00:20,510 to Programming with Scratch. 5 00:00:20,510 --> 00:00:24,170 And so far we've seen a variety of different types of Scratch blocks. 6 00:00:24,170 --> 00:00:27,020 We've seen functions that perform some sort of task. 7 00:00:27,020 --> 00:00:30,290 We've seen values that hold information, like what direction the sprite is 8 00:00:30,290 --> 00:00:34,350 facing, or what position it is, or how big or how small it is, for example. 9 00:00:34,350 --> 00:00:38,090 And last time we took a look at conditions, the ability for our Scratch 10 00:00:38,090 --> 00:00:41,510 programs to ask questions and then make decisions based 11 00:00:41,510 --> 00:00:43,340 on the answers to those questions. 12 00:00:43,340 --> 00:00:45,380 But through all of this, every time we run 13 00:00:45,380 --> 00:00:47,450 the code, every time we press the green flag 14 00:00:47,450 --> 00:00:50,330 or press the space bar inside of our Scratch program, 15 00:00:50,330 --> 00:00:53,660 everything, every block of code, is just running one time. 16 00:00:53,660 --> 00:00:56,490 We're running through the code, top to bottom, one time. 17 00:00:56,490 --> 00:00:58,490 And if we want to see the program run again, 18 00:00:58,490 --> 00:01:01,700 we have to press the green flag again, or press the space bar again, 19 00:01:01,700 --> 00:01:05,507 or make something else to trigger the rest of the blocks to run. 20 00:01:05,507 --> 00:01:07,340 This time what we're going to take a look at 21 00:01:07,340 --> 00:01:10,070 is the ability of Scratch programs to loop, 22 00:01:10,070 --> 00:01:15,070 to repeat some blocks multiple times, as computers will do quite often. 23 00:01:15,070 --> 00:01:17,060 And so let's take a simple example. 24 00:01:17,060 --> 00:01:19,800 Right now we have our usual Scratch cat. 25 00:01:19,800 --> 00:01:22,940 And if I say, when the green flag is clicked, 26 00:01:22,940 --> 00:01:26,720 let's have the Scratch cat move 10 steps. 27 00:01:26,720 --> 00:01:28,250 This program we've seen before. 28 00:01:28,250 --> 00:01:31,160 When I press the green flag the cat moves. 29 00:01:31,160 --> 00:01:33,140 And if I want the cat to move again, I have 30 00:01:33,140 --> 00:01:36,890 to press the green flag again, and then move again, and then move again, 31 00:01:36,890 --> 00:01:39,500 and then move again, and I have to be the one controlling 32 00:01:39,500 --> 00:01:41,560 every single time, the cat's movement. 33 00:01:41,560 --> 00:01:44,240 So if I want the cat to keep moving continuously, 34 00:01:44,240 --> 00:01:48,770 I have to keep pressing the green flag again and again. 35 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. 36 00:01:52,760 --> 00:01:56,460 I'll move the cat back to its original position. 37 00:01:56,460 --> 00:01:58,470 And I'm going to introduce a new block now. 38 00:01:58,470 --> 00:02:02,370 It's also under Control, where we saw the if-thens last time, 39 00:02:02,370 --> 00:02:07,200 and this time the block we're going to use is this block called "forever". 40 00:02:07,200 --> 00:02:10,320 The forever block looks similar, in terms of its shape, 41 00:02:10,320 --> 00:02:11,700 to the if-then block. 42 00:02:11,700 --> 00:02:14,910 The forever block also has a container inside of it 43 00:02:14,910 --> 00:02:18,540 where I could add another stack of blocks inside of the forever, 44 00:02:18,540 --> 00:02:20,460 but there's no question to ask. 45 00:02:20,460 --> 00:02:24,450 The forever block's role is just to run the blocks of code that 46 00:02:24,450 --> 00:02:27,810 are inside of it and then, as this little arrow suggests, 47 00:02:27,810 --> 00:02:31,410 go back to the beginning of the forever block and run it all again. 48 00:02:31,410 --> 00:02:36,160 And then run it all again, over and over forever, until I stop the program. 49 00:02:36,160 --> 00:02:38,160 This is an example of what, in computer science, 50 00:02:38,160 --> 00:02:41,550 we would call a loop, something that allows for multiple blocks 51 00:02:41,550 --> 00:02:45,580 to repeat some number of times, in this case, forever. 52 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, 53 00:02:50,220 --> 00:02:55,570 and drag it inside of the forever block, and connect that 54 00:02:55,570 --> 00:02:57,790 to when the green flag is clicked. 55 00:02:57,790 --> 00:02:59,710 Now the behavior of this program is going 56 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, 57 00:03:03,858 --> 00:03:05,650 but then the loop is going to happen again. 58 00:03:05,650 --> 00:03:07,330 It's going to move 10 steps again, and then 59 00:03:07,330 --> 00:03:10,540 move 10 steps again, and then again and again, and you'll see what happens. 60 00:03:10,540 --> 00:03:13,970 I press the green flag, and the cat moves 61 00:03:13,970 --> 00:03:16,280 until it hits the edge, at which point it's stuck, 62 00:03:16,280 --> 00:03:19,040 and it can't move any further than that. 63 00:03:19,040 --> 00:03:22,280 I can drag it back to the beginning, and we can watch it go again. 64 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, 65 00:03:27,460 --> 00:03:31,000 and the cat went all the way to the edge of the stage. 66 00:03:31,000 --> 00:03:32,440 It appears to be stopping. 67 00:03:32,440 --> 00:03:33,700 It's not really stopping. 68 00:03:33,700 --> 00:03:36,220 What's happening is that it's trying to move 10 steps, 69 00:03:36,220 --> 00:03:38,470 but because it's already at the edge of the stage, 70 00:03:38,470 --> 00:03:41,320 there's nowhere left for the cat to move. 71 00:03:41,320 --> 00:03:45,380 And so I could fix this by adding yet another block. 72 00:03:45,380 --> 00:03:47,290 I stop the program by pressing the stop sign, 73 00:03:47,290 --> 00:03:48,970 otherwise the cat would keep moving. 74 00:03:48,970 --> 00:03:51,190 I'm dragging the cat back out. 75 00:03:51,190 --> 00:03:57,660 And under the Motion section of blocks there is this block here, 76 00:03:57,660 --> 00:04:00,510 "if on edge, bounce". 77 00:04:00,510 --> 00:04:04,240 And the idea of "if on edge, bounce" is exactly as the name might suggest. 78 00:04:04,240 --> 00:04:07,930 If the cat reaches the edge, it's going to bounce off the edge. 79 00:04:07,930 --> 00:04:11,730 It's going to turn around and face the other direction. 80 00:04:11,730 --> 00:04:17,329 So I'll press the green flag now, and now notice what's happening. 81 00:04:17,329 --> 00:04:19,279 The cat's moving continuously. 82 00:04:19,279 --> 00:04:21,320 I only press the green flag once, but we're 83 00:04:21,320 --> 00:04:24,450 running this loop where the loop is saying move 10 steps, 84 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. 85 00:04:28,185 --> 00:04:31,310 And now you might recall when we were talking about directions that sprites 86 00:04:31,310 --> 00:04:32,150 can be facing. 87 00:04:32,150 --> 00:04:35,810 By default, sprites in Scratch have all-around rotation, 88 00:04:35,810 --> 00:04:37,910 meaning that if you tell it to face the other way, 89 00:04:37,910 --> 00:04:40,202 it's going to end up upside down as if it had just been 90 00:04:40,202 --> 00:04:42,380 rotated to face the other direction. 91 00:04:42,380 --> 00:04:44,510 There is a fix to that though, and the fix to that 92 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 93 00:04:48,320 --> 00:04:49,520 now. 94 00:04:49,520 --> 00:04:53,630 I can do that by going to direction here, changing the rotation 95 00:04:53,630 --> 00:04:56,120 from all-around, which is the first option, 96 00:04:56,120 --> 00:05:00,460 to just left and right, which is the second option. 97 00:05:00,460 --> 00:05:04,470 And now when I run this program, you'll notice the cat bounce a little bit 98 00:05:04,470 --> 00:05:07,380 more nicely, going back and forth forever, 99 00:05:07,380 --> 00:05:13,470 at least until I press the red stop sign to stop the program from running. 100 00:05:13,470 --> 00:05:15,840 And now if you're really paying attention to this, 101 00:05:15,840 --> 00:05:18,660 trying to see if it looks at all realistic, 102 00:05:18,660 --> 00:05:22,260 you'll notice the Scratch cat, its legs aren't moving. 103 00:05:22,260 --> 00:05:26,820 Its legs are just straight, and it's effectively gliding along the stage. 104 00:05:26,820 --> 00:05:30,880 Doesn't really look like it's walking or running across the stage, for example. 105 00:05:30,880 --> 00:05:33,570 And we can fix that with a little bit of animation. 106 00:05:33,570 --> 00:05:37,290 Animation the same way you might see a television show or a movie be animated. 107 00:05:37,290 --> 00:05:38,910 And how does animation work? 108 00:05:38,910 --> 00:05:41,177 Well, animation, when you're watching anything on TV, 109 00:05:41,177 --> 00:05:43,260 or watching anything on a movie or on your screen, 110 00:05:43,260 --> 00:05:46,890 it's really just a whole bunch of images one image after another image. 111 00:05:46,890 --> 00:05:49,710 And the images are moving so quickly by your eyes 112 00:05:49,710 --> 00:05:53,665 that your eyes just process that and think of it as actual movement. 113 00:05:53,665 --> 00:05:55,290 And that's what we can try and do here. 114 00:05:55,290 --> 00:05:58,510 Have a couple different images that look very similar to each other, 115 00:05:58,510 --> 00:06:01,620 but are slightly different, and by moving back and forth between them 116 00:06:01,620 --> 00:06:04,980 very quickly we can create the illusion of motion the same way 117 00:06:04,980 --> 00:06:09,170 you might do so if you're animating something like a movie. 118 00:06:09,170 --> 00:06:12,230 So if we go into Costumes here, you'll notice that the Scratch 119 00:06:12,230 --> 00:06:14,340 cat does have two costumes. 120 00:06:14,340 --> 00:06:17,700 It has one costume here where the legs are straight. 121 00:06:17,700 --> 00:06:20,150 And it has a second costume here, costume2, 122 00:06:20,150 --> 00:06:22,400 where the cat's legs are bent. 123 00:06:22,400 --> 00:06:24,620 And if we switch back and forth between them, 124 00:06:24,620 --> 00:06:29,240 you notice it kind of looks like the cat's walking. 125 00:06:29,240 --> 00:06:32,390 And so we can achieve this this way. 126 00:06:32,390 --> 00:06:37,650 I'll go into Looks, and I'll just add a "next costume" block 127 00:06:37,650 --> 00:06:39,300 into the forever loop. 128 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, 129 00:06:42,480 --> 00:06:46,670 and then we're going to switch the cat to the next costume. 130 00:06:46,670 --> 00:06:50,390 Now watch what happens when I press the green flag. 131 00:06:50,390 --> 00:06:53,790 Now the cat's legs appear to be moving. 132 00:06:53,790 --> 00:06:57,170 They're moving very quickly because the forever loop is happening very quickly, 133 00:06:57,170 --> 00:06:59,630 going very quickly through the movement, through the bouncing, 134 00:06:59,630 --> 00:07:01,588 and then through the switching of the costumes. 135 00:07:01,588 --> 00:07:04,760 But we've animated this cat just by moving it back and forth 136 00:07:04,760 --> 00:07:07,070 between these two different costumes. 137 00:07:07,070 --> 00:07:08,750 Now its legs are moving rather quickly. 138 00:07:08,750 --> 00:07:10,760 Maybe I want to slow this down a little bit. 139 00:07:10,760 --> 00:07:14,420 And I can do that just by going into Control and waiting a little bit. 140 00:07:14,420 --> 00:07:16,430 One second is maybe a long time to wait. 141 00:07:16,430 --> 00:07:20,390 Let's wait 0.1 seconds, just a fraction of a second, 142 00:07:20,390 --> 00:07:23,520 to slow down the cat just a little bit. 143 00:07:23,520 --> 00:07:26,490 And now the cat's walking in a little more of a leisurely pace. 144 00:07:26,490 --> 00:07:28,500 It's repeating this process over and over, 145 00:07:28,500 --> 00:07:31,560 moving 10 steps, if it's on the edge, it's going to bounce, 146 00:07:31,560 --> 00:07:35,670 switching to the next costume between whether its legs are straight or bent, 147 00:07:35,670 --> 00:07:38,460 and then waiting just a fraction of a second so that we 148 00:07:38,460 --> 00:07:40,440 can slow the cat down a little bit. 149 00:07:40,440 --> 00:07:43,320 And you could play around with this value determining 150 00:07:43,320 --> 00:07:47,280 how long you want the cat to wait for to determine how quickly 151 00:07:47,280 --> 00:07:51,360 or how slowly the cat's going to move across the screen. 152 00:07:51,360 --> 00:07:53,640 But using that loop we now have the ability 153 00:07:53,640 --> 00:07:57,270 to create some effect that's happening inside of our program forever, 154 00:07:57,270 --> 00:08:00,083 that keeps going on and on. 155 00:08:00,083 --> 00:08:01,500 So what's another example of this? 156 00:08:01,500 --> 00:08:04,320 What else could we do with this idea? 157 00:08:04,320 --> 00:08:06,390 Well, let's get rid of the cat for now. 158 00:08:06,390 --> 00:08:09,270 And let's bring back our underwater backdrop. 159 00:08:09,270 --> 00:08:11,010 Here's Underwater 1. 160 00:08:11,010 --> 00:08:17,050 And let's add the fish back into our program. 161 00:08:17,050 --> 00:08:18,750 Here's that fish. 162 00:08:18,750 --> 00:08:20,310 And let's try something else. 163 00:08:20,310 --> 00:08:24,390 When the green flag is clicked, I also want something to happen forever. 164 00:08:24,390 --> 00:08:25,840 What do I want to have happen? 165 00:08:25,840 --> 00:08:30,820 Well, let's go to Motion, and let's have it point in a particular direction. 166 00:08:30,820 --> 00:08:33,520 Now if it were always pointing in the same direction, 167 00:08:33,520 --> 00:08:35,700 then it wouldn't really matter if it was inside of a loop or not. 168 00:08:35,700 --> 00:08:38,100 It's just always going to face the right, face the right, face the right, 169 00:08:38,100 --> 00:08:40,049 face the right, again and again. 170 00:08:40,049 --> 00:08:43,140 But I could have it point in a particular direction, 171 00:08:43,140 --> 00:08:48,080 and I'll go ahead and choose a block like this instead. 172 00:08:48,080 --> 00:08:50,630 Instead of pointing in a direction with a number, 173 00:08:50,630 --> 00:08:53,330 let's have the fish point towards something. 174 00:08:53,330 --> 00:08:55,190 Let's have it point towards the mouse. 175 00:08:55,190 --> 00:08:58,530 It's pointing towards the mouse pointer. 176 00:08:58,530 --> 00:09:02,140 So now watch what happens when I press the flag. 177 00:09:02,140 --> 00:09:05,480 You'll notice that immediately when I press the flag the fish turned. 178 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. 179 00:09:10,600 --> 00:09:14,870 But watch what happens as I move the cursor. 180 00:09:14,870 --> 00:09:18,800 Every time I move the cursor you'll notice the fish is moving, as well. 181 00:09:18,800 --> 00:09:23,570 Because it's constantly, over and over forever, following this instruction 182 00:09:23,570 --> 00:09:26,510 to point towards the mouse pointer. 183 00:09:26,510 --> 00:09:31,030 So wherever the cursor goes, the fish is going to be looking in that direction 184 00:09:31,030 --> 00:09:33,490 because it's pointing towards the mouse pointer. 185 00:09:33,490 --> 00:09:37,010 Now it's looking at the cursor, though it's not really moving yet. 186 00:09:37,010 --> 00:09:39,310 So let's add one more thing to this program. 187 00:09:39,310 --> 00:09:41,800 In addition to pointing towards the mouse pointer, 188 00:09:41,800 --> 00:09:47,042 let's then have the fish move five steps. 189 00:09:47,042 --> 00:09:48,750 So now what's going to happen is wherever 190 00:09:48,750 --> 00:09:51,870 the cursor is, the fish is going to look at the cursor 191 00:09:51,870 --> 00:09:54,430 and then move a little bit closer to that cursor. 192 00:09:54,430 --> 00:09:57,360 So using the cursor will effectively be able to control 193 00:09:57,360 --> 00:10:00,010 the movement of the fish. 194 00:10:00,010 --> 00:10:03,660 I'll press the green flag again, and you'll notice the fish start 195 00:10:03,660 --> 00:10:06,450 to move after the cursor. 196 00:10:06,450 --> 00:10:09,840 Wherever I move the cursor, the fish is pointing in that direction 197 00:10:09,840 --> 00:10:12,797 and then swimming in my direction. 198 00:10:12,797 --> 00:10:16,132 199 00:10:16,132 --> 00:10:18,090 And we can use "forever" for other things, too. 200 00:10:18,090 --> 00:10:21,890 Any time I want something to continue to happen during my program, 201 00:10:21,890 --> 00:10:24,710 a forever loop is often the way that I want to achieve that. 202 00:10:24,710 --> 00:10:27,350 Right now, my backdrop, I can click on the backdrop, 203 00:10:27,350 --> 00:10:30,770 is just showing me this underwater scene. 204 00:10:30,770 --> 00:10:33,110 But I can add some blocks to the backdrop, 205 00:10:33,110 --> 00:10:35,270 as well, telling the backdrop what to do. 206 00:10:35,270 --> 00:10:38,930 And maybe I'll have the backdrop say, when the green flag is clicked, 207 00:10:38,930 --> 00:10:42,735 let's forever play a sound. 208 00:10:42,735 --> 00:10:45,400 209 00:10:45,400 --> 00:10:48,960 The default sound is "pop", but I can change those sounds. 210 00:10:48,960 --> 00:10:52,870 Let's find a sound that's appropriate for being underwater. 211 00:10:52,870 --> 00:10:57,310 And if I scroll down through this long list of sounds, I can try them out, 212 00:10:57,310 --> 00:11:01,736 but I think there's one called-- 213 00:11:01,736 --> 00:11:04,030 Let's search for water maybe? 214 00:11:04,030 --> 00:11:04,757 "Ocean Wave". 215 00:11:04,757 --> 00:11:06,340 Here's "Ocean Wave", related to water. 216 00:11:06,340 --> 00:11:09,840 217 00:11:09,840 --> 00:11:11,670 I can sample hear what that sounds like. 218 00:11:11,670 --> 00:11:13,570 I like that one. 219 00:11:13,570 --> 00:11:18,250 And so let's have the backdrop play this "Ocean Wave" sound forever. 220 00:11:18,250 --> 00:11:21,740 It's going to play the sound until it's done, and then play it again. 221 00:11:21,740 --> 00:11:25,690 And so now when I run the program, notice what you see and hear. 222 00:11:25,690 --> 00:11:33,480 223 00:11:33,480 --> 00:11:36,570 I have the fish following the cursor, and I just stopped the program. 224 00:11:36,570 --> 00:11:39,990 But because I had multiple forever loops, I had a loop on the backdrop, 225 00:11:39,990 --> 00:11:44,108 as well, we were just playing this "Ocean Wave" sound over and over again. 226 00:11:44,108 --> 00:11:46,650 And you could use this if you wanted a sound to play forever. 227 00:11:46,650 --> 00:11:48,567 You could add music to your programs, as well. 228 00:11:48,567 --> 00:11:52,080 If you wanted music to be playing in the background of your Scratch program, 229 00:11:52,080 --> 00:11:55,060 you might have that music sound play until it's done, 230 00:11:55,060 --> 00:11:58,350 and then the song starts right back up again as soon as the song is over. 231 00:11:58,350 --> 00:12:02,070 So you can continue to have music in the background of your Scratch program 232 00:12:02,070 --> 00:12:04,170 going over and over forever. 233 00:12:04,170 --> 00:12:06,660 So these forever loops can be quite useful for helping 234 00:12:06,660 --> 00:12:10,590 to create this sense of continuity, this continued 235 00:12:10,590 --> 00:12:16,770 presence of sounds or movement that are happening inside of our applications. 236 00:12:16,770 --> 00:12:20,630 And so let's try now to create another program. 237 00:12:20,630 --> 00:12:24,860 I'll get rid of the fish, and we'll get rid of the underwater backdrop. 238 00:12:24,860 --> 00:12:28,160 Let's go back to this plain backdrop. 239 00:12:28,160 --> 00:12:30,760 And I'll get rid of this code, too. 240 00:12:30,760 --> 00:12:32,140 Let's bring back the cat. 241 00:12:32,140 --> 00:12:34,870 242 00:12:34,870 --> 00:12:38,620 And what I want the cat to do now, just for fun, 243 00:12:38,620 --> 00:12:41,290 is to not let me get too close to the cat. 244 00:12:41,290 --> 00:12:44,380 If my cursor gets too close to the cat, I would like for the cat 245 00:12:44,380 --> 00:12:47,380 to meow or something, or otherwise indicate that it doesn't 246 00:12:47,380 --> 00:12:50,330 want me to get too close, for example. 247 00:12:50,330 --> 00:12:52,130 And so how might this work? 248 00:12:52,130 --> 00:12:53,260 Well, let's try this. 249 00:12:53,260 --> 00:12:55,300 When the green flag is clicked, my instinct 250 00:12:55,300 --> 00:12:57,220 might be to say, well, if I want to check 251 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. 252 00:13:01,420 --> 00:13:02,710 It sounds like an if-then. 253 00:13:02,710 --> 00:13:05,710 If I'm getting too close to the cat, then have the cat meow 254 00:13:05,710 --> 00:13:07,760 because it's unhappy, for example. 255 00:13:07,760 --> 00:13:11,040 And so I might add an if-then. 256 00:13:11,040 --> 00:13:13,080 And here I can use an operator. 257 00:13:13,080 --> 00:13:15,180 I can use the less than operator, because I 258 00:13:15,180 --> 00:13:19,230 want to check if the distance between the cat and the mouse pointer 259 00:13:19,230 --> 00:13:22,230 is less than some value, we'll say 100. 260 00:13:22,230 --> 00:13:23,550 And how do I find the distance? 261 00:13:23,550 --> 00:13:25,410 That's under the Sensing category. 262 00:13:25,410 --> 00:13:29,220 It turns out there's a block here called "distance to mouse pointer", 263 00:13:29,220 --> 00:13:29,970 so I can check. 264 00:13:29,970 --> 00:13:35,000 Is the distance between the cat and the mouse less than 100? 265 00:13:35,000 --> 00:13:42,630 And if so, let's go ahead and play the meow sound until it's done. 266 00:13:42,630 --> 00:13:45,950 So the idea of this code now, what I would like for this code to do, 267 00:13:45,950 --> 00:13:51,260 is to say if I'm less than 100 spaces away from the cat, 268 00:13:51,260 --> 00:13:53,480 then go ahead and play the meow sound. 269 00:13:53,480 --> 00:13:55,760 And that's what I want the cat to do, but watch what 270 00:13:55,760 --> 00:13:58,880 happens if I try and run this program. 271 00:13:58,880 --> 00:14:00,200 I'll run the program. 272 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 273 00:14:04,890 --> 00:14:08,800 closer nothing seems to be happening. 274 00:14:08,800 --> 00:14:11,980 I'm pretty close to the cat right now, I'm even touching the cat now, 275 00:14:11,980 --> 00:14:15,640 and yet still, I'm not hearing the cat meow. 276 00:14:15,640 --> 00:14:16,360 Why is that? 277 00:14:16,360 --> 00:14:18,430 What's going on? 278 00:14:18,430 --> 00:14:20,530 Well, it has to do with the fact that by default, 279 00:14:20,530 --> 00:14:22,900 when you're running code inside of a program, 280 00:14:22,900 --> 00:14:25,810 that code is only going to run once. 281 00:14:25,810 --> 00:14:27,730 What happened when I pressed the green flag? 282 00:14:27,730 --> 00:14:31,870 When I took my cursor, and I dragged my cursor over the green flag right there, 283 00:14:31,870 --> 00:14:34,810 and then I clicked on it, what was happening inside of the program? 284 00:14:34,810 --> 00:14:37,240 Well, immediately, Scratch checks. 285 00:14:37,240 --> 00:14:38,560 It checks this question. 286 00:14:38,560 --> 00:14:41,680 Is the distance to the mouse pointer less than 100? 287 00:14:41,680 --> 00:14:42,430 Well, no it's not. 288 00:14:42,430 --> 00:14:46,810 This is more than 100 spaces in the world of Scatch's coordinate system, 289 00:14:46,810 --> 00:14:48,740 so we didn't play the meow sound. 290 00:14:48,740 --> 00:14:51,880 And at that point, this stack of blocks was over. 291 00:14:51,880 --> 00:14:55,150 Nothing else happened because we ran through all of these blocks 292 00:14:55,150 --> 00:14:59,320 from top to bottom, and that was the end of the stack of blocks. 293 00:14:59,320 --> 00:15:02,950 What I really wanted to have happen is for this check for this question 294 00:15:02,950 --> 00:15:05,590 to be asked not once, but for the question 295 00:15:05,590 --> 00:15:08,020 to constantly be asked all the time. 296 00:15:08,020 --> 00:15:10,660 I want the cat to be checking, is the distance 297 00:15:10,660 --> 00:15:12,640 of the mouse pointer less than 100? 298 00:15:12,640 --> 00:15:15,370 And any time it turns out that I get to close, 299 00:15:15,370 --> 00:15:18,100 then I want for the cat to play the meow sound. 300 00:15:18,100 --> 00:15:21,050 And that's another case where a forever loop is quite helpful. 301 00:15:21,050 --> 00:15:23,620 I don't just want to ask this question one time. 302 00:15:23,620 --> 00:15:28,100 I want to ask the question forever as long as this program's running. 303 00:15:28,100 --> 00:15:31,120 And so I can go into Control, grab a forever block 304 00:15:31,120 --> 00:15:34,420 and put it around the condition just like that. 305 00:15:34,420 --> 00:15:37,720 And now this question, am I getting too close to the cat, 306 00:15:37,720 --> 00:15:40,120 it's going to be asked not once, but over and over again. 307 00:15:40,120 --> 00:15:43,550 Every time we ask the question, we'll then ask it again. 308 00:15:43,550 --> 00:15:44,950 I'll click on the flag. 309 00:15:44,950 --> 00:15:46,070 The program's now started. 310 00:15:46,070 --> 00:15:48,243 We still don't hear anything, but now watch 311 00:15:48,243 --> 00:15:50,410 what happens as my cursor gets too close to the cat. 312 00:15:50,410 --> 00:15:55,920 313 00:15:55,920 --> 00:15:57,810 So it's further away, we hear nothing. 314 00:15:57,810 --> 00:16:03,540 But when I get too close, then you start to hear the cat meow. 315 00:16:03,540 --> 00:16:07,920 And you hear that happen because this condition is now inside of a loop. 316 00:16:07,920 --> 00:16:10,350 It's repeating over and over again forever, 317 00:16:10,350 --> 00:16:15,620 checking to see if the distance is less than 100. 318 00:16:15,620 --> 00:16:17,470 And so that then is a forever loop, a loop 319 00:16:17,470 --> 00:16:20,110 that's going to run some code over and over again, forever, 320 00:16:20,110 --> 00:16:21,357 until the end of the program. 321 00:16:21,357 --> 00:16:24,190 And you'll often hear this also called an infinite loop, a loop that 322 00:16:24,190 --> 00:16:26,990 repeats infinitely again and again. 323 00:16:26,990 --> 00:16:29,050 But when we repeat code inside of our programs 324 00:16:29,050 --> 00:16:31,880 we don't necessarily always want for it to happen infinitely. 325 00:16:31,880 --> 00:16:34,810 We don't always want for code to run forever. 326 00:16:34,810 --> 00:16:38,740 Sometimes we do want the code to repeat, but not infinitely, many times. 327 00:16:38,740 --> 00:16:42,680 Just five times, or 10 times, or 20 times, for example. 328 00:16:42,680 --> 00:16:45,400 And so how could we do that instead? 329 00:16:45,400 --> 00:16:47,620 Well, for that we're going to need another block. 330 00:16:47,620 --> 00:16:53,330 Not the forever block, which I'll get rid of now, but this block here. 331 00:16:53,330 --> 00:16:57,310 This block is "repeat" and then 10, and this is also a loop. 332 00:16:57,310 --> 00:16:59,060 It's going to take some code and repeat it 333 00:16:59,060 --> 00:17:03,020 multiple times, which is implied by this arrow here at the bottom right. 334 00:17:03,020 --> 00:17:07,640 But we get to specify, by typing in here, how many times I 335 00:17:07,640 --> 00:17:10,287 would like for that code to repeat. 336 00:17:10,287 --> 00:17:11,579 And so when can this be useful? 337 00:17:11,579 --> 00:17:14,180 When might I want to use a repeat block? 338 00:17:14,180 --> 00:17:18,060 Well, imagine I wanted the cat to meow three times, for example. 339 00:17:18,060 --> 00:17:21,230 I could go to Sounds, and just play sound meow until done, 340 00:17:21,230 --> 00:17:24,740 and then play sound meow until done, and then play sound meow until done. 341 00:17:24,740 --> 00:17:31,640 And now when I press the flag I do hear three meows, 342 00:17:31,640 --> 00:17:33,830 but this is repetitive code. 343 00:17:33,830 --> 00:17:37,340 I find myself repeating the same blocks again and again and again. 344 00:17:37,340 --> 00:17:42,500 And I could arguably improve the design of my program by using a loop instead. 345 00:17:42,500 --> 00:17:46,370 Instead of repeating myself again and again, I'll just have a loop. 346 00:17:46,370 --> 00:17:49,160 Not repeat 10, but repeat three. 347 00:17:49,160 --> 00:17:51,650 And what am I repeating three times? 348 00:17:51,650 --> 00:17:54,560 Let's play the sound meow until done. 349 00:17:54,560 --> 00:18:00,870 Now I press the flag, and it still meows three times, 350 00:18:00,870 --> 00:18:03,420 but this is better than the program I had before. 351 00:18:03,420 --> 00:18:05,320 It's better for a couple of reasons. 352 00:18:05,320 --> 00:18:08,070 One, I'm being more efficient with my use of blocks. 353 00:18:08,070 --> 00:18:11,310 Before, I needed three blocks to say meow three times. 354 00:18:11,310 --> 00:18:13,830 Now I just have one block that says meow, 355 00:18:13,830 --> 00:18:16,540 plus one block that's handling the repeating. 356 00:18:16,540 --> 00:18:18,900 So I'm being more efficient with my use of blocks. 357 00:18:18,900 --> 00:18:21,600 And also, it's going to be easier later for me 358 00:18:21,600 --> 00:18:25,110 to modify this program if I want to change the way it behaves. 359 00:18:25,110 --> 00:18:29,670 Say that I later wanted this to say meow not three times, but 30 times. 360 00:18:29,670 --> 00:18:32,010 In my old version I would have to just drag out 361 00:18:32,010 --> 00:18:34,890 an additional 27 additional play meow blocks 362 00:18:34,890 --> 00:18:36,750 until we were able to get to that point. 363 00:18:36,750 --> 00:18:40,560 Now I just change the number that appears inside of that oval. 364 00:18:40,560 --> 00:18:43,860 And by changing that value I change the number of times 365 00:18:43,860 --> 00:18:46,290 that the cat is going to meow. 366 00:18:46,290 --> 00:18:49,110 And this doesn't just have to be a number that I type in. 367 00:18:49,110 --> 00:18:52,360 I can drag in any value into that oval, as well, 368 00:18:52,360 --> 00:18:55,740 which means I could do something like ask the user how many times 369 00:18:55,740 --> 00:18:58,450 the user wants something to happen. 370 00:18:58,450 --> 00:19:00,010 So let's give that a try. 371 00:19:00,010 --> 00:19:04,300 I can go into the Sensing category of blocks and ask a question. 372 00:19:04,300 --> 00:19:07,810 The question I'll ask is, type in a number. 373 00:19:07,810 --> 00:19:13,780 And then instead of repeating three times, let's repeat answer times. 374 00:19:13,780 --> 00:19:17,650 Whatever the answer is that's how many times I want the cat to meow. 375 00:19:17,650 --> 00:19:20,150 I press the flag and I type 3, for example. 376 00:19:20,150 --> 00:19:25,270 And then you'll hear three meows. 377 00:19:25,270 --> 00:19:35,220 But if I press the flag and this time try 5, for example, 378 00:19:35,220 --> 00:19:36,820 then you hear it five times. 379 00:19:36,820 --> 00:19:39,600 So the user controls how many times this happens 380 00:19:39,600 --> 00:19:43,770 because the answer block I've plugged into that open space 381 00:19:43,770 --> 00:19:46,372 inside of the repeat block. 382 00:19:46,372 --> 00:19:47,830 And so the repeat block is helpful. 383 00:19:47,830 --> 00:19:50,970 Now, any time I want something not to happen forever, 384 00:19:50,970 --> 00:19:53,790 but still to happen multiple times. 385 00:19:53,790 --> 00:19:55,390 What's another example of that? 386 00:19:55,390 --> 00:19:57,840 Well, way back when we first introduced motion 387 00:19:57,840 --> 00:20:00,750 I showed you how you can get the cat to move in a circle. 388 00:20:00,750 --> 00:20:05,220 I said you could do something like move, let's say, 30 steps. 389 00:20:05,220 --> 00:20:08,370 And then turn 15 degrees, just turn a little bit. 390 00:20:08,370 --> 00:20:10,920 And that had the effect, if I bring the cat up to the top, 391 00:20:10,920 --> 00:20:14,430 where whenever I press the flag you'll see the cat move 392 00:20:14,430 --> 00:20:19,620 and rotate a little bit, and the cat moves in a circle. 393 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 394 00:20:24,840 --> 00:20:26,960 I want the cat to move. 395 00:20:26,960 --> 00:20:30,950 And every time it moved 30 steps and then turned 15 degrees. 396 00:20:30,950 --> 00:20:33,560 This is a great opportunity to use a loop so that I don't 397 00:20:33,560 --> 00:20:35,120 have to keep doing all the clicking. 398 00:20:35,120 --> 00:20:38,630 The loop will just take care of repeating this process again 399 00:20:38,630 --> 00:20:39,710 and again and again. 400 00:20:39,710 --> 00:20:43,130 So I can go to Control, repeat a certain number of times 401 00:20:43,130 --> 00:20:47,190 and, I counted, to get all the way around I had to click 24 times. 402 00:20:47,190 --> 00:20:50,270 So go ahead and repeat this code 24 times 403 00:20:50,270 --> 00:20:54,230 where now when I press the green flag, you'll see the cat 404 00:20:54,230 --> 00:20:57,120 move all the way around in a circle. 405 00:20:57,120 --> 00:20:59,480 Very quickly, it's moving all the way around in a circle 406 00:20:59,480 --> 00:21:01,010 every time I click on the flag. 407 00:21:01,010 --> 00:21:03,110 And the reason for that is because it's taking 408 00:21:03,110 --> 00:21:07,310 every small movement, that movement of just moving 30 steps and then rotating, 409 00:21:07,310 --> 00:21:09,560 and it's repeating that again and again and again very 410 00:21:09,560 --> 00:21:13,790 quickly, faster than I could ever click on buttons on the Scratch interface. 411 00:21:13,790 --> 00:21:16,580 And the effect of that is that you're able to see this cat now 412 00:21:16,580 --> 00:21:20,890 move in a circle, as well. 413 00:21:20,890 --> 00:21:24,840 And so with that in mind, let's revisit one final program 414 00:21:24,840 --> 00:21:26,040 that we've seen before. 415 00:21:26,040 --> 00:21:28,830 And that's the balloon program we've seen a couple of times now. 416 00:21:28,830 --> 00:21:31,170 Where before, last time we saw the balloon, 417 00:21:31,170 --> 00:21:33,900 I was pressing the space bar to try and inflate 418 00:21:33,900 --> 00:21:36,960 the balloon until eventually it popped. 419 00:21:36,960 --> 00:21:40,740 But now that we have these loops to repeat something again and again, 420 00:21:40,740 --> 00:21:43,500 I can have the balloon inflate itself without me 421 00:21:43,500 --> 00:21:47,900 having to press buttons again and again to get the balloon to inflate. 422 00:21:47,900 --> 00:21:49,590 I'll center the balloon. 423 00:21:49,590 --> 00:21:51,820 And let's try this. 424 00:21:51,820 --> 00:21:55,920 When the flag is clicked, let's go ahead and repeat 425 00:21:55,920 --> 00:22:00,230 something some number of times. 426 00:22:00,230 --> 00:22:03,670 And what I want to do is I want to change the size 427 00:22:03,670 --> 00:22:05,740 by 10, which is what we did before. 428 00:22:05,740 --> 00:22:08,830 And then just to make this happen a little more slowly, let's 429 00:22:08,830 --> 00:22:14,870 wait, not a full second, but maybe 0.2 seconds, just a fraction of a second. 430 00:22:14,870 --> 00:22:17,620 So it's going to grow a little then wait, grow a little 431 00:22:17,620 --> 00:22:20,110 and then wait, and then after we repeat 10 times, 432 00:22:20,110 --> 00:22:27,250 after we're done with all of that looping, let's hide the balloon. 433 00:22:27,250 --> 00:22:29,380 And we'll play a sound. 434 00:22:29,380 --> 00:22:32,750 We'll play this pop sound. 435 00:22:32,750 --> 00:22:35,630 So now I'll press the flag. 436 00:22:35,630 --> 00:22:41,048 It gets bigger and bigger, and then it disappears with a pop. 437 00:22:41,048 --> 00:22:41,840 Let's try it again. 438 00:22:41,840 --> 00:22:44,380 I'll press the flag. 439 00:22:44,380 --> 00:22:45,610 OK, nothing happened. 440 00:22:45,610 --> 00:22:49,750 The balloon didn't come back, which is a little bit strange. 441 00:22:49,750 --> 00:22:52,720 But if you pay attention to my code, you'll see why that's happening. 442 00:22:52,720 --> 00:22:56,860 I repeated 10 times this process of change the size by 10 443 00:22:56,860 --> 00:22:58,820 then wait 0.2 seconds. 444 00:22:58,820 --> 00:23:02,620 And then after that repeated we decided to hide the balloon 445 00:23:02,620 --> 00:23:04,510 and play the pop sound. 446 00:23:04,510 --> 00:23:07,450 But because we use this hide block to hide the balloon, 447 00:23:07,450 --> 00:23:11,630 the balloon's disappeared, and then it never really comes back. 448 00:23:11,630 --> 00:23:15,280 So if ever I want something to reset whenever I start the program again, 449 00:23:15,280 --> 00:23:19,600 I often need to add blocks immediately after the green flag is clicked to say, 450 00:23:19,600 --> 00:23:23,170 when you press the green flag let's be sure to show the balloon. 451 00:23:23,170 --> 00:23:26,770 Because otherwise, the balloon is going to stay hidden from the last time 452 00:23:26,770 --> 00:23:29,720 I hid the balloon when I ran the program. 453 00:23:29,720 --> 00:23:32,410 And so I can use the "show" block for that. 454 00:23:32,410 --> 00:23:38,480 Going into Looks, dragging out this block that says "show", 455 00:23:38,480 --> 00:23:45,640 and because I've been changing the size I better change the size back to 100%. 456 00:23:45,640 --> 00:23:47,890 So now I see the balloon. 457 00:23:47,890 --> 00:23:50,420 It gets bigger and then it disappears. 458 00:23:50,420 --> 00:23:55,030 And I can press the green flag, and I see the same thing happen again. 459 00:23:55,030 --> 00:23:57,820 Every time I click the green flag, we show the balloon again, 460 00:23:57,820 --> 00:24:01,750 set its size back to 100%, then we let it repeat, growing and growing 461 00:24:01,750 --> 00:24:05,410 and growing before we hide the balloon and play the sound "pop" 462 00:24:05,410 --> 00:24:08,320 until that sound is done. 463 00:24:08,320 --> 00:24:12,170 And this allows me to repeat the process of growing the balloon. 464 00:24:12,170 --> 00:24:14,230 And to what size does it grow? 465 00:24:14,230 --> 00:24:17,350 Well, we're changing the size by 10 every time, 466 00:24:17,350 --> 00:24:20,013 and we're repeating that process 10 times, 467 00:24:20,013 --> 00:24:22,180 so I could do the math in my head and say, OK, we're 468 00:24:22,180 --> 00:24:24,220 going to get up to a size of 200. 469 00:24:24,220 --> 00:24:26,110 What if I wanted the size to be bigger? 470 00:24:26,110 --> 00:24:31,000 What if I wanted to let the balloon grow up to size 250, for example? 471 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. 472 00:24:34,720 --> 00:24:36,970 I want to get up to 250. 473 00:24:36,970 --> 00:24:39,700 And if we're changing the size by 10 every time, 474 00:24:39,700 --> 00:24:43,240 then I'm going to calculate that's 15 repetitions to get it up there. 475 00:24:43,240 --> 00:24:44,050 And I can try that. 476 00:24:44,050 --> 00:24:47,750 477 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 478 00:24:52,220 --> 00:24:54,860 to figure that out, to figure out, OK, 15, that's 479 00:24:54,860 --> 00:24:57,890 the number of times I would like for this code to repeat. 480 00:24:57,890 --> 00:25:00,620 There is one other type of loop that I'll introduce finally 481 00:25:00,620 --> 00:25:03,440 as one final block for today that can sometimes 482 00:25:03,440 --> 00:25:05,330 make our logic a little bit easier. 483 00:25:05,330 --> 00:25:08,750 And that's this block here, "repeat until", 484 00:25:08,750 --> 00:25:14,090 and this block effectively tries to combine a loop and a condition. 485 00:25:14,090 --> 00:25:16,850 We're going to repeat some blocks, not a fixed number of times, 486 00:25:16,850 --> 00:25:22,850 not five times, 10 times, 20 times, but until some question is answered true. 487 00:25:22,850 --> 00:25:26,600 And notice this hexagon-shaped opening is the same as the shape that we saw 488 00:25:26,600 --> 00:25:29,820 in those "if" blocks and the "if-else" blocks, for example. 489 00:25:29,820 --> 00:25:32,750 So what this is going to do is repeat some code 490 00:25:32,750 --> 00:25:36,660 until the answer to some question is true. 491 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, 492 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. 493 00:25:44,630 --> 00:25:49,280 I can just say repeat until, drag the equals sign out, 494 00:25:49,280 --> 00:25:58,950 and I'm going to repeat until the size is equal to 250. 495 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 496 00:26:02,200 --> 00:26:06,660 until block, get rid of my old repeat. 497 00:26:06,660 --> 00:26:09,170 And this is my new code. 498 00:26:09,170 --> 00:26:12,680 I show the balloon, set its size to 100%, 499 00:26:12,680 --> 00:26:15,470 we're going to keep repeating until the size is 250. 500 00:26:15,470 --> 00:26:18,620 I don't know in advance necessarily how long that's going to take, 501 00:26:18,620 --> 00:26:21,950 but we're going to just keep repeating the code until that's true. 502 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 503 00:26:26,300 --> 00:26:30,390 hide our balloon and play that pop sound. 504 00:26:30,390 --> 00:26:37,660 And so I can press the flag, and you see the balloon grow and grow, 505 00:26:37,660 --> 00:26:42,080 and once it reaches 250, at that point the balloon disappears. 506 00:26:42,080 --> 00:26:45,890 So three different types of loops that we've been able to explore today. 507 00:26:45,890 --> 00:26:49,510 One is the forever loop, that takes some blocks and repeats them over and over, 508 00:26:49,510 --> 00:26:51,940 infinitely, until we stop the program. 509 00:26:51,940 --> 00:26:55,150 One is the repeat block, which repeats a fixed number of times. 510 00:26:55,150 --> 00:26:57,910 I can say do this five times or 10 times or 20 times. 511 00:26:57,910 --> 00:27:01,987 And then, finally, the repeat until block that combines some of the ideas 512 00:27:01,987 --> 00:27:04,570 that we've been taking a look at today, looking at the ability 513 00:27:04,570 --> 00:27:08,470 to repeat until a particular question is answered true. 514 00:27:08,470 --> 00:27:10,480 And using those loops, we have the ability 515 00:27:10,480 --> 00:27:12,730 to make our programs even more interesting. 516 00:27:12,730 --> 00:27:15,010 Rather than have to click a button multiple times 517 00:27:15,010 --> 00:27:17,080 and repeat ourselves with code multiple times, 518 00:27:17,080 --> 00:27:21,537 we can let the code repeat itself just via the use of these loops. 519 00:27:21,537 --> 00:27:24,370 That's it for an introduction to Programming with Scratch for today. 520 00:27:24,370 --> 00:27:26,995 We have a couple more opportunities to take a look at what else 521 00:27:26,995 --> 00:27:28,150 we can do with Scratch. 522 00:27:28,150 --> 00:27:30,450 So we'll see you next time. 523 00:27:30,450 --> 00:27:31,239