How to create a virtual pet game in Scratch

How to create a virtual pet game in Scratch

How to create a virtual pet game in Scratch

Content Protection by DMCA.com

Scratch can help you create an extremely adorable virtual pet game. Below are detailed instructions How to make a virtual pet game using Scratch.

Have you ever wanted a virtual pet to play with and take care of? With Scratch, you can absolutely create one! A virtual pet is an image or character that can interact with you, respond to different actions, and even let you know when it’s hungry, happy, or tired. After going through this tutorial, you will be able to easily create a virtual pet using block programming.

1. Create a new project and add a backdrop

Add projects to Scratch
Add projects to Scratch

As with any other Scratch project, the first step is to create a new project. Open Scratch and click Create to create a new project. Delete the default cat image and then choose a suitable background by clicking the button Choose a Backdrop in the lower right corner (green in the image above). This example selects the mountain – Mountain.

2. Add pets

Add characters

To add a pet, click the green button Choose a Sprite. You can choose any pet you like, but try to choose one that is animated (it will move when you hover over it). This example selects the rabbit.

3. Download audio

Download audio
Download audio

This project will use two different sounds – a sound when the pet talks and a sound when the pet eats. To load sounds into your project, click Sounds in the upper right corner. Then click Choose a Sound. You can also set the filter to Animals. Find the sound you want your pet to make when it talks, then click on it to load it into your project. Repeat this process to find the sound of your pet eating. For example, choose chatter – mumbling as the sound when talking and chomp – chewing as the sound when eating.

Read More  Interesting programming exercises on Scratch for 2nd graders

4. Make a talking pet

Make talking pets
Make talking pets

The first step in the programming sequence is to make the pet talk when clicked. Take a look at the code above; The first code block will trigger the code below when the pet image is clicked, and the second code block will play the selected sound. Combined, these code blocks will make the bunny talk when clicked.

5. Add a food source

Add food sprite
Add food sprite

Of course, pets need to eat. To add this functionality, add a second sprite. Click the button Choose a Sprite as before and choose a food source for your pet. This example selects Apple – Apple. Move the feed to the appropriate location in your project.

6. Programming food sources

Programming food sources
Programming food sources

Click on the food source in the sprite window to select it. Then add the code in the image above. Block when this sprite clicked will trigger the code below when the food is clicked, and block broadcast will send a notification that other blocks can receive. Remember to click on the notification message1 loaded by default and change it to food. food by clicking new message.

7. Feed your pets

Feed your pet
Feed your pet

Now is the step to create movement for the pet. Go back to animating the pet by clicking on the pet in the tab sprites. Then add the code in the image above. Below is the function of each code block:

  • When I receive (food!): The food source has been programmed to emit the message “food. food” when clicked. You can have your pet listen to this message, and then it will perform the action in the code below. In other words, when the food source is clicked, our pet will perform the action in the following code.
  • Glide (1) secs to x: (x) y: (y): The placement of objects (sprites) on the Scratch canvas is controlled by an invisible grid, and you can place them around the grid by assigning them x values ​​and y values. The x value controls the position from left to right, and the y value controls the position from top to bottom. This part of the code wants the pet to move to a location where the x and y values ​​coincide with the food location. Find these values ​​by clicking on your feed, then add them to this “glide” command block. This command block will cause the pet to dash for 1 second towards the target location.
  • Play sound (sound) until done: This command block will play the target sound – remember to change “chomp” to the eating sound you’ve chosen for your pet!
  • Wait (1) seconds: This command block will require the pet to wait for food for 1 second to have enough time to eat.
  • Glide (1) secs to x: (x) y: (y): The pet glides to a new location. These x and y values ​​are up to you, but choose a reasonable place for your pet to rest, such as on a rock or on a chair. To make this easier, click and drag your pet to the target location, note down the x and y values, then add those values ​​to this block.
Read More  Instructions for making Cross the Road game on Scratch

8. Add a little animation

In this step, your pet will move to the food and eat when you click on the food, but there is currently no animation. You can create animations like this: . Add animation

  • Go to (front) layer: KHelps pets move up on food to eat it directly.
  • Switch costume to (costume b): Change your pet’s “outfit” while they’re gliding, making it look like they’re moving.
  • Switch costume to (costume a): While pets eat, they can return to their previous “costume” or animation frame.
  • Switch costume to (costume b): Switch outfits.
  • Point in direction: (-90): Change the direction of your pet’s movement. Depending on whether your pet starts on the left or right side of the food, you may need to use this command block in the upper part of the code. You may also need to change the direction from -90 to 90.
  • Switch costume to (costume a): Return the pet to its resting state and put on its original outfit.
  • Point in direction: (90): Adjust your pet’s direction back to normal when it has reached its resting place.

9. Create a Hunger variable

Create hunger
Create hunger

The next part of the project is to add the element of hunger by creating a variable named Hunger. Click variables > make a variable > name the new variable hunger.

10. Increase Hunger value

Increased Hunger
Increased Hunger

This block of code changes the pet’s hunger over time

  • When green flag clicked: The code starts running when the green flag is clicked.
  • Forever: The code inside this loop will run forever.
  • Wait (5) seconds: Hunger is upgraded every 5 seconds.
  • Change (hunger) by (5): This block causes hunger to increase by 5 each time it is activated.
Read More  Black Myth: Wukong - How to find and defeat Kang -Jin Loong boss

11. Allow your pet to let you know when it’s hungry

Hungry pet code
Hungry pet code
  • When green flag clicked: The code runs when the green flag is clicked
  • Forever: This code will run forever when the green flag is clicked
  • Wait (5) seconds: Check hunger every 5 seconds so your pet doesn’t talk constantly.
  • If (25)>: The code inside this loop will run when hunger reaches 25 or more.
  • Say (I’m hungry!) for (2) seconds: Whenever this block is activated, the pet will tell you it is hungry.

12. Reset hunger after eating

Reset hunger after eating
Reset hunger after eating

This is the final, extra step set (hunger) to (0) into your existing code as shown above.

GameHov is group of expert in gaming industry that cover all gaming news from e-sport to casual video entertainment.

Post Comment