- Hi everyone, It's Zoƫ from the Scratch team, also known as Zinnea. Today, I wanted to show you how to make a virtual pet in Scratch, a pet you can interact with,
take care of, give food to. In this tutorial, I'll show you how to make
a virtual pet simulator, where you can introduce your pet, animate your pet, feed your pet, and keep track of how hungry your pet is. Let's get started. So first, let's pick a
pet and introduce it. You could draw your own pet. You could upload an image, or you could pick one of the
characters we have in Scratch by clicking this button that
says, "choose a sprite." You could pick any pet you want. You could have a pet dragon
or a pet rock or a pet dog. I will have a pet hedgehog. And let's also choose a backdrop. It can be anywhere you want. I will pick the garden rock. So to introduce our pet, let's have our pet say something. In the looks category, you can drag out a "say" block and write something for your pet to say. And it would be nice if it did this whenever we click the green flag, so we can add a "when green
flag clicked" block on top. Nice. Next let's animate our pet so that it can do a little
animation like this. (hedgehog squeaking) To animate a character in Scratch, you can make it switch
between different poses with this "switch costume" block. So if I drag out two
"switch costume" blocks and pick different poses, I can make it animate between them. Let's connect these blocks by adding a "wait" block in between. I'll have it wait .2 seconds. Nice. It would be cool if we could
get it to repeat this animation and in Scratch, that's really easy. Just use a repeat loop. I'll make this repeat two times. Okay, it repeated, but it looked a little glitchy because it needs to wait after it switches to this costume too. So let's add another wait block. Almost done with our animation. To finish it off, let's
also make it play a sound. You can just go to the sounds tab and click the choose a sound button and then pick whatever sound you want. To make it play the sound, we just need to add a
"start sound" block on top. (hedgehog squeaking) Nice. And let's also add a "when
the sprite clicked" block so that we can make it play this animation every time we click the sprite. (hedgehog squeaking) There we go. Now you've animated your pet. Next, let's feed our pet. Here's what we'll make by
the end of this section, we will make a piece of food, and when we click on it, the pet will go over to it, take a bite, (hedgehog chomping) and then come back to where
it was sitting. Let's do it. So first let's add a piece of food. I'll add an apple. Let's drag out a "when
this sprite clicked" block, because we want to click on the apple and make the hedgehog come over to it. Now, how do we tell the hedgehog
to come over to the apple? There's a really easy way
to do that in Scratch, which is with a broadcast. A broadcast is like an invisible message that one sprite sends out and you can make another
sprite do something specific when it gets that broadcast. So let's make the apple broadcast this invisible
message to the world that will mean that it's
time for the hedgehog to eat. And let's call that message "Food". And now, to make the hedgehog
respond to that message, we can click on the hedgehog and you see this block,
"When I receive food," we can give it this block. Just to check if it's working, I'll have it say something
when it gets the broadcast. Okay, nice. We can click on the apple. It sends out the broadcast
and the hedgehog receives it, but we don't actually want
the hedgehog to say something when it gets the broadcast, we want it to go over to the apple. We can make it glide over to the apple by dragging out this block that says, "glide to random position" and just choosing, "apple". Nice. It glides right to it. Now let's make it glide back to the position where it was sitting. To do that, we can use this block. In Scratch, if you want
to make your sprite go to a certain position, you can tell it to go
to a certain number X and a certain number Y. If you don't know what X and Y are, X represents how far your sprite is from this side of the screen to this side of the screen. You can see the X and Y that
your sprite is at right here. So over here X is really big, like 240. And if you bring it this way, X is a little lower, like 100. X is zero if you're in
the middle of the screen. And if you go over here, X gets lower than zero and becomes a negative
number like negative 100. And then Y represents how
high up your sprite is. Like if you're up here, Y is 180. And Y is zero if you're in the middle. And if you go below the
middle of the screen, Y is a negative number. Now, here's a handy trick for having your sprite glide
to the X and Y that you want. I'll drag my sprite to
where I want it to glide to. And then the "glide" block
that's in the menu over here, actually updates to that
specific X and that specific Y so I can just drag that out, and now it glides back to
that position on the rock. And let's also add a bite sound
when it gets to the apple. I know we have the sound chomp. So after it gets to the apple, I'll have it start
playing the sound chomp, and then wait 0.5 seconds
and then glide back. (hedgehog chomping) Nice, now we can feed our pet. And lastly, let's make
our project keep track of how hungry our pet is, so that you have to keep feeding it. To do this, let's make a variable. A variable just keeps
track of a certain number in your game, like your score, or in this case, our pet's hunger level. So let's make a variable
and call it, "hunger". And we'll say that the
bigger this number is, the more hungry our pet is. So if it's 100, our pet is really, really hungry. And if it's zero, our
pet's not hungry at all. We want the hedgehog to
get more hungry over time. And for that, we can use this block, "change hunger by one". So that's increasing the hunger. To make it so that the hedgehog is always getting more hungry, we can put this "forever" block around it. Oh my gosh, the hedgehog
got really hungry! So we probably don't want
it to change hunger by one every single instant. Instead, let's have it
always wait five seconds and then change the hunger by one. Okay, now the hunger is
increasing at a nice slow rate, but it's still way too high. So let's set the hunger to
zero when the project starts. Now, we want the hunger level to go down when we eat the food. How do we do that? Well, we can change the
hunger by negative one to have the hedgehog get less hungry. Now the hunger's going down and let's have this happen
when it chomps on the apple. So let's try it out. The hunger's at one, we click on the food and now the hunger goes back down to zero. So there you go. Now you can feed your pet
to make it less hungry. Anyway, that's what I
wanted to show you today. You could take this in so
many different directions. Just like we added the apple, you could add more things
for your pet to interact with like a ball or a toy, and you could add more
variables, like thirst and fun, and the player has to make sure they all stay at a good level. You could add day and nighttime. You could add a sleeping animation. You could have multiple pets. Oh my gosh, that would be so cute. I'm so excited to see
all the things you make. Anyway, I'll see you next time and Scratch on. (hedgehog bouncing) (lighthearted music)