🎯 Learning Objectives
Develop the Programming and Development, Algorithms, Literacy, Information Technology Learning Strands:
- Develop an understanding of structured programming.
- Be to able to declare and assign variables.
- Be able to use conditional statements such as if statements.
- Be able to use arithmetic operators and loops.
- Develop an understanding and appropriate use of lists.
📝 Try It – Customising your Virtual Pet
- At the moment your Virtual Pet is probably a little dull.
- You’re going to need to experiment a little now.
- Try changing the starting values of the variables and the time it takes for the variables to change.
▶️ Run It – 🧪 Test It – ⌨️ Improve It
- Run your code and see whether the values you have chosen seem sensible
- Have a friend try your Virtual Pet and give you some feedback.
- Improve your code based on the feedback you get
Getting the Pet to talk with an Infinite loop and Conditionals
- At the moment a user of the Virtual Pet only knows when the pet is hungry by watching the variable value change.
- From a User Experience (UX) point of view, this isn’t great, so let’s make the pet speak to us and tell us when it is hungry.
- We’ll start off with a simple interaction, and then make it more
📖 Learn It
- Sometimes in our programmes we want an infinite loop.
- An infinite loop is one that goes on forever (or until the program is quit).
- We can do this by using a
forever
loop in Scratch. - The code in a
forever
loop will run until the program ends.
📖 Learn It
- Conditional statements are very important in programming.
- Conditional statements are like branches in the program. If a condition is true then one branch is taken, and if the condition is false another branch is taken.
- For instance, if you want to go to the shops then you might use the following conditional statement to make a decision on how to get there.
if the distance to the shops is less than a mile then
walk
else
take the bus
⌨️ Code It
- We’ll need a whole new script for this part, so create a new script on the Pet Sprite.
- Use the following pseudocode, and pay attention to the indentation, as this will help you with the nesting of the blocks.
when green flag clicked
forever
if PetHunger > 80 then
say "I CAN HAZ CHEEZBURGER?" for 2 secs
- Your Scratch code should look something like the following:
▶️ Run It
- Run your code and make sure the Pet speaks when it get’s too hungry and needs feeding.
Adding some more things to say
- At the moment all the Pet says is “I CAN HAZ CHEEZBURGER?”
- Let’s give it a few more things to say, and then choose what is spoken.
- To do this we’ll need to use a list.
📖 Learn It
- A list is a data structure.
- Data structures are a way of storing lots of data.
- You probably use lists all the time. Your planner probably contains lists of homeworks you have to do each day.
- We’re going to create a list and fill it with items that the Pet can say.
⌨️ Code It
- Make sure that you have your virtual pet selected.
- Click on the
Code
Palatte. - Click on
Variables
. - Then click on
Make a List
. - Name the list
HungryTalk
- Click
OK
You should now see an empty list on the Stage.
⌨️ Code It
- Click on the
+
symbol - You can now add things for the Pet to say when it is hungry
- Add at least five things for the Pet to say, to tell the user that it is hungry.
- If you want to use LOLSPEAK, then this website might help you translate.
Here is an example of things you could get your pet to ask for:
Learn It – Choosing From the List Randomly
- We want to use a random number generator to choose what the Pet is going to say.
- Randomness and computers don’t really work. Computers are really just a bunch of tiny switches and a data store, and making switches turn off and on randomly is something that is really tricky.
- We tend to call random numbers generated by a computer pseudo-random. This means pretend random. The numbers might appear to be random to humans, but in reality they have been chosen by the computer based on some form of input (often using the inbuilt clock that all computers have).
⌨️ Code It
- We’re now going to change our script a little to use a random number to speak from the list.
- Change this pseudocode script:
when green flag clicked
forever
if PetHunger > 80 then
say "I CAN HAZ CHEEZBURGER?" for 2 secs
- So it looks like this pseudocode:
when green flag clicked
forever
if PetHunger > 80 then
say (item (pick random 1 to (length of HungryTalk)) of HungryTalk) for 2 secs
- We’ve used brackets to indicate the nesting of blocks, but if you get stuck then you can look at the example below.
- Here is an example of how your Scratch blocks should look:
▶️ Run It
- Run your code and make sure that it works; picking a random element from your list each time.
🏅 Badge It – Doing it again and again and again
- To earn your next badge you need to set it all up for
Tiredness, Happiness
andThirst.
- Make sure that you create lists for each of the different variables, so the pet can tell the user what is wrong.
- Make sure you have at
least five elements
in each list. - You’ll receive:
🥈 Silver Badge
- You have finished one of the lists with at least five items in it and used it in a loop and a conditional.
🥇 Gold Badge
- You have finished two of the lists with at least five items in each and used each in a loop and a conditional.
🥉 Platinum Badge
- You have finished three of the lists with at least five items in each and used each in a loop and a conditional.
Upload screenshots of all of your badge tasks to bournetolearn.com