1 00:00:00,000 --> 00:00:04,960 [MUSIC PLAYING] 2 00:00:04,960 --> 00:00:17,033 3 00:00:17,033 --> 00:00:19,200 BRIAN YU: Welcome back, everyone, to An Introduction 4 00:00:19,200 --> 00:00:20,430 to Programming with Scratch. 5 00:00:20,430 --> 00:00:23,220 And so far, in Scratch, we've seen our Scratch programs 6 00:00:23,220 --> 00:00:25,350 use a variety of different values. 7 00:00:25,350 --> 00:00:28,890 Values that keep track of what direction a sprite is pointing in, or how big, 8 00:00:28,890 --> 00:00:30,420 or how small the sprite is. 9 00:00:30,420 --> 00:00:31,930 Where it is, for example. 10 00:00:31,930 --> 00:00:35,940 And these are all pieces of information stored inside of our Scratch program 11 00:00:35,940 --> 00:00:39,210 that we can use later in our program to make decisions, 12 00:00:39,210 --> 00:00:42,150 or to make our program behave in different ways. 13 00:00:42,150 --> 00:00:45,070 But sometimes, as our programs get a little bit more complex, 14 00:00:45,070 --> 00:00:48,540 we will want to store other pieces of information inside of our program 15 00:00:48,540 --> 00:00:52,320 as well where we decide what information gets stored in our program, 16 00:00:52,320 --> 00:00:55,320 and how we use that information later on. 17 00:00:55,320 --> 00:00:58,680 And we're going to start to call these variables in the world of programming. 18 00:00:58,680 --> 00:01:01,140 And today, we'll explore variables in Scratch, 19 00:01:01,140 --> 00:01:03,460 and how it is that we can use them. 20 00:01:03,460 --> 00:01:06,142 So I'll go ahead and open up Scratch here as we have before. 21 00:01:06,142 --> 00:01:08,100 And there's one tab here that we haven't looked 22 00:01:08,100 --> 00:01:11,220 at yet, which is this Variables tab. 23 00:01:11,220 --> 00:01:14,220 And a variable you can think of as just some value. 24 00:01:14,220 --> 00:01:16,710 Some container inside of our Scratch program 25 00:01:16,710 --> 00:01:18,990 that's going to store some information. 26 00:01:18,990 --> 00:01:24,130 Information that we might be able to use later inside of our program as well. 27 00:01:24,130 --> 00:01:26,310 And so, for example, let's imagine I was trying 28 00:01:26,310 --> 00:01:31,740 to create a program that kept track of how many times I've clicked on the cat. 29 00:01:31,740 --> 00:01:34,350 So to do that, I want some piece of information, 30 00:01:34,350 --> 00:01:37,320 some container inside of my Scratch program, that's keeping 31 00:01:37,320 --> 00:01:40,500 track of how many times I've clicked on the cat. 32 00:01:40,500 --> 00:01:43,800 And to do that, I'll click on this Make a Variable button 33 00:01:43,800 --> 00:01:47,190 that will let me create a new variable, a new piece of information, 34 00:01:47,190 --> 00:01:49,598 that I would like to store inside of my program. 35 00:01:49,598 --> 00:01:51,390 And when I create a new variable, there are 36 00:01:51,390 --> 00:01:53,700 two things that I need to decide on. 37 00:01:53,700 --> 00:01:55,650 One is the name of that variable. 38 00:01:55,650 --> 00:01:58,590 And I'll call this, count, for example because I'm 39 00:01:58,590 --> 00:02:02,190 going to be counting how many times I've clicked on the cat. 40 00:02:02,190 --> 00:02:06,570 And the second decision I need to make is is this variable for all sprites, 41 00:02:06,570 --> 00:02:08,310 or for this sprite only? 42 00:02:08,310 --> 00:02:10,479 So there are two different types of variables. 43 00:02:10,479 --> 00:02:13,110 If I have a variable that's for all sprites, 44 00:02:13,110 --> 00:02:17,580 that means that any sprite is allowed to use or modify that variable. 45 00:02:17,580 --> 00:02:19,440 We often call this a global variable. 46 00:02:19,440 --> 00:02:21,870 Everyone can use that variable. 47 00:02:21,870 --> 00:02:24,420 Or we can have a variable that's for this sprite only. 48 00:02:24,420 --> 00:02:25,560 That's only for the cat. 49 00:02:25,560 --> 00:02:27,750 Nobody else is going to get to touch it. 50 00:02:27,750 --> 00:02:29,790 That's what we often call a local variable. 51 00:02:29,790 --> 00:02:32,550 Other sprites don't have the ability to use it. 52 00:02:32,550 --> 00:02:34,840 For now, we'll go ahead and use for all spites only. 53 00:02:34,840 --> 00:02:36,520 And we'll see why in just a moment. 54 00:02:36,520 --> 00:02:39,540 And that is the default. When you normally create a variable in Scratch, 55 00:02:39,540 --> 00:02:43,600 any sprite is going to have the ability to use it. 56 00:02:43,600 --> 00:02:46,200 So I've created a new variable called, count. 57 00:02:46,200 --> 00:02:48,960 And you'll notice that by default it's checked here. 58 00:02:48,960 --> 00:02:51,490 You see the little mark on the left hand side. 59 00:02:51,490 --> 00:02:54,270 And what you might have noticed is that on the stage 60 00:02:54,270 --> 00:02:58,260 we see something appear in the upper left portion of the stage that's 61 00:02:58,260 --> 00:03:01,800 just reporting to me what the current value of the count is. 62 00:03:01,800 --> 00:03:05,880 What information is stored inside of that container. 63 00:03:05,880 --> 00:03:09,360 And right now, I see that's stored inside of my count variable 64 00:03:09,360 --> 00:03:10,710 is the number 0. 65 00:03:10,710 --> 00:03:13,090 That's what happens to be there to begin with. 66 00:03:13,090 --> 00:03:15,090 If I don't want to see that, I could uncheck it. 67 00:03:15,090 --> 00:03:19,320 And then I wouldn't see the count appear on the stage at all. 68 00:03:19,320 --> 00:03:21,570 But here's what I want to have happen. 69 00:03:21,570 --> 00:03:23,590 Every time the cat is clicked-- 70 00:03:23,590 --> 00:03:26,370 so when this sprite clicked, that's the event that I want-- 71 00:03:26,370 --> 00:03:31,800 I'm going to go into variables and change the count by one. 72 00:03:31,800 --> 00:03:34,440 Change count by one means take that variable count 73 00:03:34,440 --> 00:03:37,170 and add 1 to whatever number is currently 74 00:03:37,170 --> 00:03:41,070 being stored inside of that variable. 75 00:03:41,070 --> 00:03:45,510 And then let's not say, hello, for two seconds. 76 00:03:45,510 --> 00:03:47,640 But whenever you create a variable, you suddenly 77 00:03:47,640 --> 00:03:49,770 get this variable block that appears here 78 00:03:49,770 --> 00:03:52,330 that you can drag into your program. 79 00:03:52,330 --> 00:03:54,270 Let's say the count for-- 80 00:03:54,270 --> 00:03:54,960 not two seconds. 81 00:03:54,960 --> 00:03:57,610 Let's say for one second, for example. 82 00:03:57,610 --> 00:04:01,260 So now, when I click on the cat, the cat says one. 83 00:04:01,260 --> 00:04:04,020 Click again, the cat says two. 84 00:04:04,020 --> 00:04:05,230 Now it says three. 85 00:04:05,230 --> 00:04:10,020 Every time I click on it, we're updating the value of that count variable. 86 00:04:10,020 --> 00:04:13,112 And the cat is saying whatever its value is. 87 00:04:13,112 --> 00:04:14,820 And I can check this count checkbox to be 88 00:04:14,820 --> 00:04:19,529 able to see right now count happens to be equal to the number seven. 89 00:04:19,529 --> 00:04:21,360 And when I click the count Again, now you 90 00:04:21,360 --> 00:04:24,360 see the count update to be the number eight. 91 00:04:24,360 --> 00:04:28,320 And so these variables give you the ability to keep track of information. 92 00:04:28,320 --> 00:04:30,880 And you might keep track of information in a number of ways. 93 00:04:30,880 --> 00:04:33,490 And we'll explore some of those ways today. 94 00:04:33,490 --> 00:04:36,780 But right now, every time I click on the cat, the count is going up, and up, 95 00:04:36,780 --> 00:04:37,320 and up. 96 00:04:37,320 --> 00:04:40,050 And there is no way for me to reset this thing. 97 00:04:40,050 --> 00:04:43,350 If I keep pressing the count, the count is going to keep going up. 98 00:04:43,350 --> 00:04:45,330 But it's never going back to 0. 99 00:04:45,330 --> 00:04:48,240 So let's add the ability to reset my program. 100 00:04:48,240 --> 00:04:52,620 Maybe a button that lets me reset the count back to 0. 101 00:04:52,620 --> 00:04:53,940 And how would that work? 102 00:04:53,940 --> 00:04:57,180 Well, a button is going to be a new sprite. 103 00:04:57,180 --> 00:05:01,040 So I go down below, and choose a new sprite to add. 104 00:05:01,040 --> 00:05:04,760 And let's add this button here. 105 00:05:04,760 --> 00:05:07,010 I'll drag it to the corner. 106 00:05:07,010 --> 00:05:09,220 And I want to change what the button says. 107 00:05:09,220 --> 00:05:13,190 So we'll go into costumes, and let's add some text. 108 00:05:13,190 --> 00:05:17,960 And the text is going to say, "Reset." 109 00:05:17,960 --> 00:05:21,200 It should change the color of that text. 110 00:05:21,200 --> 00:05:24,390 We;ll make it black text. 111 00:05:24,390 --> 00:05:25,890 And let's make it a little larger. 112 00:05:25,890 --> 00:05:30,100 113 00:05:30,100 --> 00:05:33,720 So now I have a button to reset the count. 114 00:05:33,720 --> 00:05:37,950 And now what do I want to have happen when this sprite is clicked on? 115 00:05:37,950 --> 00:05:39,780 When the reset button is clicked? 116 00:05:39,780 --> 00:05:45,920 Let's go into variables, and let's set the count to 0. 117 00:05:45,920 --> 00:05:48,740 And so because this variable I said should 118 00:05:48,740 --> 00:05:52,440 be accessible by all of the sprite, it's a global variable, 119 00:05:52,440 --> 00:05:55,910 it means both the cat sprite and the button sprite 120 00:05:55,910 --> 00:05:57,710 are allowed to use that variable. 121 00:05:57,710 --> 00:06:01,620 They can both make reference to that count variable. 122 00:06:01,620 --> 00:06:04,160 And here, I'm using set count to 0. 123 00:06:04,160 --> 00:06:08,420 Meaning take the number 0, and put that inside of the container. 124 00:06:08,420 --> 00:06:12,530 That's different from change count by, which adds or subtracts 125 00:06:12,530 --> 00:06:15,440 a number from one particular variable. 126 00:06:15,440 --> 00:06:19,610 Here, I'm just saying whatever value the count has now, ignore that and instead 127 00:06:19,610 --> 00:06:24,010 put the value 0 inside of the count instead. 128 00:06:24,010 --> 00:06:28,990 And so now at this point, I can keep clicking on the cat. 129 00:06:28,990 --> 00:06:31,840 Count goes up to 9, and then to 10. 130 00:06:31,840 --> 00:06:34,460 But watch what happens when I click the Reset button. 131 00:06:34,460 --> 00:06:38,320 When I click Reset, you'll notice the count goes back to 0. 132 00:06:38,320 --> 00:06:41,170 And now the next time that I click on the cat, now the cat 133 00:06:41,170 --> 00:06:45,830 is going to say one, and then two, and then three. 134 00:06:45,830 --> 00:06:49,680 So these variables now give us the ability to keep track of information. 135 00:06:49,680 --> 00:06:50,990 But what can we use that for? 136 00:06:50,990 --> 00:06:52,640 How is that helpful to us? 137 00:06:52,640 --> 00:06:55,340 Well, you might imagine that we could use this inside of a game. 138 00:06:55,340 --> 00:06:58,722 In many games you might have played, for example, on a phone or on a computer, 139 00:06:58,722 --> 00:07:00,680 you might have had the game keep score for you. 140 00:07:00,680 --> 00:07:04,170 It's keeping track of how many points you've earned in the game for example. 141 00:07:04,170 --> 00:07:06,470 And it goes up, and up, and up as you earn points. 142 00:07:06,470 --> 00:07:09,110 We can achieve that by using a variable. 143 00:07:09,110 --> 00:07:12,830 We can have a variable that's keeping track of what score we have in a game. 144 00:07:12,830 --> 00:07:16,860 And we update that variable as the score changes. 145 00:07:16,860 --> 00:07:17,840 So let's build a game. 146 00:07:17,840 --> 00:07:20,660 I'm going to delete these sprites for now. 147 00:07:20,660 --> 00:07:22,940 And I'm also going to delete the count variable, which 148 00:07:22,940 --> 00:07:24,980 I can do by control clicking on Count. 149 00:07:24,980 --> 00:07:27,690 And just saying delete the count variable. 150 00:07:27,690 --> 00:07:29,270 Now that goes away. 151 00:07:29,270 --> 00:07:34,590 Let's create a game using the hedgehog. 152 00:07:34,590 --> 00:07:36,270 So here's our hedgehog. 153 00:07:36,270 --> 00:07:40,500 And I'll do something similar to what I did with the fish last time. 154 00:07:40,500 --> 00:07:43,380 I'll say when the green flag is clicked, let's 155 00:07:43,380 --> 00:07:50,940 have it forever point towards the mouse pointer, and move five steps. 156 00:07:50,940 --> 00:07:52,680 We've seen this kind of code before. 157 00:07:52,680 --> 00:07:55,290 What this is going to do is when I press the green flag, 158 00:07:55,290 --> 00:07:57,000 the hedgehog is going to start moving. 159 00:07:57,000 --> 00:08:00,480 And it's just constantly going to be following the cursor forever. 160 00:08:00,480 --> 00:08:02,820 It's going to point towards my mouse cursor, 161 00:08:02,820 --> 00:08:07,290 and then it's going to move five steps towards me. 162 00:08:07,290 --> 00:08:08,498 So that works well. 163 00:08:08,498 --> 00:08:09,790 But let's now make this a game. 164 00:08:09,790 --> 00:08:11,340 What is the hedgehog trying to do? 165 00:08:11,340 --> 00:08:13,390 Well, maybe it's trying to chase after something. 166 00:08:13,390 --> 00:08:15,210 What's it going to chase after? 167 00:08:15,210 --> 00:08:17,280 Let me choose a new sprite. 168 00:08:17,280 --> 00:08:23,740 Let's go ahead and pick the star. 169 00:08:23,740 --> 00:08:28,100 We'll have the hedgehog chasing after this star here. 170 00:08:28,100 --> 00:08:30,410 And how is the star going to work? 171 00:08:30,410 --> 00:08:34,429 Well, the star is going to work where if the hedgehog is ever touching the star, 172 00:08:34,429 --> 00:08:37,070 then we're going to increase our score by one. 173 00:08:37,070 --> 00:08:39,289 We're keeping track of how many points we've earned. 174 00:08:39,289 --> 00:08:41,240 So to do that, I need a new variable. 175 00:08:41,240 --> 00:08:46,010 I'll go into Variables, I'll make a new variable that I will call Score. 176 00:08:46,010 --> 00:08:47,120 And press OK. 177 00:08:47,120 --> 00:08:50,810 So I now have this new variable called Score. 178 00:08:50,810 --> 00:08:55,230 And for the star I'm going to say, when the green flag is clicked, 179 00:08:55,230 --> 00:09:01,690 I want the star to forever be checking if we're touching the hedgehog. 180 00:09:01,690 --> 00:09:03,280 Side add an if statement. 181 00:09:03,280 --> 00:09:07,760 And then under sensing, check not if we're touching the mouse pointer, 182 00:09:07,760 --> 00:09:10,430 but if we're touching the hedgehog. 183 00:09:10,430 --> 00:09:13,430 So the star is going to constantly check if we're touching the hedgehog. 184 00:09:13,430 --> 00:09:17,120 And if we are touching the hedgehog, then under variables, 185 00:09:17,120 --> 00:09:19,598 I'm going to change not my variable, which 186 00:09:19,598 --> 00:09:21,890 is a variable that's created for you automatically when 187 00:09:21,890 --> 00:09:28,000 you first create your Scratch program, but I want to change the score by one. 188 00:09:28,000 --> 00:09:33,430 And after you change the score by one, let's go to a random position. 189 00:09:33,430 --> 00:09:35,890 To keep things interesting, the star will just move around 190 00:09:35,890 --> 00:09:39,390 after we increase the score by one. 191 00:09:39,390 --> 00:09:41,640 So now we can try playing the game. 192 00:09:41,640 --> 00:09:42,840 I'll play the game. 193 00:09:42,840 --> 00:09:44,790 The hedgehog's following me around. 194 00:09:44,790 --> 00:09:49,560 And watch what happens when I end up going to the star. 195 00:09:49,560 --> 00:09:52,770 You'll notice that immediately the score went up by one. 196 00:09:52,770 --> 00:09:56,910 And you'll also notice that the star moved to a different random location. 197 00:09:56,910 --> 00:09:59,610 Now when I get to the star again, the score goes up to two. 198 00:09:59,610 --> 00:10:01,320 And the star moves. 199 00:10:01,320 --> 00:10:02,820 And I have a little game here. 200 00:10:02,820 --> 00:10:10,010 Every time I move around, we end up going to the star 201 00:10:10,010 --> 00:10:13,220 and increasing the number of points. 202 00:10:13,220 --> 00:10:14,650 So what do I want to add to this? 203 00:10:14,650 --> 00:10:16,580 What should I change about this? 204 00:10:16,580 --> 00:10:18,670 Well, one thing I should probably change is 205 00:10:18,670 --> 00:10:20,890 that when the program begins, when I press 206 00:10:20,890 --> 00:10:23,560 the green flag for the very first time, I probably 207 00:10:23,560 --> 00:10:25,900 want to reset the score back to 0. 208 00:10:25,900 --> 00:10:29,260 In most games you play when you play it again, the score goes back to 0, 209 00:10:29,260 --> 00:10:32,000 and you have to start earning points from the beginning again. 210 00:10:32,000 --> 00:10:34,450 So I can say when the green flag is clicked, 211 00:10:34,450 --> 00:10:40,090 before the forever loop happens I want to set the score to 0. 212 00:10:40,090 --> 00:10:43,160 Notice this line should not be inside of the forever loop. 213 00:10:43,160 --> 00:10:46,300 Otherwise, it's going to forever be resetting the score back to 0, 214 00:10:46,300 --> 00:10:47,540 which is not what I want. 215 00:10:47,540 --> 00:10:51,040 I want the score to be set to 0 at the very beginning of the program. 216 00:10:51,040 --> 00:10:54,670 But after that, then I want to go into this forever loop 217 00:10:54,670 --> 00:10:56,830 where the hedgehog is going to be following me. 218 00:10:56,830 --> 00:10:59,980 So now the score goes back to 0 when I press the green flag. 219 00:10:59,980 --> 00:11:04,650 And I can start earning points again. 220 00:11:04,650 --> 00:11:06,002 Now how do I win this game? 221 00:11:06,002 --> 00:11:08,460 Now I'm just earning points and nothing's really happening. 222 00:11:08,460 --> 00:11:12,082 Well, maybe I win the game once I get to a certain number of points. 223 00:11:12,082 --> 00:11:13,290 That sounds like a condition. 224 00:11:13,290 --> 00:11:17,310 Once I get to a situation where I have maybe 10 points, 225 00:11:17,310 --> 00:11:19,980 we can call that winning the game. 226 00:11:19,980 --> 00:11:21,570 And what happens when I win the game? 227 00:11:21,570 --> 00:11:22,920 I have to decide. 228 00:11:22,920 --> 00:11:25,110 Maybe the backdrop changes. 229 00:11:25,110 --> 00:11:26,760 I can take a backdrop. 230 00:11:26,760 --> 00:11:29,250 Add a new backdrop. 231 00:11:29,250 --> 00:11:31,680 Well, instead of picking an existing backdrop actually, 232 00:11:31,680 --> 00:11:33,860 let me paint a new backdrop. 233 00:11:33,860 --> 00:11:35,330 Paint a new backdrop. 234 00:11:35,330 --> 00:11:41,880 We'll go ahead and just create a green background 235 00:11:41,880 --> 00:11:44,960 by drawing a green rectangle. 236 00:11:44,960 --> 00:11:47,030 And we'll add some text. 237 00:11:47,030 --> 00:11:48,350 The text will say, you win. 238 00:11:48,350 --> 00:11:50,850 I need to change the color of that. 239 00:11:50,850 --> 00:11:53,310 Make it black. 240 00:11:53,310 --> 00:11:54,510 And make the text bigger. 241 00:11:54,510 --> 00:11:58,420 242 00:11:58,420 --> 00:12:01,090 So that'll be the backdrop that shows up when we win. 243 00:12:01,090 --> 00:12:02,780 I'll give you the backdrop a name. 244 00:12:02,780 --> 00:12:05,200 I'll call it Win. . 245 00:12:05,200 --> 00:12:08,450 And the other backdrop is just backdrop one for now. 246 00:12:08,450 --> 00:12:10,480 And so what do I need to change here? 247 00:12:10,480 --> 00:12:14,140 Well, when the program first starts, I want to set the score to 0. 248 00:12:14,140 --> 00:12:19,640 But I also want to make sure that the backdrop is backdrop one. 249 00:12:19,640 --> 00:12:23,080 The default plain white backdrop to begin with. 250 00:12:23,080 --> 00:12:25,830 But now let's add some code. 251 00:12:25,830 --> 00:12:31,630 And I would like to say maybe when we change 252 00:12:31,630 --> 00:12:36,040 the score I want to ask the question, have we won the game yet? 253 00:12:36,040 --> 00:12:43,730 I can add another condition by going to if here after we change the score. 254 00:12:43,730 --> 00:12:46,940 And random position that's going to be outside of this if statement. 255 00:12:46,940 --> 00:12:53,000 But if the score is equal to 10-- 256 00:12:53,000 --> 00:12:54,190 so something is equal to 10. 257 00:12:54,190 --> 00:12:55,060 And how do I get the score? 258 00:12:55,060 --> 00:12:55,970 That's a variable. 259 00:12:55,970 --> 00:12:57,890 So I go into variables. 260 00:12:57,890 --> 00:13:01,880 If the score is equal to 10, well, then I want to display this backdrop. 261 00:13:01,880 --> 00:13:05,230 The backdrop that says that I've now won the game. 262 00:13:05,230 --> 00:13:06,650 So we'll go into looks. 263 00:13:06,650 --> 00:13:08,880 We'll switch the backdrop to Win. 264 00:13:08,880 --> 00:13:12,230 But at this point, I also just want to stop the entire game. 265 00:13:12,230 --> 00:13:13,980 I don't want the hedgehog to move anymore. 266 00:13:13,980 --> 00:13:16,670 I don't want to have the star keep going to random positions. 267 00:13:16,670 --> 00:13:19,820 And it turns out that under control, there's one block we haven't used yet 268 00:13:19,820 --> 00:13:23,120 which is just called stop all that has the effect of stopping 269 00:13:23,120 --> 00:13:27,260 all of the sprites that are in this program. 270 00:13:27,260 --> 00:13:28,962 So we'll switch the backdrop to Win. 271 00:13:28,962 --> 00:13:30,170 And then we'll just stop all. 272 00:13:30,170 --> 00:13:32,500 We'll stop everything. 273 00:13:32,500 --> 00:13:34,500 So this program's getting a little more complex. 274 00:13:34,500 --> 00:13:36,542 But let's try and understand now what's happening 275 00:13:36,542 --> 00:13:38,360 inside the logic for the star. 276 00:13:38,360 --> 00:13:41,540 We're constantly going to be checking are we touching the hedgehog. 277 00:13:41,540 --> 00:13:44,550 If we're touching the hedgehog, we change the score by one. 278 00:13:44,550 --> 00:13:47,170 And if now the score is 10, well, now we've won the game. 279 00:13:47,170 --> 00:13:50,730 So we switch the backdrop, and then we stop all the sprites. 280 00:13:50,730 --> 00:13:54,860 But otherwise, the star just goes to a new random position 281 00:13:54,860 --> 00:13:58,100 to let the game keep going. 282 00:13:58,100 --> 00:14:02,200 So we can try the game now, and see what happens when I get to 10 points. 283 00:14:02,200 --> 00:14:03,790 I'll press the flag. 284 00:14:03,790 --> 00:14:04,660 The scores of 0. 285 00:14:04,660 --> 00:14:07,630 The backdrops gone back to this plain white background. 286 00:14:07,630 --> 00:14:10,810 And now every time I touch the star, it goes to a new random position. 287 00:14:10,810 --> 00:14:13,810 And you can watch my score in the upper left increase. 288 00:14:13,810 --> 00:14:17,740 I'm at 6.7 points, 8 points, 9 points. 289 00:14:17,740 --> 00:14:20,050 And watch what happens now as I get the 10th star. 290 00:14:20,050 --> 00:14:22,760 291 00:14:22,760 --> 00:14:24,020 Everything freezes. 292 00:14:24,020 --> 00:14:25,760 The hedgehog's no longer following me. 293 00:14:25,760 --> 00:14:28,970 But we change the backdrop to this green backdrop 294 00:14:28,970 --> 00:14:31,850 that now just says that we win. 295 00:14:31,850 --> 00:14:35,210 So one common use of variables is to keep track of your score 296 00:14:35,210 --> 00:14:38,840 in a game in something like this for example. 297 00:14:38,840 --> 00:14:41,840 Let's take a look at another use that we might use for variables. 298 00:14:41,840 --> 00:14:44,330 I'll go ahead and get rid of these sprites for now. 299 00:14:44,330 --> 00:14:46,130 And I'll get rid of the you win backdrop. 300 00:14:46,130 --> 00:14:49,670 And we'll just go back to the plain white backdrop. 301 00:14:49,670 --> 00:14:55,110 And I'll also delete that score variable, which I don't want any more. 302 00:14:55,110 --> 00:14:56,640 Let's choose a new backdrop. 303 00:14:56,640 --> 00:14:59,040 Let's choose Blue Sky, which is nice. 304 00:14:59,040 --> 00:15:01,510 We've got the sky and we've got the ground for example. 305 00:15:01,510 --> 00:15:03,960 And let's imagine that we wanted a ball, for example, 306 00:15:03,960 --> 00:15:07,600 to be bouncing on the stage. 307 00:15:07,600 --> 00:15:10,200 Well, let's add a sprite. 308 00:15:10,200 --> 00:15:12,910 And let's add the ball. 309 00:15:12,910 --> 00:15:14,280 Here's the ball. 310 00:15:14,280 --> 00:15:15,998 And what do I want the ball to do? 311 00:15:15,998 --> 00:15:17,790 Well, if it's in the air, the ball is going 312 00:15:17,790 --> 00:15:21,190 to start falling until it hits the ground. 313 00:15:21,190 --> 00:15:22,440 So how do I achieve that idea? 314 00:15:22,440 --> 00:15:25,920 The ball is going to keep falling until it hits the ground. 315 00:15:25,920 --> 00:15:29,070 Well, when the green flag is clicked, I would like 316 00:15:29,070 --> 00:15:32,811 to forever keep repeating something. 317 00:15:32,811 --> 00:15:35,630 318 00:15:35,630 --> 00:15:37,380 And what do I want to have happen forever? 319 00:15:37,380 --> 00:15:46,452 Well, I want to have happen for motion to change the y value by negative 5. 320 00:15:46,452 --> 00:15:48,910 Negative 5 is going to mean the ball is going to move down, 321 00:15:48,910 --> 00:15:51,590 and we're forever going to keep repeating that process. 322 00:15:51,590 --> 00:15:55,610 So now when I press the flag, the ball falls 323 00:15:55,610 --> 00:15:57,940 and eventually it hits the ground. 324 00:15:57,940 --> 00:16:00,230 And if I bring it back up again, it keeps falling 325 00:16:00,230 --> 00:16:02,716 because it's inside of a forever loop. 326 00:16:02,716 --> 00:16:04,270 It's inside of a forever loop. 327 00:16:04,270 --> 00:16:07,793 So every time it's going to keep falling until it hits the ground. 328 00:16:07,793 --> 00:16:09,960 And right now, it's kind of getting past the ground. 329 00:16:09,960 --> 00:16:13,900 So maybe I want it to stop if it ever hits the ground itself. 330 00:16:13,900 --> 00:16:17,443 So how can I detect when the ball is touching the ground? 331 00:16:17,443 --> 00:16:19,860 Well, you'll notice that the ground is a particular color. 332 00:16:19,860 --> 00:16:21,450 It's this brown color here. 333 00:16:21,450 --> 00:16:29,430 And I can check for that color by saying if we're touching that color. 334 00:16:29,430 --> 00:16:32,070 Using if and touching color, which we've used before, 335 00:16:32,070 --> 00:16:34,260 I can check to see if we're touching the ground. 336 00:16:34,260 --> 00:16:36,630 I'll grab a color from the stage. 337 00:16:36,630 --> 00:16:39,240 Let's grab this brown for the ground. 338 00:16:39,240 --> 00:16:46,110 And if we're touching brown, let's go ahead and stop everything. 339 00:16:46,110 --> 00:16:49,130 The ball is going to stop moving. 340 00:16:49,130 --> 00:16:50,990 So now I press the flag. 341 00:16:50,990 --> 00:16:51,950 The ball falls. 342 00:16:51,950 --> 00:16:53,690 And now as soon as it touches the ground, 343 00:16:53,690 --> 00:16:57,500 as soon as it just reaches the beginning of that brown section, 344 00:16:57,500 --> 00:16:59,210 the ball is going to stop moving. 345 00:16:59,210 --> 00:17:02,060 And if I wanted to go again, I press the green flag. 346 00:17:02,060 --> 00:17:07,395 And you notice the ball fall until it hits the ground, and then it stops. 347 00:17:07,395 --> 00:17:09,520 And as we're watching this, I can drag the ball out 348 00:17:09,520 --> 00:17:11,510 and let it fall over and over again. 349 00:17:11,510 --> 00:17:14,319 You might notice that it doesn't look very realistic. 350 00:17:14,319 --> 00:17:16,730 And why isn't this falling very realistic? 351 00:17:16,730 --> 00:17:18,650 Well, I have a ball here with me now. 352 00:17:18,650 --> 00:17:21,310 And what happens if I toss the ball, and let it fall? 353 00:17:21,310 --> 00:17:23,140 How quickly does it fall? 354 00:17:23,140 --> 00:17:26,920 Well, you'll notice as I let that ball fall, that it actually 355 00:17:26,920 --> 00:17:28,420 starts a little more slowly. 356 00:17:28,420 --> 00:17:30,250 At the top, it's more slow. 357 00:17:30,250 --> 00:17:33,370 And then as it falls, over time, it gets faster and faster. 358 00:17:33,370 --> 00:17:36,160 And it falls faster as it gets closer to the ground. 359 00:17:36,160 --> 00:17:38,650 It's slow, and then it gets faster. 360 00:17:38,650 --> 00:17:41,950 And right now, that's not what my ball here is doing. 361 00:17:41,950 --> 00:17:46,300 Right now, my ball is just moving by five units every time 362 00:17:46,300 --> 00:17:50,300 we run through an iteration of this loop. 363 00:17:50,300 --> 00:17:54,430 And so if I really wanted for the ball to be moving realistically, 364 00:17:54,430 --> 00:17:57,460 it should be moving slower at first, and then 365 00:17:57,460 --> 00:18:01,660 get faster as it gets closer to the ground. 366 00:18:01,660 --> 00:18:03,110 So how could we achieve that? 367 00:18:03,110 --> 00:18:06,250 Well, rather than move by five steps every time, 368 00:18:06,250 --> 00:18:09,220 we could move by a variable number of steps every time. 369 00:18:09,220 --> 00:18:13,720 A variable that's keeping track of how many steps should we be moving. 370 00:18:13,720 --> 00:18:16,450 And then we can update that variable so that the ball moves 371 00:18:16,450 --> 00:18:20,560 faster as it gets closer to the ground. 372 00:18:20,560 --> 00:18:21,950 So how do we achieve that? 373 00:18:21,950 --> 00:18:25,070 Well, the first thing we need is we need a new variable. 374 00:18:25,070 --> 00:18:28,870 So I'm going to go into variables here, and create a new variable. 375 00:18:28,870 --> 00:18:31,300 And I'll just call the variable, speed. 376 00:18:31,300 --> 00:18:33,640 And now I have a decision to make as I usually do. 377 00:18:33,640 --> 00:18:35,440 Is this for all sprites? 378 00:18:35,440 --> 00:18:37,390 Or for this sprite only? 379 00:18:37,390 --> 00:18:40,570 And for this variable, I'm actually going to make it for this sprite only. 380 00:18:40,570 --> 00:18:42,580 This speed is only going to be a variable 381 00:18:42,580 --> 00:18:46,150 that this ball is going to get to use, because no other sprite needs 382 00:18:46,150 --> 00:18:49,060 to use or access the speed of the ball. 383 00:18:49,060 --> 00:18:51,400 And that's going to be helpful because maybe later I 384 00:18:51,400 --> 00:18:54,350 might want to have a second ball that's bouncing up and down too. 385 00:18:54,350 --> 00:18:56,953 And then each ball should have its own speed. 386 00:18:56,953 --> 00:18:58,870 I don't want them to be sharing the same speed 387 00:18:58,870 --> 00:19:03,500 because one ball might be moving more slowly or more quickly than the other. 388 00:19:03,500 --> 00:19:06,250 So this variable is going to be a local variable. 389 00:19:06,250 --> 00:19:10,793 Only this ball is going to get to modify this speed variable. 390 00:19:10,793 --> 00:19:13,210 And you'll notice that now, in this reporting block that's 391 00:19:13,210 --> 00:19:15,640 telling me what the speed of the ball is, it's telling me 392 00:19:15,640 --> 00:19:19,600 it's the speed variable that is associated with the ball sprite. 393 00:19:19,600 --> 00:19:24,410 It's telling me what sprite the variable belongs to. 394 00:19:24,410 --> 00:19:27,300 And so now, what do I do here? 395 00:19:27,300 --> 00:19:30,350 Well, initially, when I press the green flag for the first time, 396 00:19:30,350 --> 00:19:33,670 let's set the speed to 0. 397 00:19:33,670 --> 00:19:41,110 Then instead of changing y by negative 5, let's change y by the speed. 398 00:19:41,110 --> 00:19:43,175 Changing it by a variable amount. 399 00:19:43,175 --> 00:19:44,800 And then what do I want to have happen? 400 00:19:44,800 --> 00:19:48,430 Every time the loop goes through, every time time passes, 401 00:19:48,430 --> 00:19:53,410 I want to move faster down the stage, which means 402 00:19:53,410 --> 00:19:55,480 I want this value to be more negative. 403 00:19:55,480 --> 00:20:01,140 So I'm going to change the speed by negative 1. 404 00:20:01,140 --> 00:20:02,160 What does that mean? 405 00:20:02,160 --> 00:20:04,670 That means the speed is going to start at 0. 406 00:20:04,670 --> 00:20:07,760 And the first time we run through the loop, we're going to change y by 0. 407 00:20:07,760 --> 00:20:09,325 We're not changing it at all. 408 00:20:09,325 --> 00:20:11,450 Then we're going to change the speed by negative 1. 409 00:20:11,450 --> 00:20:13,190 Now the speed is negative 1. 410 00:20:13,190 --> 00:20:15,245 We're going to change y by negative 1. 411 00:20:15,245 --> 00:20:17,120 Then we change the speed by negative 1 again. 412 00:20:17,120 --> 00:20:18,650 Now it's negative 2. 413 00:20:18,650 --> 00:20:21,920 And the next time the loop runs we change y by negative 2, 414 00:20:21,920 --> 00:20:22,850 and then negative 3. 415 00:20:22,850 --> 00:20:25,125 And every time we run through the loop, the ball 416 00:20:25,125 --> 00:20:27,500 is going to be moving more, and more, and more each time. 417 00:20:27,500 --> 00:20:34,290 Effectively, moving faster as time goes on until it's touching the ground. 418 00:20:34,290 --> 00:20:40,460 And so let's give that a try now, and see what happens. 419 00:20:40,460 --> 00:20:42,730 Bring the ball up. 420 00:20:42,730 --> 00:20:43,300 And it falls. 421 00:20:43,300 --> 00:20:45,670 And you'll notice that it does exactly what I want. 422 00:20:45,670 --> 00:20:49,722 At the very beginning, it's falling fairly slowly. 423 00:20:49,722 --> 00:20:51,180 But then it speeds up near the end. 424 00:20:51,180 --> 00:20:54,300 You notice near the end the ball speed is all the way up to negative 24, 425 00:20:54,300 --> 00:20:55,140 for example. 426 00:20:55,140 --> 00:20:58,800 It's getting faster and faster. 427 00:20:58,800 --> 00:21:00,470 And so that's working pretty well. 428 00:21:00,470 --> 00:21:04,430 It's more realistic now because at the very beginning, 429 00:21:04,430 --> 00:21:05,810 the ball is moving slowly. 430 00:21:05,810 --> 00:21:08,010 And then over time it's getting faster and faster. 431 00:21:08,010 --> 00:21:10,760 Of course, the thing that it's not doing that a real ball would do 432 00:21:10,760 --> 00:21:12,110 is it's not bouncing. 433 00:21:12,110 --> 00:21:14,270 Normally, when I bounce a ball, it hits the ground 434 00:21:14,270 --> 00:21:16,070 and then it bounces back up to me. 435 00:21:16,070 --> 00:21:17,270 So how could I do that? 436 00:21:17,270 --> 00:21:19,070 What do I need to change? 437 00:21:19,070 --> 00:21:23,190 Well, this variable speed, that's controlling 438 00:21:23,190 --> 00:21:28,420 how many steps the ball is moving every time I run through this loop. 439 00:21:28,420 --> 00:21:32,760 And so when the speed is negative, that means the y value is going to decrease. 440 00:21:32,760 --> 00:21:36,270 Meaning we're going to be moving down on the stage. 441 00:21:36,270 --> 00:21:39,900 If I instead wanted the ball to start moving up once it reached the bottom, 442 00:21:39,900 --> 00:21:43,890 then I want the speed to not be negative, but to instead be positive. 443 00:21:43,890 --> 00:21:47,980 And how do I change the speed from a negative number into a positive number? 444 00:21:47,980 --> 00:21:51,000 Well, I can multiply the speed by negative 1. 445 00:21:51,000 --> 00:21:54,270 Then the speed will go from being negative moving down quickly to being 446 00:21:54,270 --> 00:21:57,180 a positive number moving up quickly. 447 00:21:57,180 --> 00:22:01,020 And so let's give that a try now as well. 448 00:22:01,020 --> 00:22:05,310 Once I'm touching the ground, rather than stopping everything, 449 00:22:05,310 --> 00:22:07,330 let's do something else. 450 00:22:07,330 --> 00:22:12,040 Let's set the speed to a particular value. 451 00:22:12,040 --> 00:22:15,640 And I want it to be the speed multiplied by negative 1. 452 00:22:15,640 --> 00:22:16,750 So going to operations. 453 00:22:16,750 --> 00:22:18,510 We'll do multiply. 454 00:22:18,510 --> 00:22:24,350 And we'll calculate what a speed times negative 1. 455 00:22:24,350 --> 00:22:28,970 And let's set the speed to that. 456 00:22:28,970 --> 00:22:34,365 So now every time we change the speed we check are we touching the ground. 457 00:22:34,365 --> 00:22:36,990 And if we are touching the ground, the speed's going to invert. 458 00:22:36,990 --> 00:22:41,135 Moving the opposite direction and start moving up now instead. 459 00:22:41,135 --> 00:22:44,650 So if I drag the ball up and let it go, watch what happens. 460 00:22:44,650 --> 00:22:47,490 It falls and then it bounces. 461 00:22:47,490 --> 00:22:48,750 And it bounces. 462 00:22:48,750 --> 00:22:51,900 And it keeps bouncing. 463 00:22:51,900 --> 00:22:54,990 And the reason that's happening is because it's getting negative speed 464 00:22:54,990 --> 00:22:56,280 as it's going down. 465 00:22:56,280 --> 00:22:59,410 And then as soon as it touches the ground, the speed becomes positive. 466 00:22:59,410 --> 00:23:02,040 And then it goes back up again. 467 00:23:02,040 --> 00:23:04,160 Now the one thing this isn't doing if we really 468 00:23:04,160 --> 00:23:06,035 wanted to make this even more realistic, just 469 00:23:06,035 --> 00:23:08,210 to add one final touch to this program, is 470 00:23:08,210 --> 00:23:11,250 that when I throw the ball on the ground, 471 00:23:11,250 --> 00:23:13,040 it doesn't come up to the same place. 472 00:23:13,040 --> 00:23:14,720 It's a little bit lower every time. 473 00:23:14,720 --> 00:23:16,797 Every time the ball bounces it starts high. 474 00:23:16,797 --> 00:23:19,380 Then it might be a little lower then a little lower than that. 475 00:23:19,380 --> 00:23:22,847 It loses a little bit of speed every time I bounce the ball. 476 00:23:22,847 --> 00:23:24,680 But this is the great thing about variables. 477 00:23:24,680 --> 00:23:27,320 I can set this variable to be whatever I want. 478 00:23:27,320 --> 00:23:30,060 And I can use these operators-- adding, and subtracting, 479 00:23:30,060 --> 00:23:34,070 and multiplying, and dividing to figure out exactly what I want for a variable 480 00:23:34,070 --> 00:23:34,740 to be. 481 00:23:34,740 --> 00:23:38,850 So maybe I want the speed to decrease by a little every time. 482 00:23:38,850 --> 00:23:43,790 Don't just make the speed positive, but subtract 1 or subtract 2 from the speed 483 00:23:43,790 --> 00:23:48,980 so that now it's moving a little more slowly every time the ball bounces. 484 00:23:48,980 --> 00:23:51,800 So I can go back into operators, and let's 485 00:23:51,800 --> 00:23:57,740 subtract 2 from whatever that new speed was supposed to be. 486 00:23:57,740 --> 00:23:58,790 We invert the speed. 487 00:23:58,790 --> 00:23:59,930 We subtract 2. 488 00:23:59,930 --> 00:24:04,670 And now it's going to go from a higher bounce to a lower bounce. 489 00:24:04,670 --> 00:24:08,210 And every time you'll notice that it decreases a little until it 490 00:24:08,210 --> 00:24:10,750 kind of gets stuck at the bottom there. 491 00:24:10,750 --> 00:24:11,750 But we can try it again. 492 00:24:11,750 --> 00:24:15,130 493 00:24:15,130 --> 00:24:17,635 It's going lower and lower each time. 494 00:24:17,635 --> 00:24:19,510 And eventually, it's going to hit the bottom. 495 00:24:19,510 --> 00:24:21,820 And you might have to play around with it a little bit. 496 00:24:21,820 --> 00:24:23,710 Once it gets to a speed that's low enough, 497 00:24:23,710 --> 00:24:25,720 the ball should eventually just stop moving. 498 00:24:25,720 --> 00:24:29,770 But this is approximating that idea-- of a ball that's bouncing again and again. 499 00:24:29,770 --> 00:24:32,410 And we're able to use variables to control the speed 500 00:24:32,410 --> 00:24:36,990 to make this much more realistic. 501 00:24:36,990 --> 00:24:37,490 All right. 502 00:24:37,490 --> 00:24:38,948 What else can we do with variables? 503 00:24:38,948 --> 00:24:41,610 We'll take a look at a couple other final examples. 504 00:24:41,610 --> 00:24:45,740 One is we can let the user have control over the variable itself. 505 00:24:45,740 --> 00:24:46,700 What you'll notice-- 506 00:24:46,700 --> 00:24:49,220 I'll go ahead and get rid of the ball, and go back 507 00:24:49,220 --> 00:24:51,860 to the plain white backdrop. 508 00:24:51,860 --> 00:24:55,040 Let's go back to the balloon example that we've seen a couple of times 509 00:24:55,040 --> 00:24:56,840 now where the balloon was inflating. 510 00:24:56,840 --> 00:24:59,600 And it was inflating either when we blew at the microphone, 511 00:24:59,600 --> 00:25:04,410 or press the spacebar, or we just let it go automatically via a loop. 512 00:25:04,410 --> 00:25:08,840 Let's let the user now control how big or small the balloon should be. 513 00:25:08,840 --> 00:25:13,170 Let the user control how much air is inside of the balloon. 514 00:25:13,170 --> 00:25:17,240 Let's create a variable that I'm just going to call, air. 515 00:25:17,240 --> 00:25:20,510 And now what is this air variable going to do? 516 00:25:20,510 --> 00:25:25,010 Well, if I Control click on this area here where 517 00:25:25,010 --> 00:25:27,410 I can see what the value of a variable is, 518 00:25:27,410 --> 00:25:30,170 I can change what the readout looks like. 519 00:25:30,170 --> 00:25:31,520 This is a normal readout. 520 00:25:31,520 --> 00:25:34,100 If I change it to a large readout, you just 521 00:25:34,100 --> 00:25:36,110 see the number a little bit bigger. 522 00:25:36,110 --> 00:25:38,420 But I can also change it to a slider. 523 00:25:38,420 --> 00:25:41,510 And when it's a slider, the user of the program 524 00:25:41,510 --> 00:25:45,350 can just move the slider to control what the value of the variable 525 00:25:45,350 --> 00:25:46,310 is going to be. 526 00:25:46,310 --> 00:25:50,580 I can change air to be 0 all the way up to 100 for example. 527 00:25:50,580 --> 00:25:55,310 And that lets the user have some control over the variables in the program. 528 00:25:55,310 --> 00:25:57,440 And how big was this balloon before? 529 00:25:57,440 --> 00:26:01,790 Well, usually, it was somewhere between like size 100 and 200. 530 00:26:01,790 --> 00:26:04,550 So let me edit the slider range. 531 00:26:04,550 --> 00:26:06,590 Set the minimum value to 100. 532 00:26:06,590 --> 00:26:08,310 And the maximum value to 200. 533 00:26:08,310 --> 00:26:11,210 I did that by Control clicking on the slider. 534 00:26:11,210 --> 00:26:15,050 And then changing the slider range to now range between 100 535 00:26:15,050 --> 00:26:18,560 at a minimum and 200 at a maximum. 536 00:26:18,560 --> 00:26:21,570 So I can go back and forth between those two values. 537 00:26:21,570 --> 00:26:24,140 And now what do I want the balloon to do? 538 00:26:24,140 --> 00:26:27,590 Well, when the flag is clicked, we'll just 539 00:26:27,590 --> 00:26:36,290 add a few blocks that say forever I want the balloon to set it size not to 100%, 540 00:26:36,290 --> 00:26:38,720 but to air percent. 541 00:26:38,720 --> 00:26:42,980 Whatever the value of the air variable is. 542 00:26:42,980 --> 00:26:45,860 That is what I want the size of the balloon to be. 543 00:26:45,860 --> 00:26:47,990 I press the flag to start the program. 544 00:26:47,990 --> 00:26:50,330 And so now this balloon is constantly going 545 00:26:50,330 --> 00:26:53,150 to be checking what is the value of the air variable. 546 00:26:53,150 --> 00:26:55,570 And depending upon the value of the air variable, 547 00:26:55,570 --> 00:26:57,320 that's how big the balloon is going to be. 548 00:26:57,320 --> 00:27:00,170 So I can now just drag along the slider. 549 00:27:00,170 --> 00:27:03,680 And as I drag along the slider, you'll see the balloon grow and shrink. 550 00:27:03,680 --> 00:27:06,170 I'm changing the value of the air variable, 551 00:27:06,170 --> 00:27:10,010 and constantly forever the balloon is updating its size 552 00:27:10,010 --> 00:27:13,700 to be whatever the value of that variable should be. 553 00:27:13,700 --> 00:27:17,450 And so variables don't just have to be set by these like set variable to, 554 00:27:17,450 --> 00:27:19,790 or change variable by values. 555 00:27:19,790 --> 00:27:22,790 You can also just let the user control a variable directly 556 00:27:22,790 --> 00:27:25,410 to let them interact with your Scratch project. 557 00:27:25,410 --> 00:27:30,110 Let them decide how things should behave or what a particular sprite should 558 00:27:30,110 --> 00:27:32,760 look like for example. 559 00:27:32,760 --> 00:27:35,090 And let's do one final example with variables. 560 00:27:35,090 --> 00:27:39,050 This time taking advantage of the Pen tool to draw some shapes. 561 00:27:39,050 --> 00:27:41,210 And so I'll bring back the cat. 562 00:27:41,210 --> 00:27:44,540 563 00:27:44,540 --> 00:27:46,100 Here's our cat again. 564 00:27:46,100 --> 00:27:50,120 And you might recall from before that we could get the cat to move in a circle 565 00:27:50,120 --> 00:27:55,170 by doing something like when the green flag is clicked, then 566 00:27:55,170 --> 00:28:01,350 forever have the cat move a certain number of steps 567 00:28:01,350 --> 00:28:04,030 and then turn a certain number of degrees. 568 00:28:04,030 --> 00:28:06,630 And that had the effect of moving the cat in a circle 569 00:28:06,630 --> 00:28:11,810 over and over just by moving, and then turning a little bit every time. 570 00:28:11,810 --> 00:28:15,770 And if we wanted to draw that, we could use the Pen tool to try and draw that. 571 00:28:15,770 --> 00:28:18,580 I'll have the Scratch cat start in a particular place 572 00:28:18,580 --> 00:28:19,960 when the green flag is clicked. 573 00:28:19,960 --> 00:28:22,210 Let's go ahead and go to 0, 0. 574 00:28:22,210 --> 00:28:25,405 And be sure you're pointing 90 degrees-- meaning to the right. 575 00:28:25,405 --> 00:28:28,030 And that's going to start the Scratch cut off in the same place 576 00:28:28,030 --> 00:28:32,060 every time so we get a consistent circle. 577 00:28:32,060 --> 00:28:35,900 But what I can do is add the pen extension, which we've seen before, 578 00:28:35,900 --> 00:28:38,140 to do a little bit of drawing. 579 00:28:38,140 --> 00:28:41,250 We're going to erase everything. 580 00:28:41,250 --> 00:28:47,360 And then put the pen down to let the cat start drawing. 581 00:28:47,360 --> 00:28:50,730 And now what you're noticing is the cat's moving in a circle. 582 00:28:50,730 --> 00:28:53,120 And as it moves in that circle, it's drawing. 583 00:28:53,120 --> 00:28:56,680 It's using the pen to draw the circle that it's moving in. 584 00:28:56,680 --> 00:28:59,210 And if I want to make the cat move a little bit more slowly, 585 00:28:59,210 --> 00:29:01,040 I might have the cat wait a little bit. 586 00:29:01,040 --> 00:29:02,998 Let's just have it wait a fraction of a second. 587 00:29:02,998 --> 00:29:04,940 Maybe 0.05 seconds. 588 00:29:04,940 --> 00:29:07,880 And now the cat's moving a little more slowly. 589 00:29:07,880 --> 00:29:13,073 And you can actually see it drawing that circle just by drawing it like that. 590 00:29:13,073 --> 00:29:14,990 But we're not really using any variables here. 591 00:29:14,990 --> 00:29:17,690 The reason why variables can be helpful is because we 592 00:29:17,690 --> 00:29:19,520 can change the cat's behavior. 593 00:29:19,520 --> 00:29:22,460 Maybe not have it move by 10 steps every time. 594 00:29:22,460 --> 00:29:26,120 But have it move by a different number of steps every time. 595 00:29:26,120 --> 00:29:28,200 So I'll create a new variable. 596 00:29:28,200 --> 00:29:30,420 I'll call it steps. 597 00:29:30,420 --> 00:29:35,310 And we'll start by setting steps equal to 0. 598 00:29:35,310 --> 00:29:39,880 And instead of moving 10 steps, let's move steps steps. 599 00:29:39,880 --> 00:29:43,560 Whatever the value of that variable is, that's how far I would like to move. 600 00:29:43,560 --> 00:29:47,400 But then every time you move, we're going to change the number of steps 601 00:29:47,400 --> 00:29:50,190 by something like 0.5. 602 00:29:50,190 --> 00:29:53,740 A small number just to make a very small change. 603 00:29:53,740 --> 00:29:56,190 So what's going on now with all of these blocks? 604 00:29:56,190 --> 00:29:57,390 We'll try and recap. 605 00:29:57,390 --> 00:29:59,753 At the very beginning, when we first start the program, 606 00:29:59,753 --> 00:30:01,170 we're doing a little bit of setup. 607 00:30:01,170 --> 00:30:03,210 We're saying have the cat go back to 0, 0. 608 00:30:03,210 --> 00:30:04,920 Just to the center of the stage. 609 00:30:04,920 --> 00:30:06,300 Have it point in 90 degrees. 610 00:30:06,300 --> 00:30:07,620 Meaning point to the right. 611 00:30:07,620 --> 00:30:09,125 And erase everything. 612 00:30:09,125 --> 00:30:11,250 So if there were already things drawn on the stage, 613 00:30:11,250 --> 00:30:13,650 just erase them so that we can start fresh. 614 00:30:13,650 --> 00:30:15,690 And now put the pen down. 615 00:30:15,690 --> 00:30:18,960 Meaning wherever the Scratch cat moves, the pen is going to follow. 616 00:30:18,960 --> 00:30:21,210 And it's going to trace those shapes. 617 00:30:21,210 --> 00:30:24,030 And we have a variable keeping track of how far 618 00:30:24,030 --> 00:30:26,790 the cat's going to move every time. 619 00:30:26,790 --> 00:30:28,470 Right now, it's starts at 0. 620 00:30:28,470 --> 00:30:30,120 So we move that number of steps. 621 00:30:30,120 --> 00:30:33,420 But over time, we're going to increase the number of steps the cats moving. 622 00:30:33,420 --> 00:30:36,220 Increase it by a little bit, and then rotate. 623 00:30:36,220 --> 00:30:39,702 And if we keep increasing how much the cat's moving and then rotating, 624 00:30:39,702 --> 00:30:41,910 what you're going to see is a more interesting shape. 625 00:30:41,910 --> 00:30:44,730 It's not going to draw a perfect circle. 626 00:30:44,730 --> 00:30:49,485 But it's instead going to draw something like this spiral. 627 00:30:49,485 --> 00:30:52,410 Notice it's moving by more and more every time. 628 00:30:52,410 --> 00:30:54,750 And as it moves more and more with the pen down, 629 00:30:54,750 --> 00:30:58,380 we're able to see this interesting spiral result from it. 630 00:30:58,380 --> 00:31:01,050 Because it started by moving very little every single time. 631 00:31:01,050 --> 00:31:02,910 Just moving by a few steps. 632 00:31:02,910 --> 00:31:06,300 And over time it's moving more, and more, and more every time it rotates. 633 00:31:06,300 --> 00:31:09,932 And that's creating the shape that you're able to see here. 634 00:31:09,932 --> 00:31:12,390 And so by taking advantage of the pen, and taking advantage 635 00:31:12,390 --> 00:31:14,580 of variables and loops, you're able to create 636 00:31:14,580 --> 00:31:16,200 some really interesting works of art. 637 00:31:16,200 --> 00:31:19,530 Shapes that can be drawn just by sprites on the stage 638 00:31:19,530 --> 00:31:22,020 by calculating, by modifying some variables 639 00:31:22,020 --> 00:31:25,650 and adjusting what those variables should do in order to create 640 00:31:25,650 --> 00:31:27,880 more and more interesting programs. 641 00:31:27,880 --> 00:31:30,330 And you can see up here in the upper left 642 00:31:30,330 --> 00:31:34,350 this keeping track of how many steps it is that we've 643 00:31:34,350 --> 00:31:37,750 taken so far inside of this program. 644 00:31:37,750 --> 00:31:40,380 So variables add a lot of power to the programs 645 00:31:40,380 --> 00:31:42,330 that we can create inside of Scratch. 646 00:31:42,330 --> 00:31:45,480 They give us the ability to keep track of whatever information we want. 647 00:31:45,480 --> 00:31:48,870 And then to use that information later on in our programs 648 00:31:48,870 --> 00:31:52,830 to create even more exciting games, and animations, and stories 649 00:31:52,830 --> 00:31:55,590 all just through the use of the Scratch box. 650 00:31:55,590 --> 00:31:58,660 That's it for an Introduction to Programming with Scratch for today. 651 00:31:58,660 --> 00:32:00,680 We'll see you next time. 652 00:32:00,680 --> 00:32:02,000