Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Make a Virtual Pet Game in Scratch

Build a Scratch virtual pet that talks, moves to food, plays sounds, tracks hunger over time, warns the player, and resets cleanly between games.

Table of Contents

A Scratch virtual pet combines sprites, sounds, messages, animation, and variables in one approachable project. In this version, clicking the pet makes it talk, clicking food starts an eating sequence, and a Hunger variable rises over time and falls after feeding.

A finished virtual pet project in Scratch

Plan the pet's behavior

Before building, decide what each action should do:

  • Green flag: reset the pet, hunger, and food
  • Click the pet: play a voice sound and show a short message
  • Click the food: broadcast a Feed message
  • Receive Feed: move the pet, animate eating, reduce hunger, and return home
  • Every few seconds: increase hunger up to a maximum
  • When hunger passes a threshold: warn the player once, then wait until the pet is fed

Keeping these responsibilities separate makes the project easier to debug.

1. Create the project and backdrop

Start a new Scratch project. Keep or remove the default cat, then select Choose a Backdrop and choose a setting for the pet. A simple room, garden, or outdoor scene leaves space for the pet and its controls.

Selecting a backdrop for a Scratch virtual pet

2. Choose and position the pet

Select Choose a Sprite and pick an animal with at least two useful costumes if possible. Rename it Pet. Place it at its resting position and note the x and y coordinates; you will use those values after the eating animation.

Choosing a rabbit sprite as the virtual pet

Set the rotation style to left-right if the pet should face the food without rotating upside down.

3. Add voice and eating sounds

With the Pet selected, open Sounds, choose a short animal sound, and add a second sound for eating. Rename them clearly, such as Pet voice and Chomp.

Adding animal and eating sounds to a Scratch sprite

Short effects work better than long clips because repeated clicks can overlap audio. Use play sound until done when later blocks must wait for the effect to finish.

4. Make the pet respond to a click

Create this script on the Pet:

  • when this sprite clicked
  • play sound [Pet voice] until done
  • say [Hello!] for 2 seconds

Scratch blocks that make the virtual pet talk when clicked

Keep the message short. Later, you can choose a phrase based on hunger or happiness instead of always saying the same thing.

5. Add a food sprite

Choose a second sprite such as an apple, carrot, or bowl and rename it Food. Place it where the pet should eat. Note the Food's x and y coordinates.

Adding an apple food sprite to the virtual pet project

6. Broadcast a Feed message

Select Food and create:

  • when this sprite clicked
  • broadcast [Feed]

Create Feed as a new message instead of leaving the vague default name message1.

Food sprite broadcasting a Feed message in Scratch

A broadcast lets the Food request an action without containing the Pet's animation code. The Pet can listen for the same message, and other sprites can respond later.

7. Move the pet to its food

Select the Pet and create a new script beginning with when I receive [Feed]. Add:

  1. Point toward the Food or set the correct direction.
  2. Switch to a movement costume.
  3. Glide for 1 second to the Food's x and y coordinates.
  4. Play the Chomp sound until done.
  5. Wait briefly.
  6. Point back toward the resting place.
  7. Glide to the original x and y coordinates.
  8. Switch back to the resting costume.

Pet script that glides to food and returns to its resting place

Position the destination so the pet appears next to the food rather than directly covering it. Test the coordinates with the final sprite size.

8. Improve the eating animation

Use go to front layer only when the pet should appear in front of the food. Switch between two costumes during the glide or eating pause, then restore the resting costume and direction.

Costume and direction blocks used to animate the pet eating

If costume changes make the pet jump sideways, align the costume centers in the Costume editor.

9. Create the Hunger variable

Choose Variables > Make a Variable, name it Hunger, and create it for all sprites. In this project, 0 means not hungry and 100 means extremely hungry.

Creating a Hunger variable in Scratch

Show the variable on the Stage while testing. You can hide the raw monitor later or replace it with a custom meter.

10. Increase hunger over time

On the Pet, create this reset-and-timer script:

  • when green flag clicked
  • set [Hunger] to 0
  • forever
    • wait 5 seconds
    • change [Hunger] by 5
    • If Hunger is greater than 100, set Hunger to 100

Scratch timer script that increases the pet's Hunger value

Clamping the value at 100 keeps later comparisons and meters predictable.

11. Warn the player without repeating constantly

A forever loop that says “I'm hungry!” every few seconds becomes noisy. Instead, use two wait until blocks:

  • when green flag clicked
  • forever
    • wait until <Hunger > 60>
    • say [I'm hungry!] for 2 seconds
    • wait until <Hunger < 40>

Scratch blocks that warn the player when Hunger is high

This produces one warning, then waits until feeding lowers Hunger before another warning can occur.

12. Reduce hunger after feeding

In the Pet's when I receive [Feed] script, after the eating sound:

  • change [Hunger] by -25
  • If Hunger is less than 0, set Hunger to 0

Reducing the Hunger variable after the virtual pet eats

Reducing hunger instead of always resetting it to zero makes repeated feeding meaningful. If you prefer a simpler game, use set Hunger to 0.

Prevent repeated clicks during the animation

Create a variable named Busy. Set it to 0 on the green flag. When Food is clicked, broadcast Feed only if Busy equals 0. In the Pet's Feed script, set Busy to 1 before moving and back to 0 after returning. This prevents several Feed messages from starting overlapping glides.

Reset every sprite on the green flag

ObjectReset action
PetGo to the resting coordinates, set Hunger and Busy to 0, show, restore the resting costume and direction
FoodGo to its starting coordinates and show
StageSwitch to the normal backdrop
SoundsStop all sounds if an old effect could still be playing

Test the pet

  • Click the Pet repeatedly and check that sounds do not overlap badly.
  • Click Food during movement and confirm Busy blocks another feed.
  • Let Hunger exceed the warning threshold and check that the message appears once.
  • Feed the pet and confirm Hunger never becomes negative.
  • Restart while the Pet is moving and verify that every object resets.

Ideas to extend the game

  • Add Happiness and Energy variables with their own controls.
  • Change costumes when hunger is low, medium, or high.
  • Add a sleep area that restores Energy over time.
  • Create a shop where earned points buy different food.
  • Save a high score with a cloud variable only if the project and account support it.
  • Add accessible visual feedback so important states are not communicated by sound or color alone.

Add one system at a time. Give each variable a clear range and reset rule so the virtual pet behaves consistently after every green-flag restart.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.