Home

St. Mary's Catholic High School Computing Dept

@StMarysICT

A Virtual Pet

A Project in Scratch

Created by Andy Colley - based on a project by Marc Scott (@coding2learn) using reveal.js
Creative Commons License
A Virtual Pet by Andy Colley is licensed under a Creative Commons Attribution 3.0 Unported License.

Contents

  1. Learning Intentions
  2. Getting Started
  3. Creating a new costume
  4. The First Script
  5. Resetting the Simulation
  6. Scratch Functions
  7. Making the Pet get Hungry
  8. Variables and Loops
  9. Feeding the Pet
  10. Feeding the Pet - Extension
  11. Making the Pet get Thristy
  12. Giving the Pet a Drink
  13. Making the Pet get Sad
  14. Making the Pet get Tired
  15. Customising the LOL Cat
  16. Getting the Pet to Talk
  17. Adding Randomness
  18. Speaking from a List
  19. Extension Activities

Learning Intentions:

By the end of this project you should all know:

Script Elements

  1. What a variable is and how it is used
  2. What an operator is and how it is used
  3. What a function is and how is is used
  4. What a loop is and how it is used
  5. What a list is and how it is used

Getting Started

A Virtual LOL Cat

Tamagotchi

Virtual pets are computer programs. The owner has to interact with the program to keep their pet alive and happy. They can feed and stroke the pet, or tell it to go to sleep.

You're going to make your own Virtual Pet.

Scroll down to see an example of what you will create.

Click the sprites to see what happens.

you will have to wait until the variables have gone up past 9.

Creating a new

Costume

Task - Create a New Costume

First we need to create a new costume for the cat. This will be how it looks once it has died

Edit the name of the Sprite by clicking on the little blue i

Sprite Sprite Properties

Now click on the Costumes tab in the top centre of the page and then rename the first costume to Alive_Pet

Costumes

Now you'll need to import a new costume for your Pet. Use the image below to help you out here. I chose the angry ghost for mine. Rename it to Dead_Pet

Add second costume

If you're stuck, there's a video below to help you out.

Not working? Click here for the video.


The First Script

Our first script will make the cat die!!!

Task - Can You Code It?

We're going to build a test script first and then use it in a Block if it works.

If you're used to using Scratch, then look at the pseudocode (pretend code) below and try to write the code without any more help.


when the space key is pressed
say "You let me die"
switch to Dead_Pet Costume
glide for 4 seconds to x:x, y:225
hide sprite
						

If you complete the script then skip right to move on to the next task.

For more help, scroll down.

Here are all the blocks you need to make the script. See if you can assemble the script now


when the space key is pressed
say "You let me die"
switch to Dead_Pet Costume
glide for 4 seconds to x:x, y:225
hide sprite
						
Blocks for death script

If you struggle to put it together then look at the slide below

Not working? Click here for the video.


Help! My Pet Has Disappeared!

Reset Lolcat

A Problem

If your 'dying' script works, then when you hit space your pet dies and floats off screen.

Go on, try it.... Working? Excellent!

Code success

However, the pet won't come back when you click the green flag again.

Scroll down to find out how to reset the simulation.


Task - Reset Script

We're now going to make a script that we can use to reset the Cat sprite

If you're getting the hang of this now then just follow the pseudocode below, if not then got to the next slide down. Once you've built a succesful script navigate right.


When 'Green Flag' clicked
Switch to Costume "Alive_Pet"
Show Sprite
Goto position x:0, y:-125
						

Here are all the blocks you need to make the script. See if you can assemble the script now


When 'Green Flag' clicked
Switch to Costume "Alive_Pet"
Show Sprite
Goto position x:0, y:-125
						
Blocks for reset

If you need more help then go to the slide below.

Here is the completed Script

Reset Script

If you struggle to put it together then look at the slide below


Here's a video showing the script being constructed

Not working? Click here for the video.

Functions (Blocks)

Key Info - Functions

Functions are extremely important. They allow is to keep our scripts tidy by grouping them into Blocks. They also allow us to write some code once but then use it over and over again in different parts of our program.

You are going to reuse the code that makes your pet die.

Many, many times.....

Scroll down

Task - Functions

This part is easy. You just need to create a new Block and call it Pet_Death.

Next replace the "When Space is pressed", with your new function.

Function

Stuck? Don't worry as it's on the video underneath.

Here's a video showing the script being constructed

Not working? Click here for the video.

Hungry

Making the Pet get hungry

Hungry Cat

We're now going to make a script that will make the cat get hungry

For this we're going to need a variable

Key Info - Variables

A variable is made up of two things

  1. Some data that is stored (like numbers or letters)
  2. A name that links to that data.

In other words, we can use a variable to store some data in our program


Scroll down for the task.

Task - Variables

Here's the pseudocode


when "Green Flag" clicked
set Pet_Hunger to 0
repeat until Pet_Hunger=100
	change Pet_Hunger by 1
	wait 1 second
Pet_Death
End All
						

Here are all the blocks you need to make the script. See if you can assemble the script now


when "Green Flag" clicked
set Pet_Hunger to 0
repeat until Pet_Hunger=100
	change Pet_Hunger by 1
	wait 1 second
Pet_Death
End All
						
Blocks for reset

If you need more help then go to the slide below.

Here is the completed Script

Reset Script

If you struggle to put it together then look at the slide below


Here's a video showing the script being constructed

Not working? Click here for the video.

Variables and Loops

Key Info - Breaking Down the Script

You've actually just used some fairly complicated concepts in Computing

Key Info - Variables

The pet_hunger variable starts off storing the number 0. But we can change that number at any time we like. We added 1 to it by using the block change pet_hunger by 1. The name of the variable stays the same, but the number it contains can change.


Key Info - Loops

Loops are very powerful. We can cause the same set of instructions to happen over and over again. In this case the


	change Pet_Hunger by 1
	wait 1 second
						

blocks are repeated until Pet_Hunger reaches 100. Then the loop ends and the script continues.

Feeding the Pet

Feeding the Pet

Pringle Cat

So we now have a program that basically kills a cat.

To make this into a more entertaining game, we need to be able to feed our pet.

Task - Feed The Pet

  1. Create a new Sprite and choose a costume for it, one that looks like food.
  2. Give the Sprite a sensible name like Pet_Food.
  3. Create the script detailed below.

	when This Sprite clicked
	change Pet_Hunger by -10
						

Once again, scroll down for the code blocks if you are stuck.

Code Blocks


	when This Sprite clicked
	change Pet_Hunger by -10
						

Scroll down for the video if you are still stuck

Not working? Click here for the video.

Testing your script

Click the green flag and let your script run

You should see the Pet_Hunger veriable gradually increase.

When it hits 100, your pet should die

If it doesn't work then get a friend or your teacher to look over your code so far.

Feeding the Pet - Extension

We now have a small problem with our program.

It is now possible to feed our pet so much that it becomes 'minus hungry'!

Minus Hungry

Extra Challenge

Can you use the 'IF.....' and operator blocks to set a limit so that the food sprite only works if it's value is 10 or more?

There's no pseudocode for this as it's an extra challenge. Don't worry if you get stuck, move on to the next section.

Pet Thirst

Task Pets get thirsty

Drinking Cat

We now need to create a new script that will make our pet get thirsty. We're going to use the same method as we used to make the pet get hungry.

The pseudocode

Firstly create a new variable called Pet_Thirst


when "Green Flag" clicked
set Pet_Thirst to 0
repeat until Pet_Thirst=100
	change Pet_Thirst by 1
	wait 1 second
Pet_Death
End All
						

The blocks

Thrist Blocks

The video

Not working? Click here for the video.


Task - Giving the pet a drink

Sprinkler Dog

Let's set up a way to give our pet a drink

Just like when we fed out pet we need a sprite to represent a drink. This time have a go at drawing the sprite and then setting up a script so that clicking on it decreases Pet_thirst. Try to do this without scrolling down.

The pseudocode


when "This Sprite" clicked
change Pet_Thirst by -10

						

The blocks

Thirst Blocks

The video

Not working? Click here for the video.


Task - Making the pet sad

Pets need hugs

Needy Cat

The penultimate (second from last) script of this type we're going to make is one to make our pet happy.

Have a think about this one, before you scroll down and look at the hints.

We need the pet to start happy and get sadder and sadder as time goes on. When the pet's happiness reaches zero it should die. If you click on the pet (we can't really give it a hug), it's happiness should increase.

Have a go at completing this yourself before using the hints below

The pseudocode


when "Green Flag" clicked
set Pet_Contentment to 100
repeat until Pet_Contentment = 0
	change Pet_Contentment by -1
	wait 1 second
Pet_Death
End All
						

The Blocks

Contentment Blocks

The video

Not working? Click here for the video.

Task - Making the Pet Tired

Pets get sleepy

Sleepy Cat

The next one is up to you and you alone. Create a script using a variable called Pet_Tiredness. It should start at 0 and increase up to 100, at which point the pet dies.

The player can rest the pet by clicking on a pet-bed (you can find, import or create your own sprite).

Once you've completed the script, get your teacher to check it.

Pet Customisation

The setup so far

Basic Scripts

Task - Customising your Virtual lol Cat

Computer Cat

Now for some customisation.

At the moment all the variables are initialised (started) at either 0 or 100. They also change be increments of 1 or -1 and do so in 1 second steps.

Customise your script a little. Maybe Pet_Tiredness can start at 200 and be reduced by 0.1 every 2 seconds, for instance

Play around with your variables and loops until you are happy with your game. Then we can move on to the next section.

Getting the Pet to Talk

Task - Getting the pet to talk to you.

I can haz Cheezburger?

It might be a good idea to get rid of the variables that are displayed on the game screen and instead have the pet sprite warn when it is getting hungry/thirsty or tired

We'll do it the easy way first and then make it a little tricky

Hiding the Variables

Add a new script to the window.


when "Green Flag" clicked
hide Pet_Hunger
						

Add a 'Hide' block for each of your variables.

This helps you to keep your game screen a bit tidier.

Key Info - A forever loop

Forever loops are quite useful. They will run and run and run. We can use forever loops to constantly 'listen' out for an event, like a variable hitting a certain value

We're going to use a forever loop to 'listen' out for when the pet is hungry, and then get it to run a function.

The pseudocode

We're going to need two scripts here. The first will be a forever loop that listens out for when pet hunger gets too high

The second will be a function that runs when called within the forever loop


def hungry
	ask 'I CAN HAZ CHEEZBURGER?' for two seconds
						
						
when "Green Flag" clicked
	forever
		if Pet_Hunger > 75
			hungry
						

The Blocks

Hungry Script

The Script

Hungry Script

The video

Not working? Click here for the video.

Adding Randomness

Key Info - Adding some randomness

Confused Cat

It get's boring if all the cat says is

"I CAN HAZ CHEEZBURGER?"

Lets add some randomness by using a list.

Key Info - Lists

List Cat

Lists are a type of Data Structure.

Key Info - An example


  1. OH HAI
  2. R U K?
  3. K THX BAI


This is an example of a list. It contains three items. They are all strings (letters).

We could put numbers into a list instead if we wanted.

The list is ordered. For instance, the second item is "R U K?"

Task - Creating a list

You're going to need to create a list and fill it with about five sentences.

Each sentence should be asking for food in some form

You can use normal English or lol speak if you want.

We haven't used lists yet, so scroll down to watch the video on how to make one.

The video

Not working? Click here for the video.

Speaking from the List

Task - Speaking from the list

Next we're going to get the pet to select random elements of the list to say

To do this we're going to edit the Hungry function

Here's the pseudocode


def hungry
	say item (random from 1 to 5) from Hungry
						

The blocks

Random Blocks

The video

Not working? Click here for the video.

Extension

Now it's your turn

You can now add some extra features to your program

Choose from Bronze, Silver or Gold challenges.

Scroll down to see the challenges.

Bronze Challenges

  • Add lists to give the pet options to speak when it is thirsty and tired.
  • Add scripts so that the pet needs exercising. It can even jump up and down as it exercises.
  • Add scripts so that the pet needs to go to the toilet.
  • Add scripts so that your pet gets naughtier and naughtier and needs telling off.

Scroll down for the silver challenges.

Silver Challenges

  • Add a start splash screen with instructions for the game. The user has to press a button to start the game.
  • Add keyboard shortcuts to feed, give the pet a drink etc.

Scroll down for the gold challenges.

Gold Challenges

  • Add a mini game that runs when you press a button. All the pet game sprites should disappear and the new game sprites should appear.
  • Add scrolling credits at the end of the game.

Too easy? Scroll down for the ultimate challenges of ultimate destiny!

The Ultimate Challenges of Ultimate Destiny

  • Allow the user to choose their pet from a choice of three on the start screen.
  • Make it so the time of day affects the pet tiredness. The later it gets, the quicker the pet gets tired. Also change the backdrop depending on the time.