Fork me on GitHub Home

A Virtual lol Cat

A Project in Scratch

Created by Marc Scott / @coding2learn using reveal.js
Creative Commons License
A Virtual LOL Cat by Marc Scott is licensed under a Creative Commons Attribution 3.0 Unported License.

Contents

  • What is Scratch?
  • Project Objectives
  • Getting Started
  • Creating a new costume
  • The First Script
  • Resetting the Simulation
  • Scratch Functions
  • Making the Pet get Hungry
  • Variables and Loops
  • Feeding the Pet
  • Making the Pet get Thristy
  • Making the Pet get Sad
  • Making the Pet get Tired
  • Customising the LOL Cat
  • Getting the Pet to Talk
  • Adding Randomness
  • Speaking from a List
  • Extension Activities
  • Introduction

    Down arrow

    What is Scratch?

    GUI

    Scratch is a programming language that can be used to make small programs and games. Above is the Graphical User Interface (GUI) for Scratch. A GUI is how the a user interacts with software.

    Down arrow

    The screen is split into four main sections

    Key
    1. The Blocks Palette
    2. The Scripts Area
    3. The Stage
    4. The Sprite List

    Down arrow

    Getting to know Scratch

    Scratch works by dragging blocks of code from the blocks palate into the script area. These blocks then can be stacked to create scripts.

    Practice

    If you've never used Scratch before, you might want to check the slide below to see how to build scripts.

    Down arrow

    Watch the video to learn how to build scripts.

    Just watch, don't copy

    Objectives

    Down arrow

    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

    Download the project task sheet by clicking here

    Getting Started

    Down arrow

    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

    Down arrow

    Our first script

    Our first script will make the cat die!!!

    Creating a new

    Costume

    Down arrow

    Creating 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

    There's a video below

    Down arrow


    Down arrow

    Now click on the Costumes tab and then rename the first costume to Alive_Pet

    Costumes

    There's a video below showing how to do this if you're stuck

    Down arrow


    Down arrow

    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

    There's another video to help below if you need it.

    Down arrow

    The First Script

    Down arrow

    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 see if you can build the script yourself. If not then skip to the slide beneath.

    
    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 at any point then skip to the slide to the right

    Down arrow

    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 need more help then go to the slide below.

    Down arrow

    Here is the completed Script

    Death Script

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


    Down arrow

    Here's a video showing the script being constructed


    A Problem

    If you're script works then when you hit space the Cat dies

    However, he stays dead and we can't run the script again


    Resetting the simulation

    Down arrow

    A Problem

    If you're script works then when you hit space the Cat dies

    However, he stays dead and we can't run the script again


    Down arrow

    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 psudocode 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
    						

    Down arrow

    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.

    Down arrow

    Here is the completed Script

    Reset Script

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


    Down arrow

    Here's a video showing the script being constructed

    Functions (Blocks)

    Down arrow

    Functions

    Functions are extremely important. They allow is to keep our scripts tidy by grouping them into Blocks. They also allow us to run the same code over and over again in different parts of our program

    This part is easy. You just need to create a new function (Scratch calls them Blocks) 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 next video.

    Hungry

    Down arrow

    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

    Down arrow

    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


    Down arrow

    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
    						

    Down arrow

    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.

    Down arrow

    Here is the completed Script

    Reset Script

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


    Down arrow

    Here's a video showing the script being constructed

    Variables and Loops

    Down arrow

    Breaking down the script

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

    Variables

    The variable starts of storing the number 0. But we can change that number at any time we like. We added 1 to it within a loop. The name of the variable stays the same, but the number it points to can change.


    Down arrow

    Loops

    Loops are very powerful. We can cause the same set of instructions to happen over and over again, until some condition is met. In this case the;

    
    	change Pet_Hunger by 1
    	wait 1 second
    						

    keeps happening until Pet_Hunger reaches 100. Then the loop ends and the script continues

    Feeding the Pet

    Down arrow

    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

    Down arrow
    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
    						

    There's a video on the slide below

    Down arrow

    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.

    Pet Thirst

    Down arrow

    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.

    Give it a go now.

    Down arrow

    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
    						
    Down arrow

    The blocks

    Thrist Blocks
    Down arrow

    The script

    Thirst Script
    Down arrow

    The video


    Down arrow

    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.

    Down arrow

    The video

    Making the pet sad

    Down arrow

    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.

    Down arrow

    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

    Down arrow

    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
    						
    Down arrow

    The Blocks

    Contentment Blocks
    Down arrow

    The Script

    Contenment Script
    Down arrow

    The video

    Making the Pet Tired

    Down arrow

    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

    Down arrow

    The setup so far

    Basic Scripts
    Down arrow

    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

    Down arrow

    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

    Down arrow

    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.

    Down arrow

    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
    						
    Down arrow

    The Blocks

    Hungry Script
    Down arrow

    The Script

    Hungry Script
    Down arrow

    The video

    Adding Randomness

    Down arrow

    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.

    Down arrow

    Lists

    List Cat

    Lists are a type of Data Structure.

    Down arrow

    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?"

    Down arrow

    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.

    Down arrow

    The video

    Speaking from the List

    Down arrow

    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
    						
    Down arrow

    The blocks

    Random Blocks
    Down arrow

    The Script

    Random Script
    Down arrow

    The video

    Extension

    Down arrow

    Now it's your turn

    You can now add some extra features to your program

    Here are some ideas, or come up with your own...

    • 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.