Scratch: Virtual Pet Game

Instructions

In this Scratch project, you will be taking care of a pet capybara. Your goal is to keep the pet happy and healthy by feeding, playing, and taking care of its needs. To do this you need to click the buttons that are shown on the screen.

Notes and Credits

Ideas from my friend and chatGPT. Background and characters from Midjourney. Make this in 4 hours.

Click the green flag to play my Virtual Pet Game:

If you prefer to play it on the scratch website, click this link.


Now this is how I make this game:

1. Ideas & Thoughts

I first got an idea from ChatGPT:

Virtual Pet Game:

  • Build a virtual pet simulation with two characters—a pet and an owner. Use variables to track the pet's needs (hunger, happiness, etc.), conditionals for actions based on needs, and loops for ongoing care.

So I decided to take on this idea and reshape it. I made the pet a capybara because one of my friend likes it very much.

2. Stage & Sprites

In my game, there are six sprites and one stage. Only the "Meal", "Snack", and "Play" buttons are hand-drawn by me. The others are generated by AI using Midjourney. I removed the backgrounds of those sprites using this website: remove bg.

From left to right: Angry Bri, Normal Bri, Meal button, Snack button, Play button, pet Capybara, Bedroom background.

3. Sound Effects

I thought that when the user is feeding or playing with the pet, there should be some sound from the capybara. But I don't know how exactly a capybara sounds like (especially when I did not ever seen one in my life), so I decided to search online.

I went to an app called Xiaohongshu to find short videos about the sound of capybaras and I recorded some sounds into my sound library.

4. Script & Code

Let's first take a look at the code for "Normal Bri" which is the sprite who appears first.

When the green flag is clicked, the sprite will first greet the user and ask him/her whether they would like to help Bri in taking care for her pet. If the user inputs "yes" or "Yes", then the game continues. Else, the game will end there.

As the game continues, the sprite will explain the rules and instructions, also broadcasting the message "start" to other sprites and stage behind the scene which the user cannot see. Then "Normal Bri" will hide.

If the user successfully completed the game, "Normal Bri" will appear and say thanks.

Another goal for this sprite is to tell the user the night time and game over message.

Now lets look at the code of the stage:

The role of the stage is to change the values of the "clock" variable, and base on the time change its brightness. Dark when night and light when day.

The "clock" is set to 7 initially, and then changed to 8 when the stage receive the message broadcasted by "Normal Bri".

Another role of the stage is to change the variables "Happiness" and "Hunger" down by a certain time. This is repeated for infinite of times.

Okay, now after seeing how the variables work. Lets see the code behind the capybara:

There is an infinite loop in the code for the capybara to act when:

Hunger < 50 (starving) or

Happiness < 40 (sad) or

Hunger > 150 (eating too much)

To simplify the code, I made three "My Blocks" which are personalised blocks for my program. These blocks all include a conditional to check whether the boolean is true and if it is, the capybara will think a certain phrase that will appear on the screen and shown to the user.

Here below are how the buttons work:

As you can see, all of the codes of the three buttons are similar.

They hide when the game is first started.

They hide when it's night and show when it's day.

They show when they receive the message "start", and have a forever loop checking if the user clicks the button.

The only difference is that they change different variables' values by different amounts.

Also, different sounds are played based on the interaction.

The reason why I added a wait block inside the conditional is because if not the sprite will consider the condition to be true for a certain amount of times which is not good because just by one click from the user, the variable is changed by many amount up. This is not what I want, so I added a delay to ensure that one click only respond one time to the user.

Lastly, we have our final sprite, which is "Angry Bri":

The sprite is hidden initially.

She only appears when the conditions are true for the if-loop, which is when the capybara is too hungry, too sad, or too full.

Then she will yell at you and this is the end of your game.


Above all are my steps towards making this game. Hope you like it and wish you luck on making your own game.

Author photo
Publication date:
Author: Brianna