1 00:00:00,000 --> 00:00:03,458 [MUSIC PLAYING] 2 00:00:03,458 --> 00:00:17,155 3 00:00:17,155 --> 00:00:19,280 SPEAKER: Welcome back, everyone, to an introduction 4 00:00:19,280 --> 00:00:20,600 to Programming with Scratch. 5 00:00:20,600 --> 00:00:23,720 And at this point, we've seen many of the major features of Scratch, 6 00:00:23,720 --> 00:00:26,510 how we can use loops and variables and conditions 7 00:00:26,510 --> 00:00:28,800 to put together some interesting projects. 8 00:00:28,800 --> 00:00:32,420 So today, let's take a step back, think about how we might use these blocks 9 00:00:32,420 --> 00:00:36,140 inside of our projects, and how we might be able to improve upon the way 10 00:00:36,140 --> 00:00:40,110 that we've designed the blocks inside of our Scratch projects. 11 00:00:40,110 --> 00:00:42,800 So let's start with a sample application or project 12 00:00:42,800 --> 00:00:44,160 that we might want to create. 13 00:00:44,160 --> 00:00:45,620 Let's open up Scratch. 14 00:00:45,620 --> 00:00:49,010 And let's say I want to make a game that involves the dinosaur 15 00:00:49,010 --> 00:00:51,570 and moving the dinosaur around, for example. 16 00:00:51,570 --> 00:00:55,800 So to do that, I'll start by getting rid of the cat and adding a new sprite. 17 00:00:55,800 --> 00:00:58,590 I'll go ahead and find the dinosaur. 18 00:00:58,590 --> 00:01:01,890 And we'll bring the dinosaur into our Scratch project. 19 00:01:01,890 --> 00:01:05,820 And I'd like to be able to control the dinosaur by moving the arrow keys. 20 00:01:05,820 --> 00:01:09,400 And so to do that, I'll need to respond to various different events. 21 00:01:09,400 --> 00:01:14,020 I can use the when Up Arrow key is pressed, for example. 22 00:01:14,020 --> 00:01:18,510 And then have the dinosaur respond by changing its y-value-- 23 00:01:18,510 --> 00:01:21,460 the up and down value-- by 10, for example. 24 00:01:21,460 --> 00:01:25,050 And then I'll do similar things for the other arrow keys. 25 00:01:25,050 --> 00:01:27,580 I'll add one for the Down Arrow key. 26 00:01:27,580 --> 00:01:29,790 And when the Down Arrow key is pressed, let's 27 00:01:29,790 --> 00:01:35,820 go ahead and change the y-value by negative 10 to move the dinosaur down. 28 00:01:35,820 --> 00:01:40,250 Let's add one event for the Right Arrow key. 29 00:01:40,250 --> 00:01:43,250 When the Right Arrow key is pressed, instead of changing y, 30 00:01:43,250 --> 00:01:48,230 we'll instead change x, which is the left or right value for the dinosaur. 31 00:01:48,230 --> 00:01:50,450 And then finally, we'll also allow the dinosaur 32 00:01:50,450 --> 00:01:54,560 to respond to when the Left Arrow key is pressed. 33 00:01:54,560 --> 00:01:56,810 At which point, we'll also change x. 34 00:01:56,810 --> 00:01:59,835 But instead of positive 10, it's going to be negative 10. 35 00:01:59,835 --> 00:02:01,460 And we've seen blocks like this before. 36 00:02:01,460 --> 00:02:04,220 And what they allow the dinosaur to do is respond 37 00:02:04,220 --> 00:02:05,840 to me pressing keys on the keyboard. 38 00:02:05,840 --> 00:02:08,690 If I press the Right Arrow, the dinosaur moves right. 39 00:02:08,690 --> 00:02:11,120 If I press the Left Arrow, the dinosaur moves left. 40 00:02:11,120 --> 00:02:13,160 If I press Up, the dinosaur moves up. 41 00:02:13,160 --> 00:02:16,920 And if I press Down, the dinosaur moves down. 42 00:02:16,920 --> 00:02:18,920 But let's not give this game away to win, 43 00:02:18,920 --> 00:02:22,370 some way that we can-- goal that we can try to accomplish. 44 00:02:22,370 --> 00:02:24,230 And I'll add a new sprite. 45 00:02:24,230 --> 00:02:27,230 And let's look through the sprites and see what might make sense here. 46 00:02:27,230 --> 00:02:29,420 I'm going to go with the star. 47 00:02:29,420 --> 00:02:31,190 So here's the star sprite. 48 00:02:31,190 --> 00:02:34,700 And I would like the dinosaur to try to chase after the star 49 00:02:34,700 --> 00:02:36,890 as by moving around using the arrow keys. 50 00:02:36,890 --> 00:02:39,830 And maybe once the dinosaur is touching the star, 51 00:02:39,830 --> 00:02:42,368 well, then we'll say that we've won the game. 52 00:02:42,368 --> 00:02:44,910 And there are multiple ways that I could go about doing this. 53 00:02:44,910 --> 00:02:46,010 But here's one way. 54 00:02:46,010 --> 00:02:48,220 I could say that every time I move-- 55 00:02:48,220 --> 00:02:52,820 every time I change y by some amount or change x by some amount, let's go ahead 56 00:02:52,820 --> 00:02:56,570 and check to see if we're touching the star. 57 00:02:56,570 --> 00:03:00,770 So for example, when the Up Arrow is pressed, we change the y by 10. 58 00:03:00,770 --> 00:03:02,960 But we've now moved the dinosaur, so we better now 59 00:03:02,960 --> 00:03:06,140 check to see if the dinosaur is touching the star. 60 00:03:06,140 --> 00:03:07,750 So what can I do? 61 00:03:07,750 --> 00:03:12,770 I could go into control here and ask a question with IF. 62 00:03:12,770 --> 00:03:15,290 And I'll move these other scripts out of the way for now 63 00:03:15,290 --> 00:03:17,180 just by dragging them around. 64 00:03:17,180 --> 00:03:21,830 And what I'd like to check for is if the dinosaur is touching the star. 65 00:03:21,830 --> 00:03:23,720 Touching, that's under Sensing. 66 00:03:23,720 --> 00:03:27,980 And here is a touching block that I'll drag out and change 67 00:03:27,980 --> 00:03:30,570 mouse pointer to star. 68 00:03:30,570 --> 00:03:33,650 So now, when I press the Up Arrow, y will change by 10, 69 00:03:33,650 --> 00:03:37,460 and then the dinosaur will check to see if it's touching the star. 70 00:03:37,460 --> 00:03:45,210 And if it is, let's go ahead and let the dinosaur report 71 00:03:45,210 --> 00:03:47,190 how long it took to find the star. 72 00:03:47,190 --> 00:03:49,260 So maybe it's going to say something. 73 00:03:49,260 --> 00:03:53,090 And it's going to say the timer. 74 00:03:53,090 --> 00:03:55,520 The timer under Sensing [INAUDIBLE] is just how many 75 00:03:55,520 --> 00:04:00,380 seconds has it been since we started this program. 76 00:04:00,380 --> 00:04:02,330 So we'll go ahead and start my project. 77 00:04:02,330 --> 00:04:05,150 And I'll press the green flag. 78 00:04:05,150 --> 00:04:07,130 I'll move the dinosaur to the right. 79 00:04:07,130 --> 00:04:08,900 And then move the dinosaur up. 80 00:04:08,900 --> 00:04:11,930 And notice that as soon as it's touching the star, it reports. 81 00:04:11,930 --> 00:04:13,880 It's been 4.82 seconds. 82 00:04:13,880 --> 00:04:17,390 And that's how long it took the dinosaur to find the star. 83 00:04:17,390 --> 00:04:19,310 Now, this doesn't work perfectly all the time. 84 00:04:19,310 --> 00:04:21,050 I can try this again, for example. 85 00:04:21,050 --> 00:04:22,250 Press the flag. 86 00:04:22,250 --> 00:04:25,160 This time if I move up first-- move all the way up, and now 87 00:04:25,160 --> 00:04:28,470 start moving to the right, well, I'm touching the star, 88 00:04:28,470 --> 00:04:30,750 but no time was reported. 89 00:04:30,750 --> 00:04:31,542 Why not? 90 00:04:31,542 --> 00:04:33,750 Well, if you notice, we can take a look at the blocks 91 00:04:33,750 --> 00:04:35,730 that I have inside of this project. 92 00:04:35,730 --> 00:04:38,850 My condition-- this condition here, if we're touching the star, 93 00:04:38,850 --> 00:04:40,980 then say the timer for two seconds-- 94 00:04:40,980 --> 00:04:44,070 it appears here when the Up Arrow key is pressed. 95 00:04:44,070 --> 00:04:46,920 But really, I should be checking this all the time, whenever I move. 96 00:04:46,920 --> 00:04:49,920 Whether I'm moving to the right or to the left or moving down. 97 00:04:49,920 --> 00:04:52,140 Every time the dinosaur moves, I would like 98 00:04:52,140 --> 00:04:56,280 for the dinosaur to check to see if the dinosaur is touching the star. 99 00:04:56,280 --> 00:04:59,130 So what I'm going to need to do to make this project work is 100 00:04:59,130 --> 00:05:01,650 take this same condition, if we're touching the star, 101 00:05:01,650 --> 00:05:03,480 say the time say the timer. 102 00:05:03,480 --> 00:05:07,860 And I'll have to include that in each of the other scripts in my project 103 00:05:07,860 --> 00:05:08,550 as well. 104 00:05:08,550 --> 00:05:11,850 That way it's going to check regardless of what key on the keyboard 105 00:05:11,850 --> 00:05:15,420 the person playing the game is actually pressing. 106 00:05:15,420 --> 00:05:18,460 And so I could add this to the Down Arrow, for example. 107 00:05:18,460 --> 00:05:23,010 And the way that I would do that is by adding a condition and saying, 108 00:05:23,010 --> 00:05:29,070 if we are touching the star, then go ahead 109 00:05:29,070 --> 00:05:34,230 and say the timer for two seconds. 110 00:05:34,230 --> 00:05:35,898 And that took a fair bit of time. 111 00:05:35,898 --> 00:05:38,940 So you might also know that you can duplicate some of the blocks instead. 112 00:05:38,940 --> 00:05:42,120 Since these are really just the same blocks, I can just copy them again. 113 00:05:42,120 --> 00:05:44,550 To get them to work for Left Arrow, I'll go ahead 114 00:05:44,550 --> 00:05:48,540 and Control or Right Click on the IF block and just say Duplicate. 115 00:05:48,540 --> 00:05:50,370 I want to copy this whole section. 116 00:05:50,370 --> 00:05:52,920 And I'll drag it underneath the Left Arrow. 117 00:05:52,920 --> 00:05:55,590 And I'll do that one more time for the Right Arrow. 118 00:05:55,590 --> 00:05:58,620 Duplicate, drag it underneath Right Arrow. 119 00:05:58,620 --> 00:06:01,860 And now, regardless of which Arrow key is pressed, 120 00:06:01,860 --> 00:06:04,080 whether it's Up or Down or Left or Right, 121 00:06:04,080 --> 00:06:07,697 we're still going to be able to check if we're touching the star. 122 00:06:07,697 --> 00:06:09,780 And just to make things a little more interesting, 123 00:06:09,780 --> 00:06:11,940 let's add some code to the star too. 124 00:06:11,940 --> 00:06:14,830 Let's say when I press the green flag to start the program, 125 00:06:14,830 --> 00:06:17,910 let's have the star go to a random position. 126 00:06:17,910 --> 00:06:20,850 Just so that it chooses somewhere on the stage to go to 127 00:06:20,850 --> 00:06:24,890 so that the game is a little bit different every time. 128 00:06:24,890 --> 00:06:26,660 Now I press the flag. 129 00:06:26,660 --> 00:06:27,680 The program started. 130 00:06:27,680 --> 00:06:29,610 You see the star moved a little bit. 131 00:06:29,610 --> 00:06:34,520 And now I can use the arrows to try and make my way to the star. 132 00:06:34,520 --> 00:06:38,330 And once I get to the star, you'll notice that the dinosaur reported 133 00:06:38,330 --> 00:06:39,710 to me how long it took. 134 00:06:39,710 --> 00:06:42,740 In this case, it was 9.70 seconds. 135 00:06:42,740 --> 00:06:45,200 And 9.70, that's very precise. 136 00:06:45,200 --> 00:06:47,240 Maybe I decide a little bit later, I'd like 137 00:06:47,240 --> 00:06:49,280 to improve upon this program a little bit. 138 00:06:49,280 --> 00:06:53,510 And what I'd really like to do is I would like to not say 9.70, 139 00:06:53,510 --> 00:06:55,100 just rounded to 10 seconds. 140 00:06:55,100 --> 00:06:57,110 Round to the nearest number of seconds. 141 00:06:57,110 --> 00:06:59,930 And we've seen before, there's a block under Operators 142 00:06:59,930 --> 00:07:03,990 called Round that can round a number to the nearest whole number. 143 00:07:03,990 --> 00:07:06,140 And so that might be what I would want to do here. 144 00:07:06,140 --> 00:07:09,480 And what would I have to change in this project to get that to work? 145 00:07:09,480 --> 00:07:13,910 Well, I'd have to add Round to this say block and to this say block 146 00:07:13,910 --> 00:07:16,130 and to this say block, and the one down below too. 147 00:07:16,130 --> 00:07:18,410 I'd have to add it to all four directions. 148 00:07:18,410 --> 00:07:22,670 Because effectively, I've copied myself again and again and again and again. 149 00:07:22,670 --> 00:07:26,270 And whenever you find yourself copying a lot of code in your project 150 00:07:26,270 --> 00:07:29,450 multiple times, oftentimes, that's an indication 151 00:07:29,450 --> 00:07:33,310 that there might be a better way to try to solve that problem. 152 00:07:33,310 --> 00:07:34,730 And in this case, there is. 153 00:07:34,730 --> 00:07:38,990 And what we can do is create an entirely new block of our own. 154 00:07:38,990 --> 00:07:41,310 Instead of repeat all of these-- 155 00:07:41,310 --> 00:07:44,420 this combination of multiple blocks, the IF and the touching 156 00:07:44,420 --> 00:07:48,170 and the say and the timer and repeating that set of blocks multiple times, 157 00:07:48,170 --> 00:07:50,840 I'm going to write the set of blocks once. 158 00:07:50,840 --> 00:07:54,800 And then just refer back to that collection of blocks by a name 159 00:07:54,800 --> 00:07:55,400 that I choose. 160 00:07:55,400 --> 00:07:57,920 And I'll show you exactly what I mean by that right now. 161 00:07:57,920 --> 00:08:01,160 We're going to look at a new section of the Scratch interface, 162 00:08:01,160 --> 00:08:03,740 which is here called My Blocks. 163 00:08:03,740 --> 00:08:06,890 And this is a place where you can create blocks of your own. 164 00:08:06,890 --> 00:08:11,270 Scratch has blocks that say something, that change the y-value of the x-value 165 00:08:11,270 --> 00:08:12,050 by something. 166 00:08:12,050 --> 00:08:14,990 But we can create a new block, if we would like to. 167 00:08:14,990 --> 00:08:17,720 And I can click Make a Block to make a new block. 168 00:08:17,720 --> 00:08:19,520 And I have to give the block a name. 169 00:08:19,520 --> 00:08:23,960 And I'll call the block Check If Won, because that's 170 00:08:23,960 --> 00:08:25,220 what I want the block to do. 171 00:08:25,220 --> 00:08:27,800 I would like the block to just take care of checking 172 00:08:27,800 --> 00:08:29,330 if the dinosaur has won the game. 173 00:08:29,330 --> 00:08:31,370 Meaning checking if we're touching the star. 174 00:08:31,370 --> 00:08:35,299 And if so, go ahead and report how long it took. 175 00:08:35,299 --> 00:08:38,120 So I've defined this Check If Won block. 176 00:08:38,120 --> 00:08:41,130 And what you'll notice here, up at the top of my screen, 177 00:08:41,130 --> 00:08:45,620 I now have a block that says, define check if one. 178 00:08:45,620 --> 00:08:48,620 Everything beneath this block now are going 179 00:08:48,620 --> 00:08:51,230 to be the blocks that run every time I want 180 00:08:51,230 --> 00:08:57,000 to check if won-- every time I want to check if the dinosaur has won the game. 181 00:08:57,000 --> 00:08:58,100 So what should I do? 182 00:08:58,100 --> 00:09:01,610 Well, I'll take this same condition, if touching star, 183 00:09:01,610 --> 00:09:03,740 then say the timer for two seconds. 184 00:09:03,740 --> 00:09:08,150 And I'm going to put that underneath this definition for check if won. 185 00:09:08,150 --> 00:09:11,030 What does it mean to check if the dinosaurs won the game? 186 00:09:11,030 --> 00:09:14,690 Well, it means check if the dinosaur is touching the star then 187 00:09:14,690 --> 00:09:18,050 say the timer for two seconds. 188 00:09:18,050 --> 00:09:21,020 And now, underneath this event here, when the Up Arrow key 189 00:09:21,020 --> 00:09:23,900 is pressed, rather than change y by 10 and then 190 00:09:23,900 --> 00:09:27,540 this complicated condition with multiple blocks that I had to put together, 191 00:09:27,540 --> 00:09:29,570 I can just change y by 10. 192 00:09:29,570 --> 00:09:32,660 And then drag out this new block that I created and I 193 00:09:32,660 --> 00:09:36,890 picked a name for called check if won. 194 00:09:36,890 --> 00:09:40,490 So the logic for that script now, when the upper deck is pressed, 195 00:09:40,490 --> 00:09:41,580 is much simpler. 196 00:09:41,580 --> 00:09:42,830 It's only two blocks. 197 00:09:42,830 --> 00:09:44,480 Change y by 10. 198 00:09:44,480 --> 00:09:46,190 And then check if won. 199 00:09:46,190 --> 00:09:47,900 And what does it mean to check if won? 200 00:09:47,900 --> 00:09:50,270 Well, I defined what that means up here. 201 00:09:50,270 --> 00:09:52,310 The definition of the check if one block-- what 202 00:09:52,310 --> 00:09:54,710 should happen when I run that block, is we 203 00:09:54,710 --> 00:09:59,850 want to check if we're touching the star then say the timer for two seconds. 204 00:09:59,850 --> 00:10:02,870 And now, I can do the same thing for all of the other scripts as well. 205 00:10:02,870 --> 00:10:06,080 Replace this complicated condition with four different blocks 206 00:10:06,080 --> 00:10:07,230 that I've put together. 207 00:10:07,230 --> 00:10:11,640 And instead, just use a single block, the Check If Won block. 208 00:10:11,640 --> 00:10:15,860 So I'm going to delete this condition and just replace it with this one Check 209 00:10:15,860 --> 00:10:16,940 If Won block. 210 00:10:16,940 --> 00:10:19,970 And I'll do the same for the Left Arrow, getting rid of the condition, 211 00:10:19,970 --> 00:10:21,650 adding back Check If Won. 212 00:10:21,650 --> 00:10:25,130 And the same for the Right Arrow, getting rid of the condition, 213 00:10:25,130 --> 00:10:27,590 cutting back Check If Won. 214 00:10:27,590 --> 00:10:31,700 And now the logic for these arrow keys is much simpler to read 215 00:10:31,700 --> 00:10:36,082 and it's organized more nicely into just a single block, Check If Won. 216 00:10:36,082 --> 00:10:37,790 And the program will behave the same way. 217 00:10:37,790 --> 00:10:39,450 I can try again. 218 00:10:39,450 --> 00:10:41,430 I can still move the arrow keys around. 219 00:10:41,430 --> 00:10:45,450 And the dinosaur will still report to me when it's touching the star. 220 00:10:45,450 --> 00:10:47,230 But I'm not repeating myself as much. 221 00:10:47,230 --> 00:10:51,770 So I'm using fewer blocks and being more efficient about my use of blocks. 222 00:10:51,770 --> 00:10:56,090 And it's also now easier for me to modify the behavior of this program 223 00:10:56,090 --> 00:10:57,200 too. 224 00:10:57,200 --> 00:11:00,350 If instead of saying the timer, I would now like around the timer-- 225 00:11:00,350 --> 00:11:01,970 as I talked about wanting to do-- 226 00:11:01,970 --> 00:11:04,850 instead of needing to add round to four different places for four 227 00:11:04,850 --> 00:11:08,790 different arrow keys, I can just change it in one place. 228 00:11:08,790 --> 00:11:11,420 I can say, OK, let's go to Operators. 229 00:11:11,420 --> 00:11:17,930 Let's take this Round operator and let's around the timer for two seconds. 230 00:11:17,930 --> 00:11:20,590 And since I've modified what it means to check if won, 231 00:11:20,590 --> 00:11:24,550 now all of these other arrow keys that use the Check If Won block 232 00:11:24,550 --> 00:11:28,120 will be taking advantage of this updated definition of what it 233 00:11:28,120 --> 00:11:30,880 means to check if we've won the game. 234 00:11:30,880 --> 00:11:31,810 Press the green flag. 235 00:11:31,810 --> 00:11:33,100 We'll start the game. 236 00:11:33,100 --> 00:11:35,710 I'll move the dinosaur around. 237 00:11:35,710 --> 00:11:39,730 And once I'm touching the star, you'll see that it now reports a number. 238 00:11:39,730 --> 00:11:43,030 But it's been rounded to the nearest integer. 239 00:11:43,030 --> 00:11:46,630 We're now just say six seconds instead of like 6.28 seconds or something 240 00:11:46,630 --> 00:11:48,580 like that, for example. 241 00:11:48,580 --> 00:11:51,870 So the ability to create your own block opens up a lot of doors 242 00:11:51,870 --> 00:11:55,710 for trying to improve the overall design and the overall readability 243 00:11:55,710 --> 00:11:56,910 of your project as well. 244 00:11:56,910 --> 00:11:58,660 And I'll give you another example of that. 245 00:11:58,660 --> 00:12:02,550 Let's delete the dinosaur and the star and bring back our familiar balloon 246 00:12:02,550 --> 00:12:05,760 that we've been inflating and deflating a couple of times 247 00:12:05,760 --> 00:12:07,960 throughout this course. 248 00:12:07,960 --> 00:12:13,440 And so let's try now to inflate and then deflate the balloon. 249 00:12:13,440 --> 00:12:18,270 I'll say, when the flag is clicked, what should the balloon do? 250 00:12:18,270 --> 00:12:22,960 Well, let's start by giving the balloon a size of 50%. 251 00:12:22,960 --> 00:12:24,540 Start it at half size. 252 00:12:24,540 --> 00:12:26,290 And then what I would like for the balloon 253 00:12:26,290 --> 00:12:30,610 to do in order to inflate itself is to repeatedly change its size. 254 00:12:30,610 --> 00:12:33,670 Increase the size by 10, increase the size by 10 again. 255 00:12:33,670 --> 00:12:36,290 And repeat that a couple of times. 256 00:12:36,290 --> 00:12:39,320 So here is change size by 10. 257 00:12:39,320 --> 00:12:42,160 But if I wanted to repeat that process a number of times, 258 00:12:42,160 --> 00:12:45,600 then I would go under Control and say, repeat 10 times. 259 00:12:45,600 --> 00:12:46,780 That seems reasonable. 260 00:12:46,780 --> 00:12:50,480 Let's repeat 10 times and change size by 10. 261 00:12:50,480 --> 00:12:52,330 So now, when I press the green flag, you'll 262 00:12:52,330 --> 00:12:55,600 see the balloon starts at 50% size, and then it grows-- 263 00:12:55,600 --> 00:12:58,690 it inflates very quickly up to full size. 264 00:12:58,690 --> 00:13:00,530 And now just for fun, let's go backwards. 265 00:13:00,530 --> 00:13:01,900 Let's deflate the balloon. 266 00:13:01,900 --> 00:13:04,480 We'll have our balloon wait one second. 267 00:13:04,480 --> 00:13:09,310 And then I want to deflate it-- decrease its size, which I can 268 00:13:09,310 --> 00:13:12,580 do by changing its size by negative 10. 269 00:13:12,580 --> 00:13:14,230 And I'll have to repeat that as well. 270 00:13:14,230 --> 00:13:20,280 I'll repeat that 10 times by going into Control, repeat 10 times, 271 00:13:20,280 --> 00:13:23,350 change the size by negative 10. 272 00:13:23,350 --> 00:13:25,800 So now, watch what happens when I run this balloon. 273 00:13:25,800 --> 00:13:29,230 It inflates, waits a second, and then it deflates. 274 00:13:29,230 --> 00:13:31,260 It goes back down to 50% size. 275 00:13:31,260 --> 00:13:32,190 I'll run it again. 276 00:13:32,190 --> 00:13:36,300 It inflates, it waits a second, and it deflates. 277 00:13:36,300 --> 00:13:36,992 And this works. 278 00:13:36,992 --> 00:13:38,700 This certainly achieves the effect that I 279 00:13:38,700 --> 00:13:40,680 want of increasing the size of the balloon, 280 00:13:40,680 --> 00:13:42,450 waiting, and then deflating it. 281 00:13:42,450 --> 00:13:45,017 But it might be a little bit hard for someone to read. 282 00:13:45,017 --> 00:13:47,850 If I just showed this code to someone and ask them, what does it do, 283 00:13:47,850 --> 00:13:49,590 you'd have to pay very careful attention. 284 00:13:49,590 --> 00:13:51,510 We're starting to size at 50%. 285 00:13:51,510 --> 00:13:55,210 We're repeating 10 times, changing the size by 10 each time. 286 00:13:55,210 --> 00:13:56,320 So that's 10 times 10. 287 00:13:56,320 --> 00:13:59,850 We're increasing the size by 100, waiting a second, 288 00:13:59,850 --> 00:14:01,083 and then doing it in reverse. 289 00:14:01,083 --> 00:14:03,000 You'd have to do a lot of processing, thinking 290 00:14:03,000 --> 00:14:05,208 about what are the numbers, what are the loops doing, 291 00:14:05,208 --> 00:14:07,350 how many times is everything happening, just to get 292 00:14:07,350 --> 00:14:09,540 a sense for what the project is doing. 293 00:14:09,540 --> 00:14:11,970 It's not necessarily wrong, but we might be 294 00:14:11,970 --> 00:14:16,780 able to make it a little bit easier to read as by creating some new blocks. 295 00:14:16,780 --> 00:14:20,280 These blocks right here, ultimately, the changing size by 10 over and over 296 00:14:20,280 --> 00:14:23,858 again, what they're doing is they're inflating the balloon. 297 00:14:23,858 --> 00:14:25,650 But of course, Scratch doesn't have a block 298 00:14:25,650 --> 00:14:30,392 called inflate to increase the size of the balloon 10 times. 299 00:14:30,392 --> 00:14:31,350 But we could create it. 300 00:14:31,350 --> 00:14:36,010 We could add a new block called Inflate that does exactly that. 301 00:14:36,010 --> 00:14:37,290 So let's try it. 302 00:14:37,290 --> 00:14:39,590 I'll go into my blocks. 303 00:14:39,590 --> 00:14:41,180 I'll create a new block. 304 00:14:41,180 --> 00:14:44,120 And I'll call it Inflate. 305 00:14:44,120 --> 00:14:49,040 And the definition of inflation will be this loop right here, repeat 10 times 306 00:14:49,040 --> 00:14:52,420 and change the size by 10. 307 00:14:52,420 --> 00:14:56,550 And let me create another new block called Deflate. 308 00:14:56,550 --> 00:15:01,770 And the definition of deflate will be the opposite, repeat 10 times, 309 00:15:01,770 --> 00:15:05,370 change the size by negative 10. 310 00:15:05,370 --> 00:15:07,900 And now, instead of using these repeat and change 311 00:15:07,900 --> 00:15:10,980 size by blocks inside of this script here, 312 00:15:10,980 --> 00:15:17,600 I'll just drag out my new blocks-- my Inflate block and my Deflate block. 313 00:15:17,600 --> 00:15:20,960 And now, first, notice that this program does exactly the same thing. 314 00:15:20,960 --> 00:15:24,860 The balloon increases in size, waits a second, and then decreases. 315 00:15:24,860 --> 00:15:28,490 But look how much easier to read this code now it 316 00:15:28,490 --> 00:15:31,280 is, especially right underneath this when flag clicked. 317 00:15:31,280 --> 00:15:33,680 I've simplified the logic by making it easier 318 00:15:33,680 --> 00:15:37,160 to read by taking advantage of these custom blocks, which we can now 319 00:15:37,160 --> 00:15:38,540 call abstractions. 320 00:15:38,540 --> 00:15:42,308 Abstraction is all about taking things that look very different or that are 321 00:15:42,308 --> 00:15:45,350 in different parts of your program-- like I had that IF condition in four 322 00:15:45,350 --> 00:15:46,880 different places in my program-- 323 00:15:46,880 --> 00:15:50,570 and realizing that they're all just the same idea that I could give a name to. 324 00:15:50,570 --> 00:15:53,660 And here, I can give a name to inflate and deflate. 325 00:15:53,660 --> 00:15:57,210 And now just refer to those actions by their name. 326 00:15:57,210 --> 00:16:00,530 So when the flag is clicked, we're going to set the size to 50%. 327 00:16:00,530 --> 00:16:04,520 We're going to inflate the balloon, wait one second, and deflate the balloon. 328 00:16:04,520 --> 00:16:08,970 You can just read exactly what it is that this project should do. 329 00:16:08,970 --> 00:16:11,120 And if in other places inside of my project 330 00:16:11,120 --> 00:16:14,960 I later wanted to inflate or deflate the balloon, I could do that too. 331 00:16:14,960 --> 00:16:17,180 I could add other blocks and other scripts 332 00:16:17,180 --> 00:16:20,420 and just use inflate and deflate multiple times, 333 00:16:20,420 --> 00:16:23,450 rather than need to repeat myself by saying repeat 10 times, 334 00:16:23,450 --> 00:16:27,590 change size by 10 every time that I wanted to inflate the balloon. 335 00:16:27,590 --> 00:16:29,960 So these blocks really helped to organize your code, 336 00:16:29,960 --> 00:16:32,930 help to reduce repetition and help make it clearer to someone 337 00:16:32,930 --> 00:16:36,230 reading the code what it is the code is doing. 338 00:16:36,230 --> 00:16:38,630 Now one thing is true, that these blocks are always 339 00:16:38,630 --> 00:16:40,850 going to do the same thing every time. 340 00:16:40,850 --> 00:16:43,940 They're always going to repeat 10 times and change the size 341 00:16:43,940 --> 00:16:46,640 by 10, which will have the effect of increasing 342 00:16:46,640 --> 00:16:50,450 the overall size of the balloon by 100, 10 times 10. 343 00:16:50,450 --> 00:16:52,550 But maybe I'd like a little bit more control. 344 00:16:52,550 --> 00:16:55,910 Maybe sometimes I just want to inflate the balloon a little bit, 345 00:16:55,910 --> 00:16:59,540 and other times I would like to inflate the balloon a little bit more 346 00:16:59,540 --> 00:17:01,100 or a little bit less. 347 00:17:01,100 --> 00:17:03,530 So how could I do that? 348 00:17:03,530 --> 00:17:07,960 Well, let's go ahead and Control Click or Right Click on the Inflate block 349 00:17:07,960 --> 00:17:09,763 and now edit it. 350 00:17:09,763 --> 00:17:11,430 You'll notice that I've named the block. 351 00:17:11,430 --> 00:17:14,352 And I could change the name of the block now if I wanted to. 352 00:17:14,352 --> 00:17:16,560 But when you make a block or when you edit the block, 353 00:17:16,560 --> 00:17:20,280 you also have the ability to add input to that block as well. 354 00:17:20,280 --> 00:17:22,089 This block is now a function. 355 00:17:22,089 --> 00:17:23,849 And we've discussed before that functions 356 00:17:23,849 --> 00:17:29,110 can accept inputs, information that tells that function how to behave. 357 00:17:29,110 --> 00:17:34,180 And so here, for example, I could add an input that would just be some number. 358 00:17:34,180 --> 00:17:36,720 I'll call that number n, for example, which 359 00:17:36,720 --> 00:17:39,690 is just a convention for some number and for number. 360 00:17:39,690 --> 00:17:41,280 And let's add a label. 361 00:17:41,280 --> 00:17:43,920 We're going to inflate n times. 362 00:17:43,920 --> 00:17:46,170 This is just words that will describe what 363 00:17:46,170 --> 00:17:48,690 it is the block is now going to do. 364 00:17:48,690 --> 00:17:50,310 And now I'll press, OK. 365 00:17:50,310 --> 00:17:53,880 And what you'll notice now is that the Inflate block is now changed. 366 00:17:53,880 --> 00:17:57,540 It's now Inflate and then a blank value times. 367 00:17:57,540 --> 00:18:01,200 And that blank volume looks much like the blink value and change size 368 00:18:01,200 --> 00:18:04,590 by or move some number of steps, for example. 369 00:18:04,590 --> 00:18:10,580 This is now a place where I can provide input into this function 370 00:18:10,580 --> 00:18:12,260 to tell it how to behave. 371 00:18:12,260 --> 00:18:14,960 And you'll notice now that under Define Inflate, 372 00:18:14,960 --> 00:18:18,470 it doesn't just say Define Inflate, but Define Inflate n times, 373 00:18:18,470 --> 00:18:22,730 where I have access to this block here called n. 374 00:18:22,730 --> 00:18:28,400 So what I'm going to do is take n and drag it inside of this repeat. 375 00:18:28,400 --> 00:18:33,690 Instead of repeating 10 times every time, let's repeat n times every time. 376 00:18:33,690 --> 00:18:38,690 Whatever n is, where n is going to be the input to this function. 377 00:18:38,690 --> 00:18:40,550 And I'll do the same thing with Deflate. 378 00:18:40,550 --> 00:18:41,930 I'll edit it. 379 00:18:41,930 --> 00:18:47,700 And it will be deflate n and then add a label times. 380 00:18:47,700 --> 00:18:49,500 And I'll say, OK to that. 381 00:18:49,500 --> 00:18:52,560 And now instead of repeating 10 times and deflate, 382 00:18:52,560 --> 00:18:56,590 let's repeat n times as well. 383 00:18:56,590 --> 00:18:59,100 Now these functions have the ability to take some input. 384 00:18:59,100 --> 00:19:02,130 And you'll notice down below under the Inflate block, 385 00:19:02,130 --> 00:19:03,880 I need to type a number into this space. 386 00:19:03,880 --> 00:19:07,830 So I can say inflate 10 times and then deflate 10 times. 387 00:19:07,830 --> 00:19:10,230 And now, if I run the project, it works the same way. 388 00:19:10,230 --> 00:19:12,450 It inflates and then it deflates. 389 00:19:12,450 --> 00:19:17,490 But if I just change these inputs, inflate 20 times and deflate 20 times, 390 00:19:17,490 --> 00:19:20,610 well, now the project's going to behave differently. 391 00:19:20,610 --> 00:19:23,190 Inflate and deflate. 392 00:19:23,190 --> 00:19:25,200 It gets much bigger and then much smaller, 393 00:19:25,200 --> 00:19:29,050 because I'm inflating and deflating it more often. 394 00:19:29,050 --> 00:19:30,970 And then I'll change these back to 10 for now 395 00:19:30,970 --> 00:19:32,470 so we go back to the usual behavior. 396 00:19:32,470 --> 00:19:34,310 Inflate and then deflate. 397 00:19:34,310 --> 00:19:36,490 But I could use these blocks in other places too. 398 00:19:36,490 --> 00:19:39,340 I could say, for example, that any time I 399 00:19:39,340 --> 00:19:42,880 click on the balloon, when this sprite clicked, well, let's 400 00:19:42,880 --> 00:19:47,860 like clicking inflate the balloon two times. 401 00:19:47,860 --> 00:19:49,690 So not 10, a little bit fewer. 402 00:19:49,690 --> 00:19:51,910 So I press the flag and it inflates a lot. 403 00:19:51,910 --> 00:19:54,280 Inflates by 10 and then deflates. 404 00:19:54,280 --> 00:19:58,090 But every time I click on the balloon, it's just going to inflate by a little. 405 00:19:58,090 --> 00:20:00,610 It's going to inflate two times. 406 00:20:00,610 --> 00:20:02,770 And we were able to achieve all of that just 407 00:20:02,770 --> 00:20:05,230 by giving our functions some inputs. 408 00:20:05,230 --> 00:20:08,860 We defined and inflate block, gave it an input n, 409 00:20:08,860 --> 00:20:13,000 and then use that n inside of the definition of the block. 410 00:20:13,000 --> 00:20:15,910 When we run the block, we're going to look at what is the value of n. 411 00:20:15,910 --> 00:20:19,030 And that's going to tell us how many times to repeat, such 412 00:20:19,030 --> 00:20:23,080 that now when we use that block, I can decide how much to inflate 413 00:20:23,080 --> 00:20:25,700 the balloon by just by saying, well, in this case, 414 00:20:25,700 --> 00:20:27,520 I want to inflate it 10 times. 415 00:20:27,520 --> 00:20:31,760 But down here, I only want to inflate it two times for example. 416 00:20:31,760 --> 00:20:36,770 So you can make your blocks very customizable, very flexible as well. 417 00:20:36,770 --> 00:20:39,850 And these blocks don't just need to take one input. 418 00:20:39,850 --> 00:20:42,620 They could also take multiple inputs as well. 419 00:20:42,620 --> 00:20:44,630 So let's see an example of that. 420 00:20:44,630 --> 00:20:49,870 Let's go back to our bear, which we've had around before. 421 00:20:49,870 --> 00:20:52,053 And you might recall that before, we've had a walk 422 00:20:52,053 --> 00:20:53,470 in a couple of different costumes. 423 00:20:53,470 --> 00:20:56,410 If you go to Costumes, you see that we have this costume where 424 00:20:56,410 --> 00:20:57,915 the bear is on four legs. 425 00:20:57,915 --> 00:21:01,340 And I'll go ahead and call that costume 4. 426 00:21:01,340 --> 00:21:04,010 And here, the bear is on two legs. 427 00:21:04,010 --> 00:21:05,720 And I'll rename that costume-- 428 00:21:05,720 --> 00:21:08,270 up in the name section, I'll call it 2. 429 00:21:08,270 --> 00:21:11,960 So I have a costume called 4 and a costume called 2. 430 00:21:11,960 --> 00:21:14,840 I just renamed those two default costumes. 431 00:21:14,840 --> 00:21:16,850 But now let me make a new block. 432 00:21:16,850 --> 00:21:18,950 And here's what we'll call this block. 433 00:21:18,950 --> 00:21:21,950 The block will be Walk. 434 00:21:21,950 --> 00:21:24,750 Then I'll add an input, which will be a number. 435 00:21:24,750 --> 00:21:27,070 And I'm going to call it m for now. 436 00:21:27,070 --> 00:21:28,570 You can call this whatever you want. 437 00:21:28,570 --> 00:21:30,420 This is just a name of an input. 438 00:21:30,420 --> 00:21:36,390 And it's going to be a Walk m, and then I'll add some next, steps. 439 00:21:36,390 --> 00:21:39,870 And then I'm going to say, on, and then I'm going to add a second input-- 440 00:21:39,870 --> 00:21:42,510 a second input that I'll call n. 441 00:21:42,510 --> 00:21:46,440 And then I'll add some text, feet. 442 00:21:46,440 --> 00:21:49,350 So I added two inputs and two additional labels. 443 00:21:49,350 --> 00:21:55,630 But this block is now called Walk m Steps on n Feet. 444 00:21:55,630 --> 00:21:57,480 So what this is going to do is it's going 445 00:21:57,480 --> 00:22:01,810 to control how far the bear is going to walk on a certain number of feet. 446 00:22:01,810 --> 00:22:03,930 So what do I do about the number of feet? 447 00:22:03,930 --> 00:22:04,920 Well, that's a costume. 448 00:22:04,920 --> 00:22:08,220 There's a costume for four feet and a costume for two feet. 449 00:22:08,220 --> 00:22:11,970 So let's switch the costume to m or n. 450 00:22:11,970 --> 00:22:14,610 Well, n is the number of feet, and that's the costume. 451 00:22:14,610 --> 00:22:15,810 So n is going to go here. 452 00:22:15,810 --> 00:22:19,920 We're going to switch to the costume to n, representing how many feet should 453 00:22:19,920 --> 00:22:21,540 the bear now be walking on. 454 00:22:21,540 --> 00:22:24,090 And now let's walk m steps. 455 00:22:24,090 --> 00:22:28,450 Well, I can walk one step just by saying, move one step. 456 00:22:28,450 --> 00:22:32,350 And if I want to do that multiple times, I can put that in a repeat block. 457 00:22:32,350 --> 00:22:36,540 So let's go under Control and repeat the process of moving one step. 458 00:22:36,540 --> 00:22:43,800 But instead of repeating 10 times every time, let's instead repeat m times. 459 00:22:43,800 --> 00:22:46,890 So this then is our definition of what does it mean 460 00:22:46,890 --> 00:22:50,640 for the bear to walk m steps on n feet. 461 00:22:50,640 --> 00:22:53,550 We first switched the costume to n, where is the number of feet. 462 00:22:53,550 --> 00:22:56,130 n is either going to be four or two to determine 463 00:22:56,130 --> 00:22:58,170 which costume we should switch to. 464 00:22:58,170 --> 00:23:00,700 And then we're walking m steps. 465 00:23:00,700 --> 00:23:04,920 So let's repeat m times this move one step block. 466 00:23:04,920 --> 00:23:07,920 We've organized the logic for what it means for the bear 467 00:23:07,920 --> 00:23:09,990 to walk on a certain number of feet. 468 00:23:09,990 --> 00:23:13,500 And now I can use that block inside of my project. 469 00:23:13,500 --> 00:23:18,270 I can say that whenever the flag is clicked, what should the bear do? 470 00:23:18,270 --> 00:23:19,920 Well, let's first have the block-- 471 00:23:19,920 --> 00:23:25,650 or have the bear walk 30 steps on four feet 472 00:23:25,650 --> 00:23:32,450 and then walk 30 more steps on two feet. 473 00:23:32,450 --> 00:23:36,590 So it's going to start on four feet, walk for 30 steps, then go to two feet, 474 00:23:36,590 --> 00:23:37,400 walk 30 steps. 475 00:23:37,400 --> 00:23:38,650 And we can watch what happens. 476 00:23:38,650 --> 00:23:39,560 I'll press the flag. 477 00:23:39,560 --> 00:23:42,335 It walks on four feet, and then on two. 478 00:23:42,335 --> 00:23:45,500 I'll try it again and it does the same thing. 479 00:23:45,500 --> 00:23:49,410 And this code right underneath the When Flag Clicked is now very easy to read. 480 00:23:49,410 --> 00:23:52,370 I can very easily see exactly what the bear is going to do. 481 00:23:52,370 --> 00:23:54,530 And it might have been harder to see that if I 482 00:23:54,530 --> 00:23:56,900 was using all of these every single time, 483 00:23:56,900 --> 00:23:59,480 switch costume and repeat this movement process. 484 00:23:59,480 --> 00:24:01,640 And I can use this block now multiple times, 485 00:24:01,640 --> 00:24:06,120 as I have here, to avoid repeating myself as well. 486 00:24:06,120 --> 00:24:08,360 So Scratch offers a number of different blocks 487 00:24:08,360 --> 00:24:11,210 that you can use to achieve a variety of different things inside 488 00:24:11,210 --> 00:24:12,050 of your project. 489 00:24:12,050 --> 00:24:14,780 But we now have the ability to add our own blocks. 490 00:24:14,780 --> 00:24:17,900 If there's additional behavior that we want our sprites 491 00:24:17,900 --> 00:24:20,090 to have inside of our Scratch projects, we 492 00:24:20,090 --> 00:24:24,530 can create our own blocks to define some custom behaviors that we can use inside 493 00:24:24,530 --> 00:24:26,690 of our project to make them more complex, 494 00:24:26,690 --> 00:24:30,510 more interesting, and ultimately, better organized and more exciting as well. 495 00:24:30,510 --> 00:24:33,510 That's it for an introduction to Programming with Scratch for today. 496 00:24:33,510 --> 00:24:35,560 We'll see you next time. 497 00:24:35,560 --> 00:24:37,000