Lesson 6 Β· Make a phone app
Build a virtual pet app
Make a pet that gets hungry, happy and sleepy when you tap its buttons.
About 25 minutes
Tab indents. Press Escape, then Tab, to leave the editor.
This is the big one! πΆ You're going to build a virtual pet app. Your pet will get hungry, sleepy and bored, and you'll look after it by tapping buttons. You'll use everything you've learned so far, plus two new tricks.
How a virtual pet works
A virtual pet isn't really alive, of course. It's just a few numbers that the app keeps track of:
- π Food: how full its tummy is
- πΎ Fun: how happy and playful it feels
- β‘ Energy: how wide awake it is
Each number goes from 0 (empty) to 10 (full). Tapping a button changes the numbers, and the app shows a mood emoji that matches how the pet feels.
| Button | What it does |
|---|---|
| π Feed | Food goes up |
| πΎ Play | Fun goes up, but playing uses energy and makes your pet a bit hungry |
| π€ Sleep | Energy goes up |
The numbers are variables, the same kind of named boxes you used for your click counter.
New trick 1: your own functions
You already know that a function is a set of instructions wrapped up together. You've been handing functions to addEventListener. But you can also make your own function and give it a name:
function show() {
foodText.textContent = food
}
This makes a function called show. Making it doesn't run it. To run it, you call it by writing its name with round brackets:
show()
It's like teaching a dog a trick called "sit". π You teach it once, then you just say "sit!" whenever you want it. In your pet app, show() puts all the numbers on the screen. Every button calls it after changing the numbers.
New trick 2: making choices with if
Your pet's mood should depend on its numbers. If it's really hungry, it should look hungry! For that, JavaScript has if:
if (food <= 2) {
moodText.textContent = 'π«'
} else {
moodText.textContent = 'π'
}
Read it like this: "If food is 2 or less, show the hungry face. Else (otherwise), show the happy face." The computer checks the question in the round brackets. If the answer is yes, it runs the first part. If the answer is no, it runs the part after else.
The <= sign means "less than or equal to". Here are the comparison signs you can use:
| Sign | Means |
|---|---|
< | less than |
<= | less than or equal to |
> | more than |
>= | more than or equal to |
=== | exactly equal to |
You can ask more than one question with else if. The computer checks each one in order and stops at the first yes:
if (food <= 2) {
moodText.textContent = 'π«'
} else if (energy <= 2) {
moodText.textContent = 'π΄'
} else {
moodText.textContent = 'π'
}
Build your pet
Time to bring your pet to life! The Feed button already works. You'll make the other two buttons work, then give your pet moods.
Challenge
Build a virtual pet
- Press Run and tap Feed. Food goes up!
- Make Play work: add 3 to
fun, take 2 away fromenergy, and take 1 away fromfood. Then callshow(). - Make Sleep work: add 4 to
energy, then callshow(). - In
show(), useifandelseso the mood emoji changes when your pet is hungry or tired.
Then choose your own pet and name in the HTML tab! π±π°π¦
- Nothing logged yet.
- Tapping Feed gives your pet more food
- Tapping Play gives your pet more fun
- Playing uses up some energy
- Tapping Sleep gives your pet more energy
- The numbers always stay between 0 and 10
- Your pet's mood changes when it's tired or hungry
- Your pet cheers up again after food and sleep
You built a real app with screens, buttons, numbers and choices. Give yourself a high five! π
What you learned
- A virtual pet is really a few variables that buttons change.
- You can make your own function, like
show(), and call it whenever you need it. if,else ifandelselet the computer make choices, checking each question in order.- Signs like
<=and>compare numbers. - You built a whole virtual pet app that you can play with! πΆπ
Finished this lesson?
Mark it complete to track your progress on this device.