CS50 Video Player
    • 🧁

    • 🍧

    • 🍉

    • 🍿
    • 0:00:00Introduction
    • 0:02:57Broadcasts
    • 0:05:24Control the Duck
    • 0:08:31Visiting Fish
    • 0:09:38Stars
    • 0:11:45Crystal Catch
    • 0:00:00[MUSIC PLAYING]
    • 0:00:17BRIAN YU: Welcome back, everyone, to now our final class in this Introduction
    • 0:00:20to Programming with Scratch.
    • 0:00:22And at this point, we've seen most of the major features
    • 0:00:25that Scratch has to offer.
    • 0:00:26So today in this final class, what we thought we'd do
    • 0:00:28is show you a few additional features that you might find useful
    • 0:00:31as you go about building your own projects
    • 0:00:33and then show you how you could put the pieces together, so to speak,
    • 0:00:36and use all of the tools and the ideas that we've explored during this course
    • 0:00:40to see how you could put them together to create a complete game using
    • 0:00:44Scratch.
    • 0:00:45So let's go ahead and begin by taking a look at a couple final features
    • 0:00:48of Scratch to introduce.
    • 0:00:50And the first involves how it is that sprites communicate with each other.
    • 0:00:54You might recall that in an earlier project,
    • 0:00:56we had a cat talking to a dinosaur, for example.
    • 0:00:59And how we did that was something like this.
    • 0:01:01I had a cat, and then I added the dinosaur, which
    • 0:01:05I'll go ahead and find and bring back.
    • 0:01:07I wanted the cat and the dinosaur to be facing each other.
    • 0:01:10So I took the dinosaur and I set its rotation
    • 0:01:12to left right and then set its direction to negative 90
    • 0:01:16so it was facing the other way.
    • 0:01:18And if I wanted the cat and the dinosaur to greet each other,
    • 0:01:21then I would have the cat when the flag is clicked do something like say hello
    • 0:01:29to the dinosaur.
    • 0:01:32And then likewise for the dinosaur, if I want the dinosaur to respond
    • 0:01:35to the cat saying hello, well, the cat is
    • 0:01:38saying hello dinosaur for two seconds.
    • 0:01:40So the dinosaur better wait two seconds before it responds.
    • 0:01:44So the dinosaur, I did something like this.
    • 0:01:46I said when the flag is clicked, let's go under Control.
    • 0:01:49Let's wait two seconds and then say hello to the cat.
    • 0:02:00So now when I run the project, the cat says hello dinosaur.
    • 0:02:03And immediately after, the dinosaur says hello cat.
    • 0:02:07What if I later wanted to make a change?
    • 0:02:09I want the cat to only say hello dinosaur for maybe one second,
    • 0:02:12for example.
    • 0:02:13Well, now the cat speaks and I have this weird pause
    • 0:02:17before the dinosaur responds, because I forgot to change
    • 0:02:20how long the dinosaur was waiting.
    • 0:02:22I'd also have to change that from two to one
    • 0:02:25in order to get it to respond appropriately.
    • 0:02:27Ultimately, this is going to start to get more complex, especially as there
    • 0:02:30are more interactions between these two sprites
    • 0:02:33or if later I add other sprites altogether
    • 0:02:35for me to have to calculate exactly how long every sprite should
    • 0:02:38be waiting in between one thing happening and another thing happening.
    • 0:02:42What would be better is if the cat when it was done speaking
    • 0:02:46could somehow signal to the dinosaur that it's now the dinosaurs turn
    • 0:02:50to start speaking.
    • 0:02:51That way I, the programmer, don't have to do
    • 0:02:53all this math of figuring out exactly how
    • 0:02:56long these sprites should be waiting.
    • 0:02:58And so this brings us to one of our final topics
    • 0:03:00in Scratch, which is called broadcasting.
    • 0:03:03Broadcasting is the ability for one object in Scratch, one sprite,
    • 0:03:07for example, to send some message that other sprites like the dinosaur,
    • 0:03:12in this case, could receive.
    • 0:03:14So we have the ability to send or broadcast messages.
    • 0:03:17And then we have the ability for other sprites to receive those messages.
    • 0:03:21When they receive a message, the sprites are going to respond in some way.
    • 0:03:25So what would that look like?
    • 0:03:26How could we broadcast a message from the cat to the dinosaur
    • 0:03:30so that the dinosaur knows that it's time to greet the cat?
    • 0:03:34Let's go back to the cat.
    • 0:03:36And after the cat says hello dinosaur for let's go back to two seconds,
    • 0:03:41then under Event, I'm going to use this broadcast block to broadcast a message.
    • 0:03:48And I'm going to choose a new message from the dropdown.
    • 0:03:51And I can just name this message.
    • 0:03:52This is any message that I'm now going to send.
    • 0:03:55And I'm going to call the message greet, just because it's
    • 0:03:58going to be a message that signals to the dinosaur
    • 0:04:01that it's time to greet the cat.
    • 0:04:03So the cat says hello dinosaur and then broadcasts greet.
    • 0:04:08And the dinosaur now instead of saying hello cat when the flag is clicked,
    • 0:04:13I'm going to get rid of the when flag is clicked event entirely
    • 0:04:16and replace it with this event here when I receive greet.
    • 0:04:22So the cat is first going to say hello dinosaur for two seconds
    • 0:04:26and then broadcast this message.
    • 0:04:28The message is greet.
    • 0:04:30And then the dinosaur when it receives the greet message is then
    • 0:04:34going to say hello to the cat.
    • 0:04:36And so now the cat says hello dinosaur, and immediately the dinosaur
    • 0:04:41responds hello cat.
    • 0:04:42The cat broadcasted a message and the dinosaur received it.
    • 0:04:46And now it doesn't matter if there's a delay.
    • 0:04:48I could add a delay of a second to the cat.
    • 0:04:52And so the cat will now wait a second before speaking,
    • 0:04:55but the dinosaur still responds right on cue.
    • 0:04:57I didn't have to change any of the timing,
    • 0:05:00because the cat broadcasted a message once the cat was
    • 0:05:03done doing whatever it was doing and the dinosaur was then able to respond.
    • 0:05:08So this works great for stories where you have multiple sprites
    • 0:05:10and they're interacting in some way and you want some sprite to signal
    • 0:05:13to someone else when it's their turn to say something or do something,
    • 0:05:17for example.
    • 0:05:18It can also be used in other ways to let one sprite control
    • 0:05:22or influence the behavior of another sprite.
    • 0:05:25Let's imagine that I had a duck.
    • 0:05:27Let's add a new animal and let's go back to the duck here.
    • 0:05:34And let's say I wanted the duck to move up and down.
    • 0:05:36And we could use the arrow keys.
    • 0:05:38I could use the up and down arrow keys to control it.
    • 0:05:40But I'd like for the user just to be able to click
    • 0:05:42on up and down arrows on the screen as some user interface
    • 0:05:45that they could use as a visual on the screen that
    • 0:05:48lets them control if the duck is going to move up or move down.
    • 0:05:51So let's add that interface.
    • 0:05:53Here's an arrow.
    • 0:05:55And right now the arrow is facing the right.
    • 0:05:56But if I wanted to go up, then I'm going to rotate it instead of 90 degrees,
    • 0:06:00let's set it to 0 degrees.
    • 0:06:02That's going to be an arrow that's facing up.
    • 0:06:04And if I want a button that lets the duck go down, let's add an arrow.
    • 0:06:10And this time set it to 180 degrees.
    • 0:06:13And now the arrow is facing down.
    • 0:06:16So now I have two arrows.
    • 0:06:18But clicking these arrows doesn't achieve anything.
    • 0:06:22They just do nothing right now because I haven't added any code to them.
    • 0:06:25But I could.
    • 0:06:27Let me add an event to the up arrow that says when this sprite is clicked,
    • 0:06:32when I click on the up arrow, well then let's broadcast a message.
    • 0:06:38And the message is going to be up.
    • 0:06:41That's the direction you should move.
    • 0:06:43Now let's choose the down arrow.
    • 0:06:46This time when the sprite is clicked, let's
    • 0:06:49broadcast a message not up, but this time a new message.
    • 0:06:53And the message is going to be down.
    • 0:06:55So now the up arrow is broadcasting a message that says up.
    • 0:06:58The down arrow is broadcasting a message that says down.
    • 0:07:02And now I'll have the duck respond to those broadcasts.
    • 0:07:05When the duck receives the up message, well then
    • 0:07:10let's have the duck change its y value by 10 going upward in direction.
    • 0:07:16And likewise under Events, when the duck receives the down broadcast message,
    • 0:07:22then we're going to change the y value by negative 10.
    • 0:07:28And now these arrows, these sprites on the screen,
    • 0:07:31are controlling the behavior of the duck.
    • 0:07:33If I click the up arrow, the duck moves up.
    • 0:07:36And if I click the down arrow, the duck moves down.
    • 0:07:39And they're doing that because of broadcasting.
    • 0:07:41When I click on the arrows, those arrows are themselves
    • 0:07:44sprites that are saying move up or move down.
    • 0:07:48They're broadcasting some message.
    • 0:07:49And the duck in turn is then responding to those messages.
    • 0:07:55And it doesn't just have to be sprites that are broadcasting messages.
    • 0:07:59The stage, the background can broadcast messages too.
    • 0:08:03I'll show you an example of that.
    • 0:08:05I'll delete all of these sprites for now.
    • 0:08:06Let's add a backdrop.
    • 0:08:08And let's go back to our underwater backdrop.
    • 0:08:11And let's bring our fish back.
    • 0:08:13So I'll add a new sprite and add back the fish.
    • 0:08:20And now what I would like to do is in the backdrop,
    • 0:08:23I'll click on the backdrop, add some code here,
    • 0:08:26I'll say whenever the stage is clicked, when stage clicked,
    • 0:08:31let's now broadcast a message.
    • 0:08:34And the message is going to be visit.
    • 0:08:38I would like the fish to visit wherever I click on the stage.
    • 0:08:44And now for the fish whenever it receives the visit message,
    • 0:08:49let's have the fish point towards the mouse pointer
    • 0:08:53and then take one second to glide to the mouse pointer.
    • 0:08:59So now I press the flag.
    • 0:09:00At first, nothing happens.
    • 0:09:01But if I choose a location and then click, the fish goes there.
    • 0:09:06I choose the location and click, and the fish visits wherever I happen to click.
    • 0:09:11I click over here.
    • 0:09:12The fish goes there.
    • 0:09:13And every time when I click, the backdrop is broadcasting a message.
    • 0:09:17The stage is broadcasting this visit message
    • 0:09:19and the fish is now receiving that visit message
    • 0:09:23and doing something in response, pointing towards the mouse pointer
    • 0:09:26and gliding there.
    • 0:09:27So broadcasting allows multiple different parts of our Scratch project
    • 0:09:31to communicate with each other to decide when something should happen.
    • 0:09:36And that's one very powerful, very useful feature within Scratch.
    • 0:09:40Another feature that can be useful, and for this I'll get rid of the backdrop,
    • 0:09:44I'll go back to the default white backdrop,
    • 0:09:47is the ability to clone a sprite.
    • 0:09:50If I have one copy of a sprite oftentimes in a game
    • 0:09:53or in some sort of other animation, I might not just one
    • 0:09:56copy of a sprite on the stage, but I might want multiple copies
    • 0:10:00of that sprite on the stage as well.
    • 0:10:03So let's add a sprite.
    • 0:10:04Let me search for the star and let's grab the star.
    • 0:10:08And maybe what I'd like to happen is to not just
    • 0:10:10have one star but to have multiple stars inside of my project.
    • 0:10:15So let's add some code here.
    • 0:10:18I'll say when this sprite is clicked, when I click on the star,
    • 0:10:23I am going to, and this is under Control, create a clone of myself.
    • 0:10:30When the star is clicked, we're going to create a clone of the star.
    • 0:10:34And that new clone is going to be another star that
    • 0:10:36can behave on its own.
    • 0:10:39Now I'll add this new event.
    • 0:10:40When I start as a clone, what should this clone star do?
    • 0:10:44Well, let's have the cloned star just glide one second to a random position.
    • 0:10:50So I have this star, and watch what happens.
    • 0:10:52Every time I click on the star, the star creates a new clone.
    • 0:10:58Every time I click, a new clone is created.
    • 0:11:00And when the new star starts as a clone, it's
    • 0:11:03going to glide to a random position.
    • 0:11:05Every time I clone a star, you'll see we create a duplicate event
    • 0:11:10and it ends up going to a random position.
    • 0:11:13So if I want multiple of a particular sprite that appear on the stage,
    • 0:11:17cloning is a great way to do that.
    • 0:11:19We can create multiple copies of a sprite
    • 0:11:21and then decide how it is that those clones should ultimately behave.
    • 0:11:27So now I'll go ahead and get rid of all those clones
    • 0:11:30just by deleting the sprite and clearing the screen.
    • 0:11:34Now we've seen a lot of features that Scratch has to offer.
    • 0:11:37We've seen functions and we've seen events.
    • 0:11:40We've seen variable, loops and conditions, and now the ability
    • 0:11:43to broadcast messages and clone sprites.
    • 0:11:45Let's take all of those ideas now and put them
    • 0:11:48together inside of a fully complete game to show you
    • 0:11:51how you could go about building a more complex project within Scratch.
    • 0:11:55And what characters should I use in the game?
    • 0:11:58Let's start and just start building this game one piece at a time.
    • 0:12:02Well, let's start by using the cat.
    • 0:12:05Sort of the classic default sprite character.
    • 0:12:08And let's have the cat have some objective.
    • 0:12:12What is the cat trying to do?
    • 0:12:13Well, I can just scroll through these sprites
    • 0:12:15and look for what seems interesting.
    • 0:12:17Here are the crystals.
    • 0:12:18Let's have the cat try to catch as many crystals as it can.
    • 0:12:23And what are the crystals going to do?
    • 0:12:24Well, just thinking about what I want the game to do,
    • 0:12:26I could decide how I would like the game to work.
    • 0:12:28And maybe the crystals are going to fall from the sky
    • 0:12:31and the cat has to try to catch the crystals before they hit the ground.
    • 0:12:35So that's ultimately what I would like to happen.
    • 0:12:37The cat's going to try and catch crystals before they hit the ground.
    • 0:12:41I have a cat.
    • 0:12:41I have a crystal.
    • 0:12:43But now I need to add code to figure out how to make that work.
    • 0:12:48Well, what do I need?
    • 0:12:50Well, first I need some way for the cat to move around.
    • 0:12:52And I'll have the cat move around with the arrow keys.
    • 0:12:55So let's add an event.
    • 0:12:59When the left arrow is clicked, we're going to change x by negative 10.
    • 0:13:06And I'll duplicate this as well.
    • 0:13:09And I'll say that when the right arrow is clicked, let's change x by 10.
    • 0:13:14So now I have the ability to move the cat.
    • 0:13:16It can move around to the left and it can move around to the right.
    • 0:13:20And that's what I want.
    • 0:13:21As the crystals are falling, the cat's going
    • 0:13:24to move left and right to try and catch the crystal.
    • 0:13:26I don't need the cat to move up and down, because really the cat's only
    • 0:13:29going to exist along this bottom portion of the stage here.
    • 0:13:34And now when the game starts, what should happen?
    • 0:13:36Well, the game's going to start when the flag is clicked.
    • 0:13:40And when the flag is clicked, I'd like for the cat
    • 0:13:43to go back to the middle of the stage just as a starting point.
    • 0:13:47So I'll have the cat first go to 0 for x.
    • 0:13:50And we'll go negative 125 for y.
    • 0:13:53So this now is the starting point for the cat at 0 for x, negative 125 for y.
    • 0:13:58And from there, I can use the arrow keys to move back and forth.
    • 0:14:04And because it's a game, it's probably helpful for me
    • 0:14:06to tell the user how to play the game.
    • 0:14:09So let's add a message for the cat to say.
    • 0:14:13And the cat's going to say something like, catch the crystals
    • 0:14:18without letting them hit the ground.
    • 0:14:23Those are the instructions.
    • 0:14:24Catch the crystals without letting them hit the ground.
    • 0:14:27And I'll have the cat say that for let's say
    • 0:14:29four seconds to give the user enough time to see that message
    • 0:14:32and then respond to it.
    • 0:14:34So we're going to start.
    • 0:14:37The cat speaks.
    • 0:14:37Catch the crystals without letting them hit the ground.
    • 0:14:40And now we have the ability to move around.
    • 0:14:43That's a pretty good start.
    • 0:14:45Now let's see if we can do something with this crystal.
    • 0:14:48Now, one thing you might have noticed is that when I started the game,
    • 0:14:51the crystal's already just hanging around here.
    • 0:14:54Maybe I'd like to not show the crystal until the cat's
    • 0:14:57done giving in the instructions.
    • 0:14:58I want the cat to give the instructions and then the crystal can show itself
    • 0:15:02and we can start playing the game.
    • 0:15:04How would I do that?
    • 0:15:05Well, I would say for the crystal when the flag is clicked,
    • 0:15:08when this program starts running, let's go under Looks and let's hide.
    • 0:15:15So initially when I start the program, all that we see is the cat.
    • 0:15:19The cat gives the instructions.
    • 0:15:21And now that the instructions are over, I
    • 0:15:24would like the cat to somehow signal to the crystal, all right, it's OK for you
    • 0:15:27to create a crystal to now start falling from the sky.
    • 0:15:32And maybe eventually in my game, I'm going
    • 0:15:34to have multiple crystals moving around, but we'll start with just one.
    • 0:15:37And what is this going to look like?
    • 0:15:39How does the cat signal to the crystal that we're done giving instructions?
    • 0:15:43It's time for the crystal to now appear.
    • 0:15:46Well, we can use broadcasting.
    • 0:15:48Remember that broadcasting allows one sprite to send
    • 0:15:51a message to another sprite to indicate that it's now their turn
    • 0:15:54to do something or say something or appear on the stage.
    • 0:15:59So I'll go to Events and we'll broadcast a message.
    • 0:16:03And the message will be a new message just called begin.
    • 0:16:09Begin is going to mean, all right, now is the time to actually begin the game.
    • 0:16:13We're done giving the instructions.
    • 0:16:15And now the crystal needs to respond to that begin message.
    • 0:16:20So when I receive begin, now the crystal can decide what to do.
    • 0:16:24But as I'm thinking ahead to the way that I
    • 0:16:26want to organize the code in this game, I'm
    • 0:16:28realizing that I might not just want one crystal.
    • 0:16:31I might not just have one crystal that's falling,
    • 0:16:33but I probably want to have multiple crystals falling.
    • 0:16:36Once one crystal falls, I want another crystal to appear,
    • 0:16:39and maybe there are eventually going to be multiple crystals at the same time.
    • 0:16:42And so thinking ahead, when I receive begin,
    • 0:16:46let's go ahead and create a clone of the crystal.
    • 0:16:50Just create a brand new crystal.
    • 0:16:52And every time I create a clone of a crystal,
    • 0:16:55I'll get a new crystal that's going to start falling from the sky.
    • 0:16:59And so what should happen?
    • 0:17:01When the crystal starts as a clone, let's start now by showing the crystal.
    • 0:17:09So how is this working?
    • 0:17:10At first the crystal is going to hide itself.
    • 0:17:12But once we receive the begin message from the cat,
    • 0:17:15once the cat is done giving the instructions and the cat says begin,
    • 0:17:18we're going to create a new clone of the crystal.
    • 0:17:21And when the crystal starts as a clone, the crystal will show itself.
    • 0:17:25So we see the instructions from the cat.
    • 0:17:28And then after the instructions are over, we now see the crystal up here.
    • 0:17:33We'd like for that crystal to go somewhere.
    • 0:17:35So let's have the crystal go to--
    • 0:17:38we'll have it centered for now.
    • 0:17:40We'll have it centered at 0.
    • 0:17:41And y should be pretty big.
    • 0:17:43I'll make it 160 just to make it a little taller.
    • 0:17:45And so now the cat gives the instructions.
    • 0:17:48And after the instructions are over, the crystal
    • 0:17:51appears right at the top up high at the top of the screen.
    • 0:17:54But now I'd like the crystal to start falling.
    • 0:17:57It's up in the sky.
    • 0:17:58Now I need to get it to move down.
    • 0:18:00Well, I can move down just by changing the y value.
    • 0:18:04If I change the y value by a negative number, like negative 1,
    • 0:18:08or if I wanted a little faster, negative 2 for example,
    • 0:18:10that's going to cause the crystal to move down.
    • 0:18:14But now I don't just want the crystal to move down once,
    • 0:18:18but I want the crystal to move down over and over again,
    • 0:18:20to move down a little bit and then keep moving down again and again and again.
    • 0:18:24So I'll put all of that under control inside of a forever loop.
    • 0:18:31So now here's what's happening.
    • 0:18:33When we start a new crystal, the crystal is going to show itself.
    • 0:18:36It's going to go up to the top of the screen,
    • 0:18:39and then forever it's just going to start falling.
    • 0:18:42So now we can see what happens.
    • 0:18:44The cat gives the instructions.
    • 0:18:46The crystal appears, and the crystal now is falling.
    • 0:18:52And so this is often what the development process
    • 0:18:54is like for the game.
    • 0:18:55It's not that you're going to write all of the blocks
    • 0:18:57and put together all the different sprites for the entire game
    • 0:18:59and then run it and see the finished product.
    • 0:19:01It's that you're going to build it a little bit at a time.
    • 0:19:04I need the cat to move.
    • 0:19:05I need the cat to speak some instructions.
    • 0:19:07I need the crystal to appear.
    • 0:19:08Now I need the crystal to start falling.
    • 0:19:10And after each time, you add just a few blocks
    • 0:19:12to change the behavior a little bit.
    • 0:19:14You can run the project, see what happens,
    • 0:19:16and if you need to tinker with some of the blocks
    • 0:19:18to make it work if it didn't work the way you wanted it to work.
    • 0:19:21And we're now just building this game slowly one piece at a time.
    • 0:19:25So what do we have so far?
    • 0:19:26We have a cat that can move around.
    • 0:19:28That works.
    • 0:19:29We have a crystal that appears from the sky and starts to fall.
    • 0:19:32But right now nothing yet happens once the cat catches the crystal.
    • 0:19:36Once the crystal touches the cat.
    • 0:19:39So let's add that.
    • 0:19:40How can we do that?
    • 0:19:42Well, that sounds like a condition.
    • 0:19:44We're checking if the crystal is touching the cat.
    • 0:19:48So let's add an if statement into the loop.
    • 0:19:52And we'll say if we're touching not the mouse pointer but the cat.
    • 0:19:58If the crystal is touching the cat, well then what should happen?
    • 0:20:03Well, I want to somehow keep score.
    • 0:20:04I want to keep track of how many crystals has the cat caught.
    • 0:20:08And so to do that I need to store information
    • 0:20:11somewhere inside of my Scratch project.
    • 0:20:12Sounds like we need a variable, some variable that's
    • 0:20:15going to keep track of this information for us.
    • 0:20:18So let's go to Variable.
    • 0:20:19Let's make a new variable.
    • 0:20:21And I will call this variable catches.
    • 0:20:24That's going to represent the score.
    • 0:20:25It's how many catches the cat has made, how many crystals the cat has caught.
    • 0:20:30You'll notice the catches right now is 0.
    • 0:20:33And I would like to make sure that when the game first begins that we always
    • 0:20:38reset the number of catches back to 0.
    • 0:20:41So inside the cat, you'll notice that when the flag is clicked
    • 0:20:44we're doing a lot of resetting.
    • 0:20:45We're going to this original x equals 0 position to center
    • 0:20:49the cat at the beginning.
    • 0:20:50At the same time, let me set catches to 0 at the beginning.
    • 0:20:54Because if I accumulate a score and I play the game again,
    • 0:20:58I want to reset the score.
    • 0:20:59I want the score to go back to 0, and then I can start adding to the score.
    • 0:21:04How do I add to the score?
    • 0:21:06Well, that's going to happen in the crystal sprite
    • 0:21:08when it's touching the cat.
    • 0:21:10If we're touching the cat, we have now caught one additional crystal.
    • 0:21:14Let's change catches by one.
    • 0:21:16And now what should happen?
    • 0:21:19Well, now once the cat has caught a crystal,
    • 0:21:21let's create a new crystal for the cat to try to catch.
    • 0:21:26To do that, I can go back to control.
    • 0:21:30And I can create another clone.
    • 0:21:32Every time I create a clone, let's go ahead and stop this for now,
    • 0:21:36every time I create a new clone, that is going to create a new crystal.
    • 0:21:40And whenever we start as a clone, that crystal
    • 0:21:42is going to go to the top of the screen and it's going to start to fall.
    • 0:21:46And after I create a clone, let's go ahead and delete the current clone.
    • 0:21:51So what's going on here?
    • 0:21:53Why have I added these blocks into place?
    • 0:21:54Well, when we're touching the cat, that's one additional catch.
    • 0:21:57We're updating the variable, changing catches by one.
    • 0:21:59And creating a new clone, because once the cat has caught one crystal,
    • 0:22:05I'd like for a new crystal to appear at the top of the screen.
    • 0:22:08But then finally, I'm deleting this clone.
    • 0:22:12Once the cat has caught the crystal, I'm just
    • 0:22:14going to delete that crystal, because I don't need it anymore.
    • 0:22:17I have a new crystal that's going to be falling from the top of the screen
    • 0:22:22instead.
    • 0:22:24And so now let's try this program.
    • 0:22:26I'll press the flag.
    • 0:22:28The cat gives some instructions, catches, resets back to 0,
    • 0:22:32and we see the crystal falling.
    • 0:22:34And when the cat catches it, notice the catches goes up by one.
    • 0:22:37The score has increased and a new crystal appears.
    • 0:22:40And you'll notice this keeps happening.
    • 0:22:41It's in a loop.
    • 0:22:42Every time a crystal is caught, our score goes up by one.
    • 0:22:46And the crystal creates a new crystal that appears at the top of the screen.
    • 0:22:51And then the crystal that the cat caught disappears and we no longer see it.
    • 0:22:56So we're just seeing this flow of crystals appear one at a time.
    • 0:22:58We're seeing our variable update and increase by one every time
    • 0:23:02the cat catches a crystal.
    • 0:23:03And ultimately, this seems to now work pretty well.
    • 0:23:07The problem that I see is that the game is a little bit too easy.
    • 0:23:10I'm not even at the computer right now.
    • 0:23:12I'm not moving the cat at all and the crystal is just
    • 0:23:15appearing right on top of the cat and the cat
    • 0:23:17is catching it every single time.
    • 0:23:20So that's maybe not quite what I want.
    • 0:23:21What I'd like to do is introduce a bit of randomness into a game.
    • 0:23:25And this is a common property of games, randomness,
    • 0:23:27because you don't want the game to be exactly the same every time.
    • 0:23:30You want some different choices to be made sometimes,
    • 0:23:32a little bit of unpredictability so that the user
    • 0:23:35is going to have to respond to whatever they see.
    • 0:23:37And in this case, the unpredictability that I want
    • 0:23:40is the x value of the crystal.
    • 0:23:43That right now the x value of the crystal is always 0.
    • 0:23:45It's always going to x equals 0.
    • 0:23:49And because of that, the crystal is always
    • 0:23:51just immediately on top of the cat.
    • 0:23:53What I'd like to happen is for the crystal to sometimes be on the left
    • 0:23:57and sometimes be on the right.
    • 0:23:58That way I have to move around rather than just keep
    • 0:24:00increasing my score by doing nothing.
    • 0:24:03So let's go ahead and do that.
    • 0:24:05I will come over here and stop my project.
    • 0:24:09And instead of going to x equals 0 every time
    • 0:24:12the crystal starts, let's add some randomness.
    • 0:24:15I go under Operators.
    • 0:24:16Here's pick random.
    • 0:24:18And far left, that's going to be a big negative number.
    • 0:24:21So we'll go from negative 200 to the far right.
    • 0:24:24That's going to be a big positive number.
    • 0:24:26I'll make it positive 200.
    • 0:24:27But you could play around with those values for whatever you'd like.
    • 0:24:30And now when I start the game, the score resets and notice
    • 0:24:36that the crystal is going to appear.
    • 0:24:38This time it was kind of centered, but maybe it won't be that way every time.
    • 0:24:42Now the crystal's off to the right, and I have to move to the right
    • 0:24:45in order to catch it.
    • 0:24:47And every time I catch the crystal, the crystal
    • 0:24:49is going to generate a new random number between negative 200 and 200.
    • 0:24:54And that's going to tell me where I need to go in order to catch the crystal.
    • 0:24:58This is a much more interesting game than when the crystal was just always
    • 0:25:02appearing in the same spot every time.
    • 0:25:05So this is definitely an improvement.
    • 0:25:08And now I'm curious, what happens if I miss?
    • 0:25:10If I miss the crystal altogether and just let the crystal fall.
    • 0:25:14All right, the crystal hits the bottom of the screen
    • 0:25:17and now the game is kind of stuck.
    • 0:25:20What I'd like to do is add some code that can handle this situation.
    • 0:25:23What happens if the crystal ever touches the edge of the screen
    • 0:25:27and I didn't catch it?
    • 0:25:29What should happen?
    • 0:25:31Well, let's add another condition.
    • 0:25:34I'll add a condition to the bottom here underneath the loop.
    • 0:25:37Not if we're touching the cat, but this time
    • 0:25:40if we're touching the edge of the screen.
    • 0:25:43If ever the crystal makes it all the way down to the bottom
    • 0:25:46and now it's touching the edge of the screen, what should we do?
    • 0:25:50Well, we can decide.
    • 0:25:51We haven't really talked about what's going
    • 0:25:53to happen in the game at this point.
    • 0:25:54Maybe the game's over right away and we're just done.
    • 0:25:56Or maybe you get a couple of chances.
    • 0:25:59You're given like three misses.
    • 0:26:00And after three misses, then you lose the game.
    • 0:26:02And let's try and do that.
    • 0:26:04In order to make that happen, I need to keep track of how many misses
    • 0:26:07there have been so far.
    • 0:26:09So I'm going to go to variables and create a new variable called misses.
    • 0:26:14How many times has the cat now missed the crystal?
    • 0:26:18And just as in the beginning of the cat, we
    • 0:26:21set catches to 0 to reset the value of catches at the beginning of the game,
    • 0:26:25let's also now set misses to 0.
    • 0:26:28When the game first begins, you haven't missed any yet.
    • 0:26:31So let's reset it back to 0.
    • 0:26:32That way any previous progress in the game is erased.
    • 0:26:37But now if ever we're touching the edge, let's go ahead
    • 0:26:41and change the number of misses by one.
    • 0:26:46And at that point, we can do much the same thing as we were doing before.
    • 0:26:49We're going to create a new crystal by creating a clone.
    • 0:26:53Let's go ahead and stop this for now, otherwise we're
    • 0:26:55going to get a lot of clones.
    • 0:26:57But then let's also delete this clone after that.
    • 0:27:02So when we're touching the edge, we're going to change the number of misses
    • 0:27:06by one.
    • 0:27:06We now have one additional miss.
    • 0:27:07We're going to create a new crystal, and then we're
    • 0:27:10going to delete the clone to say we're done with this crystal.
    • 0:27:13We've now created a new one.
    • 0:27:15So now we'll see how the game behaves.
    • 0:27:19The crystals are falling.
    • 0:27:20I can try to catch them.
    • 0:27:23That's one that I caught.
    • 0:27:24I'll catch one more.
    • 0:27:27Now let me miss this one and see what happens.
    • 0:27:31The crystal is going to hit the bottom of the screen and I missed it.
    • 0:27:34You notice my misses goes up by one.
    • 0:27:36If I miss this one as well, misses goes up again.
    • 0:27:39And now I effectively have two variables,
    • 0:27:42one keeping track of how many times I've caught the crystal and one keeping
    • 0:27:47track of how many times I've missed the crystal.
    • 0:27:49And there seems to be no limit right now.
    • 0:27:50Misses can just keep going up and up and up every time I miss the crystal.
    • 0:27:54What I'd like to do is do something once I miss three of them.
    • 0:27:58Once I miss three, I want the game to be over.
    • 0:28:02So let's add a condition to check for that.
    • 0:28:05I'm going to take this if condition.
    • 0:28:06I'll build it out here for now and then I'll drag it in,
    • 0:28:09just to keep it separate for now.
    • 0:28:11But I'm going to say if the number of misses is equal to three,
    • 0:28:15so equals is an operator, and I'm going to say if the number of misses
    • 0:28:19are now equal to three, well what should we do?
    • 0:28:22Well, the game is now going to be over.
    • 0:28:24So I'm going to eventually delete this clone, because I no longer need
    • 0:28:28the crystal once the game is over.
    • 0:28:30But at this point as well, I need some way
    • 0:28:34to signal to the rest of the program that the game is over.
    • 0:28:37I need the cat to know the game is over.
    • 0:28:39Because maybe the cat is now going to report what my score was.
    • 0:28:42What is my score at the end of the game?
    • 0:28:44How many crystals did I catch?
    • 0:28:46So how can the crystal tell the cat that something has happened in the project?
    • 0:28:51Well, that's going to be a broadcast.
    • 0:28:52Broadcast as a way of sending messages, again, between sprites.
    • 0:28:56I'll broadcast a message.
    • 0:28:58But instead of begin, the message is going to be game over.
    • 0:29:02The game is over.
    • 0:29:03Let's make sure the cat knows that.
    • 0:29:06And let's drag that into this logic right after we change misses by one.
    • 0:29:12So we increase the number of misses, and then we check.
    • 0:29:15Is our number of misses equal to three?
    • 0:29:17If so we broadcast that the game is over.
    • 0:29:20And now last step here, in the cat I need to respond to that event.
    • 0:29:27When I receive game over, what should happen?
    • 0:29:31Well, when I receive game over, let's go ahead and say the score.
    • 0:29:35Let's say-- and I could just say the score directly.
    • 0:29:39But to make it a little nicer, I'll join together two words.
    • 0:29:42I'll say your score is and then a space and then
    • 0:29:47I'll drag in the number of catches.
    • 0:29:50And I'll say that for five seconds.
    • 0:29:53So when the game is over, when I receive that message,
    • 0:29:55the cat is going to say your score is this for five seconds
    • 0:29:59and then the game will be over.
    • 0:30:01So let's try it.
    • 0:30:02I start the game.
    • 0:30:03Catches and misses go back to 0, I get some instructions from the cat
    • 0:30:06and I can start catching.
    • 0:30:09I'll catch one.
    • 0:30:10I'll catch a second one.
    • 0:30:12My catches are now two.
    • 0:30:14But now I'm going to let the crystals just keep falling.
    • 0:30:18We missed one.
    • 0:30:22We're going to miss the second one.
    • 0:30:24And I'll miss a third one.
    • 0:30:25I'll get out of the way just to test out what
    • 0:30:27happens when I actually lose the game.
    • 0:30:29That's my third miss.
    • 0:30:30We broadcast that the game is over, and the cat reports that my score is two.
    • 0:30:35So at this point, we now have a completed game that
    • 0:30:38does sort of what we wanted it to do.
    • 0:30:40The cat can move around.
    • 0:30:41Crystals are falling from the sky.
    • 0:30:43The cat's able to try and catch as many of those crystals as we can.
    • 0:30:46And you could now play this game, share it with others.
    • 0:30:49And the nice thing is that you can always add to the game
    • 0:30:51if you think of new things you'd like to try inside of the game
    • 0:30:54just to experiment with it.
    • 0:30:56Maybe, for example, right now I think the game is maybe a little too easy.
    • 0:31:00There's always just one crystal falling from the sky
    • 0:31:02and I know how to move around to catch it.
    • 0:31:04So let's try something.
    • 0:31:05Let's make the game a little bit more challenging.
    • 0:31:10Where am I creating crystals in the game?
    • 0:31:13Well, I'm creating crystals here.
    • 0:31:15When I receive begin, we're creating a clone of the crystal
    • 0:31:18to create a brand new crystal.
    • 0:31:21But let's make that a little more exciting.
    • 0:31:24I'm going to put this in a forever loop, meaning we're always
    • 0:31:27going to be creating crystals.
    • 0:31:29Now, if I just did that to create lots and lots of crystals, what you'll see
    • 0:31:33is something like this where suddenly hundreds of crystals
    • 0:31:37are falling from the sky all at the same time.
    • 0:31:40That's maybe a little bit too challenging.
    • 0:31:43But let's add a wait here and say create a crystal
    • 0:31:47and then wait 15 seconds or so.
    • 0:31:49We're going to wait 15 seconds and then create
    • 0:31:52a new crystal so that a new crystal appears every 15 seconds.
    • 0:31:56Every 15 seconds, the game gets a little bit harder.
    • 0:31:58And so now I could try it again.
    • 0:32:00And you could play with that number to see what it's like.
    • 0:32:02But let's try playing this game.
    • 0:32:04I'm going to catch a crystal and keep doing this a few times.
    • 0:32:10And after 15 seconds or so, which should be in just a couple of seconds now,
    • 0:32:14we'll see what happens, it should create a second clone.
    • 0:32:17And now you'll notice there are two crystals that have to try and catch.
    • 0:32:20I'll catch one.
    • 0:32:21Can I get the other one?
    • 0:32:22All right, I caught the other one.
    • 0:32:25And in another 15 seconds, we're going to see the game get even harder.
    • 0:32:32Now if you notice, there are three crystals that
    • 0:32:34are all out there at the same time.
    • 0:32:36And maybe I'm going to start to miss some of them.
    • 0:32:38I missed one already, and I'm probably going to miss a couple more now.
    • 0:32:42And now I missed three, and it's telling me that my score is 14.
    • 0:32:48And the crystals are still falling.
    • 0:32:50And so what could I do to stop everything after the game is over?
    • 0:32:53Well, after the game is over, after I say your score is something,
    • 0:32:56I can go onto this block, stop all, and that's just going to stop everything.
    • 0:33:01Every sprite is just going to stop operating
    • 0:33:03after I say stop all so that the sprites don't continue to try to behave.
    • 0:33:07Since I have multiple clones of the crystals,
    • 0:33:09stop all will just now stop all of them.
    • 0:33:13So hopefully you can start to see now that by combining
    • 0:33:15these various different pieces, these different components,
    • 0:33:18these different tools and programming from different parts of Scratch,
    • 0:33:21we can start to create some creative and exciting projects as well.
    • 0:33:25So now as we wrap up this course, you now
    • 0:33:27have the tools you need to build projects of your own
    • 0:33:30that are interesting to you by taking advantage of functions and loops
    • 0:33:33and conditions and values and variables and events
    • 0:33:36and more to create interesting and exciting programs of your own.
    • 0:33:39And I'd encourage you to do so.
    • 0:33:41Play around with this Scratch interface.
    • 0:33:43Try and create a story or an animation or a game that's exciting to you.
    • 0:33:47Share it with your friends.
    • 0:33:48Share it with your family.
    • 0:33:49And we look forward to what you create.
    • 0:33:51Thank you so much for joining us.
    • 0:33:52This was an introduction to Programming with Scratch.
  • CS50.ai
Shortcuts
Before using a shortcut, click at least once on the video itself (to give it "focus") after closing this window.
Play/Pause spacebar or k
Rewind 10 seconds left arrow or j
Fast forward 10 seconds right arrow or l
Previous frame (while paused) ,
Next frame (while paused) .
Decrease playback rate <
Increase playback rate >
Toggle captions on/off c
Toggle mute m
Toggle full screen f or double-click video