1 00:00:00,000 --> 00:00:02,470 [MUSIC PLAYING] 2 00:00:02,470 --> 00:00:17,300 3 00:00:17,300 --> 00:00:20,840 BRIAN YU: Welcome back, everyone, to now our final class in this Introduction 4 00:00:20,840 --> 00:00:22,160 to Programming with Scratch. 5 00:00:22,160 --> 00:00:25,040 And at this point, we've seen most of the major features 6 00:00:25,040 --> 00:00:26,280 that Scratch has to offer. 7 00:00:26,280 --> 00:00:28,670 So today in this final class, what we thought we'd do 8 00:00:28,670 --> 00:00:31,580 is show you a few additional features that you might find useful 9 00:00:31,580 --> 00:00:33,380 as you go about building your own projects 10 00:00:33,380 --> 00:00:36,620 and then show you how you could put the pieces together, so to speak, 11 00:00:36,620 --> 00:00:40,430 and use all of the tools and the ideas that we've explored during this course 12 00:00:40,430 --> 00:00:44,510 to see how you could put them together to create a complete game using 13 00:00:44,510 --> 00:00:45,440 Scratch. 14 00:00:45,440 --> 00:00:48,620 So let's go ahead and begin by taking a look at a couple final features 15 00:00:48,620 --> 00:00:50,030 of Scratch to introduce. 16 00:00:50,030 --> 00:00:54,540 And the first involves how it is that sprites communicate with each other. 17 00:00:54,540 --> 00:00:56,420 You might recall that in an earlier project, 18 00:00:56,420 --> 00:00:59,267 we had a cat talking to a dinosaur, for example. 19 00:00:59,267 --> 00:01:01,100 And how we did that was something like this. 20 00:01:01,100 --> 00:01:05,239 I had a cat, and then I added the dinosaur, which 21 00:01:05,239 --> 00:01:07,430 I'll go ahead and find and bring back. 22 00:01:07,430 --> 00:01:10,020 I wanted the cat and the dinosaur to be facing each other. 23 00:01:10,020 --> 00:01:12,620 So I took the dinosaur and I set its rotation 24 00:01:12,620 --> 00:01:16,440 to left right and then set its direction to negative 90 25 00:01:16,440 --> 00:01:18,240 so it was facing the other way. 26 00:01:18,240 --> 00:01:21,320 And if I wanted the cat and the dinosaur to greet each other, 27 00:01:21,320 --> 00:01:29,390 then I would have the cat when the flag is clicked do something like say hello 28 00:01:29,390 --> 00:01:32,330 to the dinosaur. 29 00:01:32,330 --> 00:01:35,810 And then likewise for the dinosaur, if I want the dinosaur to respond 30 00:01:35,810 --> 00:01:38,000 to the cat saying hello, well, the cat is 31 00:01:38,000 --> 00:01:40,430 saying hello dinosaur for two seconds. 32 00:01:40,430 --> 00:01:44,120 So the dinosaur better wait two seconds before it responds. 33 00:01:44,120 --> 00:01:46,700 So the dinosaur, I did something like this. 34 00:01:46,700 --> 00:01:49,850 I said when the flag is clicked, let's go under Control. 35 00:01:49,850 --> 00:02:00,050 Let's wait two seconds and then say hello to the cat. 36 00:02:00,050 --> 00:02:03,560 So now when I run the project, the cat says hello dinosaur. 37 00:02:03,560 --> 00:02:07,580 And immediately after, the dinosaur says hello cat. 38 00:02:07,580 --> 00:02:09,270 What if I later wanted to make a change? 39 00:02:09,270 --> 00:02:12,530 I want the cat to only say hello dinosaur for maybe one second, 40 00:02:12,530 --> 00:02:13,490 for example. 41 00:02:13,490 --> 00:02:17,450 Well, now the cat speaks and I have this weird pause 42 00:02:17,450 --> 00:02:20,690 before the dinosaur responds, because I forgot to change 43 00:02:20,690 --> 00:02:22,610 how long the dinosaur was waiting. 44 00:02:22,610 --> 00:02:25,010 I'd also have to change that from two to one 45 00:02:25,010 --> 00:02:27,650 in order to get it to respond appropriately. 46 00:02:27,650 --> 00:02:30,875 Ultimately, this is going to start to get more complex, especially as there 47 00:02:30,875 --> 00:02:33,560 are more interactions between these two sprites 48 00:02:33,560 --> 00:02:35,810 or if later I add other sprites altogether 49 00:02:35,810 --> 00:02:38,960 for me to have to calculate exactly how long every sprite should 50 00:02:38,960 --> 00:02:42,560 be waiting in between one thing happening and another thing happening. 51 00:02:42,560 --> 00:02:46,460 What would be better is if the cat when it was done speaking 52 00:02:46,460 --> 00:02:50,870 could somehow signal to the dinosaur that it's now the dinosaurs turn 53 00:02:50,870 --> 00:02:51,800 to start speaking. 54 00:02:51,800 --> 00:02:53,930 That way I, the programmer, don't have to do 55 00:02:53,930 --> 00:02:56,210 all this math of figuring out exactly how 56 00:02:56,210 --> 00:02:58,430 long these sprites should be waiting. 57 00:02:58,430 --> 00:03:00,530 And so this brings us to one of our final topics 58 00:03:00,530 --> 00:03:03,080 in Scratch, which is called broadcasting. 59 00:03:03,080 --> 00:03:07,370 Broadcasting is the ability for one object in Scratch, one sprite, 60 00:03:07,370 --> 00:03:12,650 for example, to send some message that other sprites like the dinosaur, 61 00:03:12,650 --> 00:03:14,660 in this case, could receive. 62 00:03:14,660 --> 00:03:17,900 So we have the ability to send or broadcast messages. 63 00:03:17,900 --> 00:03:21,290 And then we have the ability for other sprites to receive those messages. 64 00:03:21,290 --> 00:03:25,672 When they receive a message, the sprites are going to respond in some way. 65 00:03:25,672 --> 00:03:26,880 So what would that look like? 66 00:03:26,880 --> 00:03:30,590 How could we broadcast a message from the cat to the dinosaur 67 00:03:30,590 --> 00:03:34,370 so that the dinosaur knows that it's time to greet the cat? 68 00:03:34,370 --> 00:03:36,140 Let's go back to the cat. 69 00:03:36,140 --> 00:03:41,060 And after the cat says hello dinosaur for let's go back to two seconds, 70 00:03:41,060 --> 00:03:48,710 then under Event, I'm going to use this broadcast block to broadcast a message. 71 00:03:48,710 --> 00:03:51,080 And I'm going to choose a new message from the dropdown. 72 00:03:51,080 --> 00:03:52,850 And I can just name this message. 73 00:03:52,850 --> 00:03:55,400 This is any message that I'm now going to send. 74 00:03:55,400 --> 00:03:58,730 And I'm going to call the message greet, just because it's 75 00:03:58,730 --> 00:04:01,460 going to be a message that signals to the dinosaur 76 00:04:01,460 --> 00:04:03,980 that it's time to greet the cat. 77 00:04:03,980 --> 00:04:08,030 So the cat says hello dinosaur and then broadcasts greet. 78 00:04:08,030 --> 00:04:13,280 And the dinosaur now instead of saying hello cat when the flag is clicked, 79 00:04:13,280 --> 00:04:16,700 I'm going to get rid of the when flag is clicked event entirely 80 00:04:16,700 --> 00:04:22,670 and replace it with this event here when I receive greet. 81 00:04:22,670 --> 00:04:26,630 So the cat is first going to say hello dinosaur for two seconds 82 00:04:26,630 --> 00:04:28,680 and then broadcast this message. 83 00:04:28,680 --> 00:04:30,650 The message is greet. 84 00:04:30,650 --> 00:04:34,700 And then the dinosaur when it receives the greet message is then 85 00:04:34,700 --> 00:04:36,890 going to say hello to the cat. 86 00:04:36,890 --> 00:04:41,360 And so now the cat says hello dinosaur, and immediately the dinosaur 87 00:04:41,360 --> 00:04:42,710 responds hello cat. 88 00:04:42,710 --> 00:04:46,250 The cat broadcasted a message and the dinosaur received it. 89 00:04:46,250 --> 00:04:48,170 And now it doesn't matter if there's a delay. 90 00:04:48,170 --> 00:04:52,250 I could add a delay of a second to the cat. 91 00:04:52,250 --> 00:04:55,700 And so the cat will now wait a second before speaking, 92 00:04:55,700 --> 00:04:57,830 but the dinosaur still responds right on cue. 93 00:04:57,830 --> 00:05:00,060 I didn't have to change any of the timing, 94 00:05:00,060 --> 00:05:03,290 because the cat broadcasted a message once the cat was 95 00:05:03,290 --> 00:05:08,130 done doing whatever it was doing and the dinosaur was then able to respond. 96 00:05:08,130 --> 00:05:10,783 So this works great for stories where you have multiple sprites 97 00:05:10,783 --> 00:05:13,700 and they're interacting in some way and you want some sprite to signal 98 00:05:13,700 --> 00:05:17,750 to someone else when it's their turn to say something or do something, 99 00:05:17,750 --> 00:05:18,590 for example. 100 00:05:18,590 --> 00:05:22,070 It can also be used in other ways to let one sprite control 101 00:05:22,070 --> 00:05:25,130 or influence the behavior of another sprite. 102 00:05:25,130 --> 00:05:27,830 Let's imagine that I had a duck. 103 00:05:27,830 --> 00:05:34,280 Let's add a new animal and let's go back to the duck here. 104 00:05:34,280 --> 00:05:36,787 And let's say I wanted the duck to move up and down. 105 00:05:36,787 --> 00:05:38,120 And we could use the arrow keys. 106 00:05:38,120 --> 00:05:40,280 I could use the up and down arrow keys to control it. 107 00:05:40,280 --> 00:05:42,363 But I'd like for the user just to be able to click 108 00:05:42,363 --> 00:05:45,620 on up and down arrows on the screen as some user interface 109 00:05:45,620 --> 00:05:48,020 that they could use as a visual on the screen that 110 00:05:48,020 --> 00:05:51,620 lets them control if the duck is going to move up or move down. 111 00:05:51,620 --> 00:05:53,270 So let's add that interface. 112 00:05:53,270 --> 00:05:55,070 Here's an arrow. 113 00:05:55,070 --> 00:05:56,910 And right now the arrow is facing the right. 114 00:05:56,910 --> 00:06:00,410 But if I wanted to go up, then I'm going to rotate it instead of 90 degrees, 115 00:06:00,410 --> 00:06:02,150 let's set it to 0 degrees. 116 00:06:02,150 --> 00:06:04,970 That's going to be an arrow that's facing up. 117 00:06:04,970 --> 00:06:10,400 And if I want a button that lets the duck go down, let's add an arrow. 118 00:06:10,400 --> 00:06:13,530 And this time set it to 180 degrees. 119 00:06:13,530 --> 00:06:16,890 And now the arrow is facing down. 120 00:06:16,890 --> 00:06:18,680 So now I have two arrows. 121 00:06:18,680 --> 00:06:22,140 But clicking these arrows doesn't achieve anything. 122 00:06:22,140 --> 00:06:25,790 They just do nothing right now because I haven't added any code to them. 123 00:06:25,790 --> 00:06:27,200 But I could. 124 00:06:27,200 --> 00:06:32,960 Let me add an event to the up arrow that says when this sprite is clicked, 125 00:06:32,960 --> 00:06:38,480 when I click on the up arrow, well then let's broadcast a message. 126 00:06:38,480 --> 00:06:41,210 And the message is going to be up. 127 00:06:41,210 --> 00:06:43,670 That's the direction you should move. 128 00:06:43,670 --> 00:06:46,320 Now let's choose the down arrow. 129 00:06:46,320 --> 00:06:49,280 This time when the sprite is clicked, let's 130 00:06:49,280 --> 00:06:53,180 broadcast a message not up, but this time a new message. 131 00:06:53,180 --> 00:06:55,530 And the message is going to be down. 132 00:06:55,530 --> 00:06:58,700 So now the up arrow is broadcasting a message that says up. 133 00:06:58,700 --> 00:07:02,030 The down arrow is broadcasting a message that says down. 134 00:07:02,030 --> 00:07:05,120 And now I'll have the duck respond to those broadcasts. 135 00:07:05,120 --> 00:07:10,010 When the duck receives the up message, well then 136 00:07:10,010 --> 00:07:16,220 let's have the duck change its y value by 10 going upward in direction. 137 00:07:16,220 --> 00:07:22,340 And likewise under Events, when the duck receives the down broadcast message, 138 00:07:22,340 --> 00:07:28,010 then we're going to change the y value by negative 10. 139 00:07:28,010 --> 00:07:31,430 And now these arrows, these sprites on the screen, 140 00:07:31,430 --> 00:07:33,230 are controlling the behavior of the duck. 141 00:07:33,230 --> 00:07:36,470 If I click the up arrow, the duck moves up. 142 00:07:36,470 --> 00:07:39,470 And if I click the down arrow, the duck moves down. 143 00:07:39,470 --> 00:07:41,540 And they're doing that because of broadcasting. 144 00:07:41,540 --> 00:07:44,720 When I click on the arrows, those arrows are themselves 145 00:07:44,720 --> 00:07:48,020 sprites that are saying move up or move down. 146 00:07:48,020 --> 00:07:49,970 They're broadcasting some message. 147 00:07:49,970 --> 00:07:55,190 And the duck in turn is then responding to those messages. 148 00:07:55,190 --> 00:07:59,930 And it doesn't just have to be sprites that are broadcasting messages. 149 00:07:59,930 --> 00:08:03,350 The stage, the background can broadcast messages too. 150 00:08:03,350 --> 00:08:05,090 I'll show you an example of that. 151 00:08:05,090 --> 00:08:06,860 I'll delete all of these sprites for now. 152 00:08:06,860 --> 00:08:08,270 Let's add a backdrop. 153 00:08:08,270 --> 00:08:11,270 And let's go back to our underwater backdrop. 154 00:08:11,270 --> 00:08:13,850 And let's bring our fish back. 155 00:08:13,850 --> 00:08:20,270 So I'll add a new sprite and add back the fish. 156 00:08:20,270 --> 00:08:23,540 And now what I would like to do is in the backdrop, 157 00:08:23,540 --> 00:08:26,150 I'll click on the backdrop, add some code here, 158 00:08:26,150 --> 00:08:31,160 I'll say whenever the stage is clicked, when stage clicked, 159 00:08:31,160 --> 00:08:34,669 let's now broadcast a message. 160 00:08:34,669 --> 00:08:38,150 And the message is going to be visit. 161 00:08:38,150 --> 00:08:44,870 I would like the fish to visit wherever I click on the stage. 162 00:08:44,870 --> 00:08:49,000 And now for the fish whenever it receives the visit message, 163 00:08:49,000 --> 00:08:53,530 let's have the fish point towards the mouse pointer 164 00:08:53,530 --> 00:08:59,060 and then take one second to glide to the mouse pointer. 165 00:08:59,060 --> 00:09:00,160 So now I press the flag. 166 00:09:00,160 --> 00:09:01,550 At first, nothing happens. 167 00:09:01,550 --> 00:09:06,400 But if I choose a location and then click, the fish goes there. 168 00:09:06,400 --> 00:09:11,380 I choose the location and click, and the fish visits wherever I happen to click. 169 00:09:11,380 --> 00:09:12,130 I click over here. 170 00:09:12,130 --> 00:09:13,310 The fish goes there. 171 00:09:13,310 --> 00:09:17,170 And every time when I click, the backdrop is broadcasting a message. 172 00:09:17,170 --> 00:09:19,990 The stage is broadcasting this visit message 173 00:09:19,990 --> 00:09:23,380 and the fish is now receiving that visit message 174 00:09:23,380 --> 00:09:26,320 and doing something in response, pointing towards the mouse pointer 175 00:09:26,320 --> 00:09:27,560 and gliding there. 176 00:09:27,560 --> 00:09:31,390 So broadcasting allows multiple different parts of our Scratch project 177 00:09:31,390 --> 00:09:36,370 to communicate with each other to decide when something should happen. 178 00:09:36,370 --> 00:09:40,330 And that's one very powerful, very useful feature within Scratch. 179 00:09:40,330 --> 00:09:44,470 Another feature that can be useful, and for this I'll get rid of the backdrop, 180 00:09:44,470 --> 00:09:47,320 I'll go back to the default white backdrop, 181 00:09:47,320 --> 00:09:50,650 is the ability to clone a sprite. 182 00:09:50,650 --> 00:09:53,350 If I have one copy of a sprite oftentimes in a game 183 00:09:53,350 --> 00:09:56,320 or in some sort of other animation, I might not just one 184 00:09:56,320 --> 00:10:00,130 copy of a sprite on the stage, but I might want multiple copies 185 00:10:00,130 --> 00:10:03,020 of that sprite on the stage as well. 186 00:10:03,020 --> 00:10:04,270 So let's add a sprite. 187 00:10:04,270 --> 00:10:08,320 Let me search for the star and let's grab the star. 188 00:10:08,320 --> 00:10:10,450 And maybe what I'd like to happen is to not just 189 00:10:10,450 --> 00:10:15,890 have one star but to have multiple stars inside of my project. 190 00:10:15,890 --> 00:10:18,670 So let's add some code here. 191 00:10:18,670 --> 00:10:23,740 I'll say when this sprite is clicked, when I click on the star, 192 00:10:23,740 --> 00:10:30,550 I am going to, and this is under Control, create a clone of myself. 193 00:10:30,550 --> 00:10:34,000 When the star is clicked, we're going to create a clone of the star. 194 00:10:34,000 --> 00:10:36,550 And that new clone is going to be another star that 195 00:10:36,550 --> 00:10:39,170 can behave on its own. 196 00:10:39,170 --> 00:10:40,360 Now I'll add this new event. 197 00:10:40,360 --> 00:10:44,230 When I start as a clone, what should this clone star do? 198 00:10:44,230 --> 00:10:50,270 Well, let's have the cloned star just glide one second to a random position. 199 00:10:50,270 --> 00:10:52,730 So I have this star, and watch what happens. 200 00:10:52,730 --> 00:10:58,490 Every time I click on the star, the star creates a new clone. 201 00:10:58,490 --> 00:11:00,600 Every time I click, a new clone is created. 202 00:11:00,600 --> 00:11:03,350 And when the new star starts as a clone, it's 203 00:11:03,350 --> 00:11:05,720 going to glide to a random position. 204 00:11:05,720 --> 00:11:10,880 Every time I clone a star, you'll see we create a duplicate event 205 00:11:10,880 --> 00:11:13,950 and it ends up going to a random position. 206 00:11:13,950 --> 00:11:17,930 So if I want multiple of a particular sprite that appear on the stage, 207 00:11:17,930 --> 00:11:19,350 cloning is a great way to do that. 208 00:11:19,350 --> 00:11:21,920 We can create multiple copies of a sprite 209 00:11:21,920 --> 00:11:27,470 and then decide how it is that those clones should ultimately behave. 210 00:11:27,470 --> 00:11:30,140 So now I'll go ahead and get rid of all those clones 211 00:11:30,140 --> 00:11:34,870 just by deleting the sprite and clearing the screen. 212 00:11:34,870 --> 00:11:37,870 Now we've seen a lot of features that Scratch has to offer. 213 00:11:37,870 --> 00:11:40,030 We've seen functions and we've seen events. 214 00:11:40,030 --> 00:11:43,420 We've seen variable, loops and conditions, and now the ability 215 00:11:43,420 --> 00:11:45,940 to broadcast messages and clone sprites. 216 00:11:45,940 --> 00:11:48,430 Let's take all of those ideas now and put them 217 00:11:48,430 --> 00:11:51,100 together inside of a fully complete game to show you 218 00:11:51,100 --> 00:11:55,810 how you could go about building a more complex project within Scratch. 219 00:11:55,810 --> 00:11:58,010 And what characters should I use in the game? 220 00:11:58,010 --> 00:12:02,380 Let's start and just start building this game one piece at a time. 221 00:12:02,380 --> 00:12:05,800 Well, let's start by using the cat. 222 00:12:05,800 --> 00:12:08,780 Sort of the classic default sprite character. 223 00:12:08,780 --> 00:12:12,140 And let's have the cat have some objective. 224 00:12:12,140 --> 00:12:13,765 What is the cat trying to do? 225 00:12:13,765 --> 00:12:15,640 Well, I can just scroll through these sprites 226 00:12:15,640 --> 00:12:17,140 and look for what seems interesting. 227 00:12:17,140 --> 00:12:18,880 Here are the crystals. 228 00:12:18,880 --> 00:12:23,177 Let's have the cat try to catch as many crystals as it can. 229 00:12:23,177 --> 00:12:24,760 And what are the crystals going to do? 230 00:12:24,760 --> 00:12:26,410 Well, just thinking about what I want the game to do, 231 00:12:26,410 --> 00:12:28,600 I could decide how I would like the game to work. 232 00:12:28,600 --> 00:12:31,480 And maybe the crystals are going to fall from the sky 233 00:12:31,480 --> 00:12:35,780 and the cat has to try to catch the crystals before they hit the ground. 234 00:12:35,780 --> 00:12:37,870 So that's ultimately what I would like to happen. 235 00:12:37,870 --> 00:12:41,170 The cat's going to try and catch crystals before they hit the ground. 236 00:12:41,170 --> 00:12:41,990 I have a cat. 237 00:12:41,990 --> 00:12:43,120 I have a crystal. 238 00:12:43,120 --> 00:12:48,260 But now I need to add code to figure out how to make that work. 239 00:12:48,260 --> 00:12:50,060 Well, what do I need? 240 00:12:50,060 --> 00:12:52,540 Well, first I need some way for the cat to move around. 241 00:12:52,540 --> 00:12:55,060 And I'll have the cat move around with the arrow keys. 242 00:12:55,060 --> 00:12:59,240 So let's add an event. 243 00:12:59,240 --> 00:13:06,920 When the left arrow is clicked, we're going to change x by negative 10. 244 00:13:06,920 --> 00:13:09,300 And I'll duplicate this as well. 245 00:13:09,300 --> 00:13:14,370 And I'll say that when the right arrow is clicked, let's change x by 10. 246 00:13:14,370 --> 00:13:16,370 So now I have the ability to move the cat. 247 00:13:16,370 --> 00:13:20,362 It can move around to the left and it can move around to the right. 248 00:13:20,362 --> 00:13:21,320 And that's what I want. 249 00:13:21,320 --> 00:13:24,163 As the crystals are falling, the cat's going 250 00:13:24,163 --> 00:13:26,330 to move left and right to try and catch the crystal. 251 00:13:26,330 --> 00:13:29,288 I don't need the cat to move up and down, because really the cat's only 252 00:13:29,288 --> 00:13:34,370 going to exist along this bottom portion of the stage here. 253 00:13:34,370 --> 00:13:36,780 And now when the game starts, what should happen? 254 00:13:36,780 --> 00:13:40,190 Well, the game's going to start when the flag is clicked. 255 00:13:40,190 --> 00:13:43,280 And when the flag is clicked, I'd like for the cat 256 00:13:43,280 --> 00:13:47,070 to go back to the middle of the stage just as a starting point. 257 00:13:47,070 --> 00:13:50,480 So I'll have the cat first go to 0 for x. 258 00:13:50,480 --> 00:13:53,120 And we'll go negative 125 for y. 259 00:13:53,120 --> 00:13:58,820 So this now is the starting point for the cat at 0 for x, negative 125 for y. 260 00:13:58,820 --> 00:14:04,310 And from there, I can use the arrow keys to move back and forth. 261 00:14:04,310 --> 00:14:06,740 And because it's a game, it's probably helpful for me 262 00:14:06,740 --> 00:14:09,360 to tell the user how to play the game. 263 00:14:09,360 --> 00:14:13,850 So let's add a message for the cat to say. 264 00:14:13,850 --> 00:14:18,680 And the cat's going to say something like, catch the crystals 265 00:14:18,680 --> 00:14:23,525 without letting them hit the ground. 266 00:14:23,525 --> 00:14:24,650 Those are the instructions. 267 00:14:24,650 --> 00:14:27,320 Catch the crystals without letting them hit the ground. 268 00:14:27,320 --> 00:14:29,480 And I'll have the cat say that for let's say 269 00:14:29,480 --> 00:14:32,480 four seconds to give the user enough time to see that message 270 00:14:32,480 --> 00:14:34,740 and then respond to it. 271 00:14:34,740 --> 00:14:37,190 So we're going to start. 272 00:14:37,190 --> 00:14:37,850 The cat speaks. 273 00:14:37,850 --> 00:14:40,250 Catch the crystals without letting them hit the ground. 274 00:14:40,250 --> 00:14:43,260 And now we have the ability to move around. 275 00:14:43,260 --> 00:14:45,210 That's a pretty good start. 276 00:14:45,210 --> 00:14:48,613 Now let's see if we can do something with this crystal. 277 00:14:48,613 --> 00:14:51,530 Now, one thing you might have noticed is that when I started the game, 278 00:14:51,530 --> 00:14:54,350 the crystal's already just hanging around here. 279 00:14:54,350 --> 00:14:57,187 Maybe I'd like to not show the crystal until the cat's 280 00:14:57,187 --> 00:14:58,520 done giving in the instructions. 281 00:14:58,520 --> 00:15:02,420 I want the cat to give the instructions and then the crystal can show itself 282 00:15:02,420 --> 00:15:04,380 and we can start playing the game. 283 00:15:04,380 --> 00:15:05,670 How would I do that? 284 00:15:05,670 --> 00:15:08,900 Well, I would say for the crystal when the flag is clicked, 285 00:15:08,900 --> 00:15:15,710 when this program starts running, let's go under Looks and let's hide. 286 00:15:15,710 --> 00:15:19,580 So initially when I start the program, all that we see is the cat. 287 00:15:19,580 --> 00:15:21,630 The cat gives the instructions. 288 00:15:21,630 --> 00:15:24,110 And now that the instructions are over, I 289 00:15:24,110 --> 00:15:27,650 would like the cat to somehow signal to the crystal, all right, it's OK for you 290 00:15:27,650 --> 00:15:32,500 to create a crystal to now start falling from the sky. 291 00:15:32,500 --> 00:15:34,250 And maybe eventually in my game, I'm going 292 00:15:34,250 --> 00:15:37,950 to have multiple crystals moving around, but we'll start with just one. 293 00:15:37,950 --> 00:15:39,450 And what is this going to look like? 294 00:15:39,450 --> 00:15:43,910 How does the cat signal to the crystal that we're done giving instructions? 295 00:15:43,910 --> 00:15:46,790 It's time for the crystal to now appear. 296 00:15:46,790 --> 00:15:48,140 Well, we can use broadcasting. 297 00:15:48,140 --> 00:15:51,170 Remember that broadcasting allows one sprite to send 298 00:15:51,170 --> 00:15:54,710 a message to another sprite to indicate that it's now their turn 299 00:15:54,710 --> 00:15:59,420 to do something or say something or appear on the stage. 300 00:15:59,420 --> 00:16:03,170 So I'll go to Events and we'll broadcast a message. 301 00:16:03,170 --> 00:16:09,900 And the message will be a new message just called begin. 302 00:16:09,900 --> 00:16:13,150 Begin is going to mean, all right, now is the time to actually begin the game. 303 00:16:13,150 --> 00:16:15,050 We're done giving the instructions. 304 00:16:15,050 --> 00:16:20,420 And now the crystal needs to respond to that begin message. 305 00:16:20,420 --> 00:16:24,588 So when I receive begin, now the crystal can decide what to do. 306 00:16:24,588 --> 00:16:26,380 But as I'm thinking ahead to the way that I 307 00:16:26,380 --> 00:16:28,540 want to organize the code in this game, I'm 308 00:16:28,540 --> 00:16:31,480 realizing that I might not just want one crystal. 309 00:16:31,480 --> 00:16:33,700 I might not just have one crystal that's falling, 310 00:16:33,700 --> 00:16:36,100 but I probably want to have multiple crystals falling. 311 00:16:36,100 --> 00:16:39,040 Once one crystal falls, I want another crystal to appear, 312 00:16:39,040 --> 00:16:42,890 and maybe there are eventually going to be multiple crystals at the same time. 313 00:16:42,890 --> 00:16:46,090 And so thinking ahead, when I receive begin, 314 00:16:46,090 --> 00:16:50,890 let's go ahead and create a clone of the crystal. 315 00:16:50,890 --> 00:16:52,600 Just create a brand new crystal. 316 00:16:52,600 --> 00:16:55,120 And every time I create a clone of a crystal, 317 00:16:55,120 --> 00:16:59,960 I'll get a new crystal that's going to start falling from the sky. 318 00:16:59,960 --> 00:17:01,330 And so what should happen? 319 00:17:01,330 --> 00:17:09,260 When the crystal starts as a clone, let's start now by showing the crystal. 320 00:17:09,260 --> 00:17:10,329 So how is this working? 321 00:17:10,329 --> 00:17:12,640 At first the crystal is going to hide itself. 322 00:17:12,640 --> 00:17:15,010 But once we receive the begin message from the cat, 323 00:17:15,010 --> 00:17:18,579 once the cat is done giving the instructions and the cat says begin, 324 00:17:18,579 --> 00:17:21,579 we're going to create a new clone of the crystal. 325 00:17:21,579 --> 00:17:25,670 And when the crystal starts as a clone, the crystal will show itself. 326 00:17:25,670 --> 00:17:28,780 So we see the instructions from the cat. 327 00:17:28,780 --> 00:17:33,410 And then after the instructions are over, we now see the crystal up here. 328 00:17:33,410 --> 00:17:35,900 We'd like for that crystal to go somewhere. 329 00:17:35,900 --> 00:17:38,708 So let's have the crystal go to-- 330 00:17:38,708 --> 00:17:40,000 we'll have it centered for now. 331 00:17:40,000 --> 00:17:41,590 We'll have it centered at 0. 332 00:17:41,590 --> 00:17:43,240 And y should be pretty big. 333 00:17:43,240 --> 00:17:45,760 I'll make it 160 just to make it a little taller. 334 00:17:45,760 --> 00:17:48,520 And so now the cat gives the instructions. 335 00:17:48,520 --> 00:17:51,370 And after the instructions are over, the crystal 336 00:17:51,370 --> 00:17:54,730 appears right at the top up high at the top of the screen. 337 00:17:54,730 --> 00:17:57,130 But now I'd like the crystal to start falling. 338 00:17:57,130 --> 00:17:58,460 It's up in the sky. 339 00:17:58,460 --> 00:18:00,760 Now I need to get it to move down. 340 00:18:00,760 --> 00:18:04,600 Well, I can move down just by changing the y value. 341 00:18:04,600 --> 00:18:08,140 If I change the y value by a negative number, like negative 1, 342 00:18:08,140 --> 00:18:10,990 or if I wanted a little faster, negative 2 for example, 343 00:18:10,990 --> 00:18:14,050 that's going to cause the crystal to move down. 344 00:18:14,050 --> 00:18:18,167 But now I don't just want the crystal to move down once, 345 00:18:18,167 --> 00:18:20,500 but I want the crystal to move down over and over again, 346 00:18:20,500 --> 00:18:24,730 to move down a little bit and then keep moving down again and again and again. 347 00:18:24,730 --> 00:18:31,790 So I'll put all of that under control inside of a forever loop. 348 00:18:31,790 --> 00:18:33,370 So now here's what's happening. 349 00:18:33,370 --> 00:18:36,790 When we start a new crystal, the crystal is going to show itself. 350 00:18:36,790 --> 00:18:39,040 It's going to go up to the top of the screen, 351 00:18:39,040 --> 00:18:42,430 and then forever it's just going to start falling. 352 00:18:42,430 --> 00:18:44,500 So now we can see what happens. 353 00:18:44,500 --> 00:18:46,450 The cat gives the instructions. 354 00:18:46,450 --> 00:18:52,050 The crystal appears, and the crystal now is falling. 355 00:18:52,050 --> 00:18:54,230 And so this is often what the development process 356 00:18:54,230 --> 00:18:55,130 is like for the game. 357 00:18:55,130 --> 00:18:57,260 It's not that you're going to write all of the blocks 358 00:18:57,260 --> 00:18:59,870 and put together all the different sprites for the entire game 359 00:18:59,870 --> 00:19:01,903 and then run it and see the finished product. 360 00:19:01,903 --> 00:19:04,320 It's that you're going to build it a little bit at a time. 361 00:19:04,320 --> 00:19:05,300 I need the cat to move. 362 00:19:05,300 --> 00:19:07,400 I need the cat to speak some instructions. 363 00:19:07,400 --> 00:19:08,823 I need the crystal to appear. 364 00:19:08,823 --> 00:19:10,490 Now I need the crystal to start falling. 365 00:19:10,490 --> 00:19:12,980 And after each time, you add just a few blocks 366 00:19:12,980 --> 00:19:14,660 to change the behavior a little bit. 367 00:19:14,660 --> 00:19:16,638 You can run the project, see what happens, 368 00:19:16,638 --> 00:19:18,680 and if you need to tinker with some of the blocks 369 00:19:18,680 --> 00:19:21,650 to make it work if it didn't work the way you wanted it to work. 370 00:19:21,650 --> 00:19:25,620 And we're now just building this game slowly one piece at a time. 371 00:19:25,620 --> 00:19:26,780 So what do we have so far? 372 00:19:26,780 --> 00:19:28,310 We have a cat that can move around. 373 00:19:28,310 --> 00:19:29,250 That works. 374 00:19:29,250 --> 00:19:32,810 We have a crystal that appears from the sky and starts to fall. 375 00:19:32,810 --> 00:19:36,950 But right now nothing yet happens once the cat catches the crystal. 376 00:19:36,950 --> 00:19:39,620 Once the crystal touches the cat. 377 00:19:39,620 --> 00:19:40,860 So let's add that. 378 00:19:40,860 --> 00:19:42,540 How can we do that? 379 00:19:42,540 --> 00:19:44,630 Well, that sounds like a condition. 380 00:19:44,630 --> 00:19:48,770 We're checking if the crystal is touching the cat. 381 00:19:48,770 --> 00:19:52,700 So let's add an if statement into the loop. 382 00:19:52,700 --> 00:19:58,790 And we'll say if we're touching not the mouse pointer but the cat. 383 00:19:58,790 --> 00:20:03,060 If the crystal is touching the cat, well then what should happen? 384 00:20:03,060 --> 00:20:04,760 Well, I want to somehow keep score. 385 00:20:04,760 --> 00:20:08,810 I want to keep track of how many crystals has the cat caught. 386 00:20:08,810 --> 00:20:11,060 And so to do that I need to store information 387 00:20:11,060 --> 00:20:12,860 somewhere inside of my Scratch project. 388 00:20:12,860 --> 00:20:15,710 Sounds like we need a variable, some variable that's 389 00:20:15,710 --> 00:20:18,570 going to keep track of this information for us. 390 00:20:18,570 --> 00:20:19,760 So let's go to Variable. 391 00:20:19,760 --> 00:20:21,230 Let's make a new variable. 392 00:20:21,230 --> 00:20:24,080 And I will call this variable catches. 393 00:20:24,080 --> 00:20:25,580 That's going to represent the score. 394 00:20:25,580 --> 00:20:30,710 It's how many catches the cat has made, how many crystals the cat has caught. 395 00:20:30,710 --> 00:20:33,410 You'll notice the catches right now is 0. 396 00:20:33,410 --> 00:20:38,330 And I would like to make sure that when the game first begins that we always 397 00:20:38,330 --> 00:20:41,480 reset the number of catches back to 0. 398 00:20:41,480 --> 00:20:44,298 So inside the cat, you'll notice that when the flag is clicked 399 00:20:44,298 --> 00:20:45,590 we're doing a lot of resetting. 400 00:20:45,590 --> 00:20:49,340 We're going to this original x equals 0 position to center 401 00:20:49,340 --> 00:20:50,930 the cat at the beginning. 402 00:20:50,930 --> 00:20:54,680 At the same time, let me set catches to 0 at the beginning. 403 00:20:54,680 --> 00:20:58,310 Because if I accumulate a score and I play the game again, 404 00:20:58,310 --> 00:20:59,690 I want to reset the score. 405 00:20:59,690 --> 00:21:04,670 I want the score to go back to 0, and then I can start adding to the score. 406 00:21:04,670 --> 00:21:06,170 How do I add to the score? 407 00:21:06,170 --> 00:21:08,720 Well, that's going to happen in the crystal sprite 408 00:21:08,720 --> 00:21:10,550 when it's touching the cat. 409 00:21:10,550 --> 00:21:14,120 If we're touching the cat, we have now caught one additional crystal. 410 00:21:14,120 --> 00:21:16,940 Let's change catches by one. 411 00:21:16,940 --> 00:21:19,290 And now what should happen? 412 00:21:19,290 --> 00:21:21,740 Well, now once the cat has caught a crystal, 413 00:21:21,740 --> 00:21:26,510 let's create a new crystal for the cat to try to catch. 414 00:21:26,510 --> 00:21:30,140 To do that, I can go back to control. 415 00:21:30,140 --> 00:21:32,180 And I can create another clone. 416 00:21:32,180 --> 00:21:36,140 Every time I create a clone, let's go ahead and stop this for now, 417 00:21:36,140 --> 00:21:40,430 every time I create a new clone, that is going to create a new crystal. 418 00:21:40,430 --> 00:21:42,680 And whenever we start as a clone, that crystal 419 00:21:42,680 --> 00:21:46,400 is going to go to the top of the screen and it's going to start to fall. 420 00:21:46,400 --> 00:21:51,950 And after I create a clone, let's go ahead and delete the current clone. 421 00:21:51,950 --> 00:21:53,070 So what's going on here? 422 00:21:53,070 --> 00:21:54,860 Why have I added these blocks into place? 423 00:21:54,860 --> 00:21:57,485 Well, when we're touching the cat, that's one additional catch. 424 00:21:57,485 --> 00:21:59,780 We're updating the variable, changing catches by one. 425 00:21:59,780 --> 00:22:05,810 And creating a new clone, because once the cat has caught one crystal, 426 00:22:05,810 --> 00:22:08,900 I'd like for a new crystal to appear at the top of the screen. 427 00:22:08,900 --> 00:22:12,080 But then finally, I'm deleting this clone. 428 00:22:12,080 --> 00:22:14,667 Once the cat has caught the crystal, I'm just 429 00:22:14,667 --> 00:22:17,250 going to delete that crystal, because I don't need it anymore. 430 00:22:17,250 --> 00:22:22,080 I have a new crystal that's going to be falling from the top of the screen 431 00:22:22,080 --> 00:22:24,200 instead. 432 00:22:24,200 --> 00:22:26,666 And so now let's try this program. 433 00:22:26,666 --> 00:22:28,790 I'll press the flag. 434 00:22:28,790 --> 00:22:32,150 The cat gives some instructions, catches, resets back to 0, 435 00:22:32,150 --> 00:22:34,490 and we see the crystal falling. 436 00:22:34,490 --> 00:22:37,730 And when the cat catches it, notice the catches goes up by one. 437 00:22:37,730 --> 00:22:40,095 The score has increased and a new crystal appears. 438 00:22:40,095 --> 00:22:41,720 And you'll notice this keeps happening. 439 00:22:41,720 --> 00:22:42,470 It's in a loop. 440 00:22:42,470 --> 00:22:46,370 Every time a crystal is caught, our score goes up by one. 441 00:22:46,370 --> 00:22:51,170 And the crystal creates a new crystal that appears at the top of the screen. 442 00:22:51,170 --> 00:22:56,060 And then the crystal that the cat caught disappears and we no longer see it. 443 00:22:56,060 --> 00:22:58,850 So we're just seeing this flow of crystals appear one at a time. 444 00:22:58,850 --> 00:23:02,420 We're seeing our variable update and increase by one every time 445 00:23:02,420 --> 00:23:03,800 the cat catches a crystal. 446 00:23:03,800 --> 00:23:07,020 And ultimately, this seems to now work pretty well. 447 00:23:07,020 --> 00:23:10,455 The problem that I see is that the game is a little bit too easy. 448 00:23:10,455 --> 00:23:12,080 I'm not even at the computer right now. 449 00:23:12,080 --> 00:23:15,020 I'm not moving the cat at all and the crystal is just 450 00:23:15,020 --> 00:23:17,390 appearing right on top of the cat and the cat 451 00:23:17,390 --> 00:23:20,310 is catching it every single time. 452 00:23:20,310 --> 00:23:21,920 So that's maybe not quite what I want. 453 00:23:21,920 --> 00:23:25,290 What I'd like to do is introduce a bit of randomness into a game. 454 00:23:25,290 --> 00:23:27,590 And this is a common property of games, randomness, 455 00:23:27,590 --> 00:23:30,440 because you don't want the game to be exactly the same every time. 456 00:23:30,440 --> 00:23:32,900 You want some different choices to be made sometimes, 457 00:23:32,900 --> 00:23:35,210 a little bit of unpredictability so that the user 458 00:23:35,210 --> 00:23:37,880 is going to have to respond to whatever they see. 459 00:23:37,880 --> 00:23:40,850 And in this case, the unpredictability that I want 460 00:23:40,850 --> 00:23:43,340 is the x value of the crystal. 461 00:23:43,340 --> 00:23:45,950 That right now the x value of the crystal is always 0. 462 00:23:45,950 --> 00:23:49,190 It's always going to x equals 0. 463 00:23:49,190 --> 00:23:51,050 And because of that, the crystal is always 464 00:23:51,050 --> 00:23:53,210 just immediately on top of the cat. 465 00:23:53,210 --> 00:23:57,020 What I'd like to happen is for the crystal to sometimes be on the left 466 00:23:57,020 --> 00:23:58,400 and sometimes be on the right. 467 00:23:58,400 --> 00:24:00,920 That way I have to move around rather than just keep 468 00:24:00,920 --> 00:24:03,930 increasing my score by doing nothing. 469 00:24:03,930 --> 00:24:05,610 So let's go ahead and do that. 470 00:24:05,610 --> 00:24:09,080 I will come over here and stop my project. 471 00:24:09,080 --> 00:24:12,290 And instead of going to x equals 0 every time 472 00:24:12,290 --> 00:24:15,200 the crystal starts, let's add some randomness. 473 00:24:15,200 --> 00:24:16,550 I go under Operators. 474 00:24:16,550 --> 00:24:18,290 Here's pick random. 475 00:24:18,290 --> 00:24:21,150 And far left, that's going to be a big negative number. 476 00:24:21,150 --> 00:24:24,410 So we'll go from negative 200 to the far right. 477 00:24:24,410 --> 00:24:26,210 That's going to be a big positive number. 478 00:24:26,210 --> 00:24:27,710 I'll make it positive 200. 479 00:24:27,710 --> 00:24:30,960 But you could play around with those values for whatever you'd like. 480 00:24:30,960 --> 00:24:36,020 And now when I start the game, the score resets and notice 481 00:24:36,020 --> 00:24:38,810 that the crystal is going to appear. 482 00:24:38,810 --> 00:24:42,140 This time it was kind of centered, but maybe it won't be that way every time. 483 00:24:42,140 --> 00:24:45,260 Now the crystal's off to the right, and I have to move to the right 484 00:24:45,260 --> 00:24:47,430 in order to catch it. 485 00:24:47,430 --> 00:24:49,670 And every time I catch the crystal, the crystal 486 00:24:49,670 --> 00:24:54,650 is going to generate a new random number between negative 200 and 200. 487 00:24:54,650 --> 00:24:58,140 And that's going to tell me where I need to go in order to catch the crystal. 488 00:24:58,140 --> 00:25:02,030 This is a much more interesting game than when the crystal was just always 489 00:25:02,030 --> 00:25:05,790 appearing in the same spot every time. 490 00:25:05,790 --> 00:25:08,270 So this is definitely an improvement. 491 00:25:08,270 --> 00:25:10,700 And now I'm curious, what happens if I miss? 492 00:25:10,700 --> 00:25:14,900 If I miss the crystal altogether and just let the crystal fall. 493 00:25:14,900 --> 00:25:17,840 All right, the crystal hits the bottom of the screen 494 00:25:17,840 --> 00:25:20,570 and now the game is kind of stuck. 495 00:25:20,570 --> 00:25:23,930 What I'd like to do is add some code that can handle this situation. 496 00:25:23,930 --> 00:25:27,380 What happens if the crystal ever touches the edge of the screen 497 00:25:27,380 --> 00:25:29,400 and I didn't catch it? 498 00:25:29,400 --> 00:25:31,260 What should happen? 499 00:25:31,260 --> 00:25:34,460 Well, let's add another condition. 500 00:25:34,460 --> 00:25:37,130 I'll add a condition to the bottom here underneath the loop. 501 00:25:37,130 --> 00:25:40,040 Not if we're touching the cat, but this time 502 00:25:40,040 --> 00:25:43,430 if we're touching the edge of the screen. 503 00:25:43,430 --> 00:25:46,280 If ever the crystal makes it all the way down to the bottom 504 00:25:46,280 --> 00:25:50,820 and now it's touching the edge of the screen, what should we do? 505 00:25:50,820 --> 00:25:51,710 Well, we can decide. 506 00:25:51,710 --> 00:25:53,030 We haven't really talked about what's going 507 00:25:53,030 --> 00:25:54,530 to happen in the game at this point. 508 00:25:54,530 --> 00:25:56,840 Maybe the game's over right away and we're just done. 509 00:25:56,840 --> 00:25:59,150 Or maybe you get a couple of chances. 510 00:25:59,150 --> 00:26:00,560 You're given like three misses. 511 00:26:00,560 --> 00:26:02,900 And after three misses, then you lose the game. 512 00:26:02,900 --> 00:26:04,640 And let's try and do that. 513 00:26:04,640 --> 00:26:07,640 In order to make that happen, I need to keep track of how many misses 514 00:26:07,640 --> 00:26:09,300 there have been so far. 515 00:26:09,300 --> 00:26:14,480 So I'm going to go to variables and create a new variable called misses. 516 00:26:14,480 --> 00:26:18,860 How many times has the cat now missed the crystal? 517 00:26:18,860 --> 00:26:21,200 And just as in the beginning of the cat, we 518 00:26:21,200 --> 00:26:25,370 set catches to 0 to reset the value of catches at the beginning of the game, 519 00:26:25,370 --> 00:26:28,430 let's also now set misses to 0. 520 00:26:28,430 --> 00:26:31,110 When the game first begins, you haven't missed any yet. 521 00:26:31,110 --> 00:26:32,810 So let's reset it back to 0. 522 00:26:32,810 --> 00:26:37,940 That way any previous progress in the game is erased. 523 00:26:37,940 --> 00:26:41,600 But now if ever we're touching the edge, let's go ahead 524 00:26:41,600 --> 00:26:46,280 and change the number of misses by one. 525 00:26:46,280 --> 00:26:49,490 And at that point, we can do much the same thing as we were doing before. 526 00:26:49,490 --> 00:26:53,432 We're going to create a new crystal by creating a clone. 527 00:26:53,432 --> 00:26:55,640 Let's go ahead and stop this for now, otherwise we're 528 00:26:55,640 --> 00:26:57,230 going to get a lot of clones. 529 00:26:57,230 --> 00:27:02,925 But then let's also delete this clone after that. 530 00:27:02,925 --> 00:27:06,050 So when we're touching the edge, we're going to change the number of misses 531 00:27:06,050 --> 00:27:06,550 by one. 532 00:27:06,550 --> 00:27:07,970 We now have one additional miss. 533 00:27:07,970 --> 00:27:10,340 We're going to create a new crystal, and then we're 534 00:27:10,340 --> 00:27:13,760 going to delete the clone to say we're done with this crystal. 535 00:27:13,760 --> 00:27:15,750 We've now created a new one. 536 00:27:15,750 --> 00:27:19,220 So now we'll see how the game behaves. 537 00:27:19,220 --> 00:27:20,450 The crystals are falling. 538 00:27:20,450 --> 00:27:23,278 I can try to catch them. 539 00:27:23,278 --> 00:27:24,320 That's one that I caught. 540 00:27:24,320 --> 00:27:27,110 I'll catch one more. 541 00:27:27,110 --> 00:27:31,175 Now let me miss this one and see what happens. 542 00:27:31,175 --> 00:27:34,050 The crystal is going to hit the bottom of the screen and I missed it. 543 00:27:34,050 --> 00:27:36,500 You notice my misses goes up by one. 544 00:27:36,500 --> 00:27:39,590 If I miss this one as well, misses goes up again. 545 00:27:39,590 --> 00:27:42,440 And now I effectively have two variables, 546 00:27:42,440 --> 00:27:47,070 one keeping track of how many times I've caught the crystal and one keeping 547 00:27:47,070 --> 00:27:49,070 track of how many times I've missed the crystal. 548 00:27:49,070 --> 00:27:50,778 And there seems to be no limit right now. 549 00:27:50,778 --> 00:27:54,050 Misses can just keep going up and up and up every time I miss the crystal. 550 00:27:54,050 --> 00:27:58,400 What I'd like to do is do something once I miss three of them. 551 00:27:58,400 --> 00:28:02,160 Once I miss three, I want the game to be over. 552 00:28:02,160 --> 00:28:05,090 So let's add a condition to check for that. 553 00:28:05,090 --> 00:28:06,590 I'm going to take this if condition. 554 00:28:06,590 --> 00:28:09,110 I'll build it out here for now and then I'll drag it in, 555 00:28:09,110 --> 00:28:11,060 just to keep it separate for now. 556 00:28:11,060 --> 00:28:15,590 But I'm going to say if the number of misses is equal to three, 557 00:28:15,590 --> 00:28:19,220 so equals is an operator, and I'm going to say if the number of misses 558 00:28:19,220 --> 00:28:22,350 are now equal to three, well what should we do? 559 00:28:22,350 --> 00:28:24,440 Well, the game is now going to be over. 560 00:28:24,440 --> 00:28:28,670 So I'm going to eventually delete this clone, because I no longer need 561 00:28:28,670 --> 00:28:30,930 the crystal once the game is over. 562 00:28:30,930 --> 00:28:34,250 But at this point as well, I need some way 563 00:28:34,250 --> 00:28:37,550 to signal to the rest of the program that the game is over. 564 00:28:37,550 --> 00:28:39,350 I need the cat to know the game is over. 565 00:28:39,350 --> 00:28:42,590 Because maybe the cat is now going to report what my score was. 566 00:28:42,590 --> 00:28:44,390 What is my score at the end of the game? 567 00:28:44,390 --> 00:28:46,760 How many crystals did I catch? 568 00:28:46,760 --> 00:28:51,068 So how can the crystal tell the cat that something has happened in the project? 569 00:28:51,068 --> 00:28:52,610 Well, that's going to be a broadcast. 570 00:28:52,610 --> 00:28:56,750 Broadcast as a way of sending messages, again, between sprites. 571 00:28:56,750 --> 00:28:58,590 I'll broadcast a message. 572 00:28:58,590 --> 00:29:02,720 But instead of begin, the message is going to be game over. 573 00:29:02,720 --> 00:29:03,800 The game is over. 574 00:29:03,800 --> 00:29:06,080 Let's make sure the cat knows that. 575 00:29:06,080 --> 00:29:12,740 And let's drag that into this logic right after we change misses by one. 576 00:29:12,740 --> 00:29:15,350 So we increase the number of misses, and then we check. 577 00:29:15,350 --> 00:29:17,270 Is our number of misses equal to three? 578 00:29:17,270 --> 00:29:20,060 If so we broadcast that the game is over. 579 00:29:20,060 --> 00:29:27,050 And now last step here, in the cat I need to respond to that event. 580 00:29:27,050 --> 00:29:31,770 When I receive game over, what should happen? 581 00:29:31,770 --> 00:29:35,780 Well, when I receive game over, let's go ahead and say the score. 582 00:29:35,780 --> 00:29:39,500 Let's say-- and I could just say the score directly. 583 00:29:39,500 --> 00:29:42,200 But to make it a little nicer, I'll join together two words. 584 00:29:42,200 --> 00:29:47,810 I'll say your score is and then a space and then 585 00:29:47,810 --> 00:29:50,900 I'll drag in the number of catches. 586 00:29:50,900 --> 00:29:53,030 And I'll say that for five seconds. 587 00:29:53,030 --> 00:29:55,790 So when the game is over, when I receive that message, 588 00:29:55,790 --> 00:29:59,420 the cat is going to say your score is this for five seconds 589 00:29:59,420 --> 00:30:01,933 and then the game will be over. 590 00:30:01,933 --> 00:30:02,600 So let's try it. 591 00:30:02,600 --> 00:30:03,308 I start the game. 592 00:30:03,308 --> 00:30:06,920 Catches and misses go back to 0, I get some instructions from the cat 593 00:30:06,920 --> 00:30:09,290 and I can start catching. 594 00:30:09,290 --> 00:30:10,830 I'll catch one. 595 00:30:10,830 --> 00:30:12,680 I'll catch a second one. 596 00:30:12,680 --> 00:30:14,180 My catches are now two. 597 00:30:14,180 --> 00:30:18,620 But now I'm going to let the crystals just keep falling. 598 00:30:18,620 --> 00:30:19,370 We missed one. 599 00:30:19,370 --> 00:30:22,992 600 00:30:22,992 --> 00:30:24,450 We're going to miss the second one. 601 00:30:24,450 --> 00:30:25,500 And I'll miss a third one. 602 00:30:25,500 --> 00:30:27,375 I'll get out of the way just to test out what 603 00:30:27,375 --> 00:30:29,520 happens when I actually lose the game. 604 00:30:29,520 --> 00:30:30,690 That's my third miss. 605 00:30:30,690 --> 00:30:35,700 We broadcast that the game is over, and the cat reports that my score is two. 606 00:30:35,700 --> 00:30:38,250 So at this point, we now have a completed game that 607 00:30:38,250 --> 00:30:40,020 does sort of what we wanted it to do. 608 00:30:40,020 --> 00:30:41,400 The cat can move around. 609 00:30:41,400 --> 00:30:43,230 Crystals are falling from the sky. 610 00:30:43,230 --> 00:30:46,680 The cat's able to try and catch as many of those crystals as we can. 611 00:30:46,680 --> 00:30:49,345 And you could now play this game, share it with others. 612 00:30:49,345 --> 00:30:51,720 And the nice thing is that you can always add to the game 613 00:30:51,720 --> 00:30:54,660 if you think of new things you'd like to try inside of the game 614 00:30:54,660 --> 00:30:56,040 just to experiment with it. 615 00:30:56,040 --> 00:31:00,190 Maybe, for example, right now I think the game is maybe a little too easy. 616 00:31:00,190 --> 00:31:02,670 There's always just one crystal falling from the sky 617 00:31:02,670 --> 00:31:04,570 and I know how to move around to catch it. 618 00:31:04,570 --> 00:31:05,920 So let's try something. 619 00:31:05,920 --> 00:31:10,420 Let's make the game a little bit more challenging. 620 00:31:10,420 --> 00:31:13,060 Where am I creating crystals in the game? 621 00:31:13,060 --> 00:31:15,400 Well, I'm creating crystals here. 622 00:31:15,400 --> 00:31:18,430 When I receive begin, we're creating a clone of the crystal 623 00:31:18,430 --> 00:31:21,010 to create a brand new crystal. 624 00:31:21,010 --> 00:31:24,160 But let's make that a little more exciting. 625 00:31:24,160 --> 00:31:27,670 I'm going to put this in a forever loop, meaning we're always 626 00:31:27,670 --> 00:31:29,260 going to be creating crystals. 627 00:31:29,260 --> 00:31:33,340 Now, if I just did that to create lots and lots of crystals, what you'll see 628 00:31:33,340 --> 00:31:37,600 is something like this where suddenly hundreds of crystals 629 00:31:37,600 --> 00:31:40,220 are falling from the sky all at the same time. 630 00:31:40,220 --> 00:31:43,180 That's maybe a little bit too challenging. 631 00:31:43,180 --> 00:31:47,080 But let's add a wait here and say create a crystal 632 00:31:47,080 --> 00:31:49,900 and then wait 15 seconds or so. 633 00:31:49,900 --> 00:31:52,450 We're going to wait 15 seconds and then create 634 00:31:52,450 --> 00:31:56,110 a new crystal so that a new crystal appears every 15 seconds. 635 00:31:56,110 --> 00:31:58,917 Every 15 seconds, the game gets a little bit harder. 636 00:31:58,917 --> 00:32:00,250 And so now I could try it again. 637 00:32:00,250 --> 00:32:02,667 And you could play with that number to see what it's like. 638 00:32:02,667 --> 00:32:04,960 But let's try playing this game. 639 00:32:04,960 --> 00:32:10,020 I'm going to catch a crystal and keep doing this a few times. 640 00:32:10,020 --> 00:32:14,020 And after 15 seconds or so, which should be in just a couple of seconds now, 641 00:32:14,020 --> 00:32:17,890 we'll see what happens, it should create a second clone. 642 00:32:17,890 --> 00:32:20,890 And now you'll notice there are two crystals that have to try and catch. 643 00:32:20,890 --> 00:32:21,515 I'll catch one. 644 00:32:21,515 --> 00:32:22,740 Can I get the other one? 645 00:32:22,740 --> 00:32:25,400 All right, I caught the other one. 646 00:32:25,400 --> 00:32:29,110 And in another 15 seconds, we're going to see the game get even harder. 647 00:32:29,110 --> 00:32:32,470 648 00:32:32,470 --> 00:32:34,540 Now if you notice, there are three crystals that 649 00:32:34,540 --> 00:32:36,667 are all out there at the same time. 650 00:32:36,667 --> 00:32:38,750 And maybe I'm going to start to miss some of them. 651 00:32:38,750 --> 00:32:42,130 I missed one already, and I'm probably going to miss a couple more now. 652 00:32:42,130 --> 00:32:48,190 And now I missed three, and it's telling me that my score is 14. 653 00:32:48,190 --> 00:32:50,390 And the crystals are still falling. 654 00:32:50,390 --> 00:32:53,560 And so what could I do to stop everything after the game is over? 655 00:32:53,560 --> 00:32:56,770 Well, after the game is over, after I say your score is something, 656 00:32:56,770 --> 00:33:01,300 I can go onto this block, stop all, and that's just going to stop everything. 657 00:33:01,300 --> 00:33:03,550 Every sprite is just going to stop operating 658 00:33:03,550 --> 00:33:07,630 after I say stop all so that the sprites don't continue to try to behave. 659 00:33:07,630 --> 00:33:09,850 Since I have multiple clones of the crystals, 660 00:33:09,850 --> 00:33:13,400 stop all will just now stop all of them. 661 00:33:13,400 --> 00:33:15,852 So hopefully you can start to see now that by combining 662 00:33:15,852 --> 00:33:18,310 these various different pieces, these different components, 663 00:33:18,310 --> 00:33:21,430 these different tools and programming from different parts of Scratch, 664 00:33:21,430 --> 00:33:25,910 we can start to create some creative and exciting projects as well. 665 00:33:25,910 --> 00:33:27,760 So now as we wrap up this course, you now 666 00:33:27,760 --> 00:33:30,280 have the tools you need to build projects of your own 667 00:33:30,280 --> 00:33:33,820 that are interesting to you by taking advantage of functions and loops 668 00:33:33,820 --> 00:33:36,490 and conditions and values and variables and events 669 00:33:36,490 --> 00:33:39,848 and more to create interesting and exciting programs of your own. 670 00:33:39,848 --> 00:33:41,140 And I'd encourage you to do so. 671 00:33:41,140 --> 00:33:43,120 Play around with this Scratch interface. 672 00:33:43,120 --> 00:33:47,095 Try and create a story or an animation or a game that's exciting to you. 673 00:33:47,095 --> 00:33:48,220 Share it with your friends. 674 00:33:48,220 --> 00:33:49,303 Share it with your family. 675 00:33:49,303 --> 00:33:51,100 And we look forward to what you create. 676 00:33:51,100 --> 00:33:52,580 Thank you so much for joining us. 677 00:33:52,580 --> 00:33:56,310 This was an introduction to Programming with Scratch. 678 00:33:56,310 --> 00:33:58,000