If you’re looking for a fun Scratch project to build, look no further! In this guide, you and your child will learn how to make a virtual pet in Scratch that will react when you pet it, and will eat food. After following this guide, you will have a great program that you can continue to work on and customize to make your own.

Enroll your child in award-winning free Scratch classes led live by an expert instructor and designed by professionals from Google, Stanford, and MIT, to create all sorts of cool games with Scratch coding.

Create Your Own Virtual Pet In Scratch

Follow along with the steps below to create your own virtual pet in Scratch. If you would like to start with our version of the project, you can find it here.

1. Create a New Project and Add a Backdrop

Add a Scratch project backdrop

Quick Summary: Log in to Scratch, click Create to start a new project, delete the default cat sprite, and choose a backdrop for your pet's habitat.

As with any Scratch coding project, begin by navigating to Scratch and clicking Create in the top menu bar to open a fresh canvas. Delete the default Scratch Cat sprite by clicking the trash icon on its thumbnail in the Sprite Pane. Next, choose a backdrop by hovering over the blue Choose a Backdrop button in the bottom-right corner. Select any environment where your virtual pet will feel at home—we chose "Mountain."

Want guided support while learning block coding? Kids can build interactive pets, games, and animations with expert instructor guidance in Create & Learn's live online Scratch coding classes.

2. Add Your Pet!

Add your Scratch sprite

To add your pet, click the green “Choose a Sprite” button. For this step as well, you can choose any pet you like, but try to choose one with an animation (it will move when you hover your mouse over it). For our project, we chose “hare", but you can choose any animal you want!

3. Load In Sounds

Load sounds

Quick Summary: Switch to the Sounds tab in the top-left corner, click Choose a Sound in the bottom-left, and select two sounds: one for talking and one for eating.

This project uses two different sound effects: one for when your pet speaks, and one for when it eats. To add custom audio to your sprite, click the Sounds tab located in the upper-left corner of the Scratch editor (next to Code and Costumes). Then, hover over the blue Choose a Sound icon at the bottom-left of the screen. Filter by the Animals or Effects category to find audio for your pet. Click your preferred talking sound, then repeat the process to choose an eating sound. For our rabbit, we chose "Chatter" for talking and "Chomp" for eating.

4. Make Your Pet Talk

Make your pet talk in Scratch coding

For our first bit of coding, we will make our pet talk when we click it. Take a look at the code above. The first block is called an Event Block and will activate the code below it when the sprite is clicked. The second block will play the chosen sound. Together, these blocks make it so that our rabbit will talk when it is clicked. Once you understand the code, go ahead and add it to your project, being sure to change the selected sound to the sound you want to play when you click on your pet.

5. Add A Food Source

Add a food sprite

Of course, your pet will need to eat! To add this functionality, we need to add a second sprite. Click the “Choose a Sprite” button, as before, and choose a food source for your pet. We chose “apple.” Move the food to an appropriate place in your project. Choose whatever you feel appropriate for your pet!

6. Code The Food

Code the food

We can add some simple code to program our food source. Click on the food source in your sprites window to select it. Then, add the code in the image above. The “when this sprite clicked” block will activate the code below when the food is clicked, and the “broadcast” block sends out a message other blocks can receive (more on that later). Be sure to click the “message1” message that loads in by default and change it to “food!” by clicking “new message.” Broadcast is like an internal alert system to alert or message the other sprites.

7. Make Your Pet Eat

Make pet eat

We are now ready to animate some movement into our pet. Return to animating your pet by clicking on the pet in the sprites tab. Whichever sprite is highlighted blue, is the active sprite. Then, add the code in the image above. Let’s take a look at what each block of code is doing:

  • When I receive (food!): Before, we programmed our food source to broadcast the message “food!” when it is clicked. We can make our pet listen for this broadcast, and when it hears it, it will take the action in the code below. In other words, when the food source is clicked, our pet will take the action in the following code.
  • Glide (1) secs to x: (x) y: (y): Sprite placement in the Scratch canvas is controlled by an invisible grid, and you can place things around the grid by giving them an x value and a y value. The x value controls placement left to right, and the y value controls placement up and down. In this part of our code, we want our pet to move to the x and y values of our food. Find these values by clicking on your food, and then add them to this “glide” block. This block will make your animal glide for 1 second towards the target location.
  • Play sound (sound) until done: This block will play the target sound - be sure to change “chomp” to the eating sound you chose for your pet!
  • Wait (1) seconds: We want our pet to eat for a bit, so this block will have it wait on the food for 1 second so it has time to eat.
  • Glide (1) secs to x: (x) y: (y): As before, we want our pet to glide to a new location. These x and y values are up to you, but choose somewhere that makes sense for your pet to rest, such as on a rock or on a chair. To make this easier, click and drag your pet to your target location, take note of the x and y values, and then add those values to this block.

8. Add some animation

At this point, your pet should move to the food and eat when the food is clicked, but there is no animation. We can add a few new blocks to our existing code to improve how it looks and moves. Read below to understand the purpose of these new blocks, and then add them to your code.

Animate your virtual pet
  • Go to (front) layer: We want our pet to be on top of the food as it eats; this block moves our pet on top of the food. Everything in scratch is in layers so the top layer ensures the pet is fully in front.
  • Switch costume to (costume b): This changes our pet’s “costume” while they are gliding, which makes it look like they are moving. It's like a flip book switching between images fast.
  • Switch costume to (costume a): While our pet eats, they can revert back to their previous “costume,” or animation frame.
  • Switch costume to (costume b): Again, we want our pet to look like it is moving, so we can use their secondary costume while they glide.
  • Point in direction: (-90): We don’t want our pet to move backwards, so we can use this block to change the direction it is facing. Depending on if your pet starts on the left side or the right side of your food, you may need to use this block further up in your code. You also may need to change the direction from -90 to 90.
  • Switch costume to (costume a): Now that our pet is back to its resting spot, we can change it back to its original costume.
  • Point in direction: (90): Similarly, we can change our pet’s direction back to normal now that it is back at its resting spot.

9. Create a Hunger Variable

Make a hunger variable

The next part of our project is to add in hunger. We can do this by creating a variable called “hunger.” Click “variables,” “make a variable,” and name the new variable “hunger.” This is going to track how "hungry" your animal is.

10. Increase the Hunger Variable

Increase hunger variable

Next, we want to make our pet get hungry over time. We can do so with the code above. Let’s take a look at what each element does:

  • When green flag clicked: The code below will start running when the green flag is clicked.
  • Forever: The code inside this loop will run forever, which is what we want!
  • Wait (5) seconds: We don’t want the hunger to update too quickly, so we will update it every 5 seconds.
  • Change (hunger) by (5): This block causes our hunger to increase by 5 every time the block is triggered, which will suit our needs well

11. Allow Your Pet To Tell You When It Is Hungry

When pet is hungry code

Your pet will need a way to tell you when it’s hungry! Let’s add the code above to do just that; take a look below to understand how it works.

When green flag clicked: The code below this block will run when the green flag is clicked. This is one of the most common event blocks used.

Forever: This code will run forever once the green flag is clicked

Wait (5) seconds: We don’t want the animal to talk non-stop, so we will only check for hunger every 5 seconds.

If <(hunger) > (25)>: The code inside this loop will only run if the pet’s hunger is greater than 25.

Say (I’m hungry!) for (2) seconds: Whenever this block is activated, your pet will speak for two seconds to let you know it’s hungry.

12. Reset Hunger After Eating

Reset your virtual pet's hunger

The final step will be to reset your pet’s hunger after it has eaten. To do so, add this “set (hunger) to (0)” block to your existing code, as shown above.

Frequently Asked Questions & Common Fixes

IssueCauseSolution
Pet slides upside down or backwardsRotation style is set to 360 degreesSet rotation style to left-right only or add a set rotation style [left-right v] block.
Hunger doesn't decrease after eatingMissing broadcast trigger or reset blockEnsure the set [hunger v] to (0) block is attached inside the when I receive [food! v] script.
Pet stays behind backdrop or foodLayer depth issueAdd a go to [front v] layer block inside the movement script.

Once you complete the core mechanics, consider adding a Happiness variable, a Play button sprite, or multiple food options to turn your project into a full Tamagotchi-style game!

Make A Virtual Pet In Scratch

At this point, you should have a virtual pet that will make a sound when you pet it, will let you know when it’s hungry, and will eat when you feed it! This project is great because it can continue to be customized - perhaps you could create a “thirst” variable and water your pet, or maybe you could create a ball that it can play with. The possibilities are endless!

If your student thought this project was fun and would like to continue to improve their skills with Scratch, check out our Scratch Ninja Course (for beginnerz) and Game Building Course (for more advanced coders). And, learn how to make snow fall in Scratch. Thanks for following along, and happy coding!

Written by Matt Schofield, an educator and avid coder. After studying Spanish at the University of Pennsylvania, Matt began teaching English as a second language to elementary students in Baltimore. In addition to his full-time teaching position, Matt enjoys teaching computer science in the evenings and weekends with Create & Learn.