Web development
Pet Pal
Look after a virtual pet: its food and fun drop every second, so feed it and play with it, or it gets sad.
Meet Mochi, a cat who lives in your browser. Every second Mochi gets a little hungrier and a little more bored. Two bars show how Mochi is doing, and it is up to you to keep them filled up. Leave Mochi alone too long and you might come back to an empty basket.
You will learn
- Draw bars whose width comes from a variable
- Change values over time with
setInterval - Keep numbers between limits with
Math.minandMath.max - Change a pet's mood with
ifandelse if
You will need
- A computer with a web browser
- The Web Studio, or Notepad
- Days 4 and 5, for events and intervals
Words to know
- state
- The values that describe something right now, such as
foodandfun. - interval
- A timer that repeats, here once a second.
- Math.min
- Gives the smaller of two numbers, used to stop a bar going over 100.
- else if
- A second test, checked only when the first was false.
Step by step
Step 1: Meet your pet
In the Web Studio, choose Day 6: Pet Pal. The page has the pet, a line about its mood, two bars and two buttons. Each bar is a grey
trackwith a greenfillinside: the fill's width will show how full the bar is.1<!DOCTYPE html>2<html>3 <head>4 <meta charset="utf-8">5 <title>Pet Pal</title>6 <link rel="stylesheet" href="style.css">7 </head>8 <body>9 <h1><span id="name">Mochi</span> the cat</h1>10 <div id="pet">🐱</div>11 <p id="mood">Mochi is happy.</p>12 <div class="bar"><span>Food</span><div class="track"><div id="food" class="fill"></div></div></div>13 <div class="bar"><span>Fun</span><div class="track"><div id="fun" class="fill"></div></div></div>14 <p>15 <button id="feed">Feed 🐟</button>16 <button id="play">Play 🧶</button>17 </p>18 <script src="script.js"></script>19 </body>20</html>1body {2 font-family: Arial, sans-serif;3 text-align: center;4 background: #fdf3e7;5}6 7#pet {8 font-size: 110px;9}10 11.bar {12 display: flex;13 align-items: center;14 gap: 10px;15 width: 300px;16 margin: 8px auto;17}18 19.bar span {20 width: 50px;21 text-align: right;22 font-weight: bold;23}24 25.track {26 flex: 1;27 height: 18px;28 background: #e3d5c5;29 border-radius: 9px;30 overflow: hidden;31}32 33.fill {34 height: 100%;35 width: 100%;36 background: #2f9e44;37}38 39button {40 font-size: 18px;41 padding: 8px 18px;42 border-radius: 20px;43 border: 2px solid #8a5a2b;44 background: white;45 cursor: pointer;46}1Mochi, with full Food and Fun bars. Step 2: Bars from numbers
foodandfunstart at 100.updatesets each bar's width from its number: 100 is100%wide, and 50 would be half full.update()on the last line draws the bars when the page opens.1let food = 100;2let fun = 100;3 4function update() {5 document.getElementById("food").style.width = food + "%";6 document.getElementById("fun").style.width = fun + "%";7}8 9update();The bars are full: both numbers are 100. Step 3: Time passes
Every second, food drops by 2 and fun by 3.
Math.max(0, ...)picks the bigger of two numbers, so they never go below 0.New: lines 9 to 14.
1let food = 100;2let fun = 100;3 4function update() {5 document.getElementById("food").style.width = food + "%";6 document.getElementById("fun").style.width = fun + "%";7}8 9const timer = setInterval(function () {10 food = Math.max(0, food - 2);11 fun = Math.max(0, fun - 3);12 update();13}, 1000);14 15update();Watch the bars shrink, second by second. Step 4: Feed and play
Each button adds 20 to its bar.
Math.min(100, ...)picks the smaller number, so a bar never goes over 100.New: lines 15 to 24.
1let food = 100;2let fun = 100;3 4function update() {5 document.getElementById("food").style.width = food + "%";6 document.getElementById("fun").style.width = fun + "%";7}8 9const timer = setInterval(function () {10 food = Math.max(0, food - 2);11 fun = Math.max(0, fun - 3);12 update();13}, 1000);14 15document.getElementById("feed").addEventListener("click", function () {16 food = Math.min(100, food + 20);17 update();18});19 20document.getElementById("play").addEventListener("click", function () {21 fun = Math.min(100, fun + 20);22 update();23});24 25update();Click Feed and Play to fill the bars up again. Step 5: Moods
Now Mochi reacts. If both bars are empty, Mochi runs away and the timer stops. Otherwise, if either bar is below 30, Mochi is sad.
&&means and,||means or.\u{1F63F}is the code for the sad cat emoji.New: lines 3 to 5 and 10 to 20.
1let food = 100;2let fun = 100;3let petName = "Mochi";4const pet = document.getElementById("pet");5const mood = document.getElementById("mood");6 7function update() {8 document.getElementById("food").style.width = food + "%";9 document.getElementById("fun").style.width = fun + "%";10 if (food === 0 && fun === 0) {11 clearInterval(timer);12 pet.textContent = "\u{1F4A8}";13 mood.textContent = petName + " ran away to find a better owner!";14 } else if (food < 30 || fun < 30) {15 pet.textContent = "\u{1F63F}";16 mood.textContent = petName + " is sad. Look after me!";17 } else {18 pet.textContent = "\u{1F431}";19 mood.textContent = petName + " is happy.";20 }21}22 23const timer = setInterval(function () {24 food = Math.max(0, food - 2);25 fun = Math.max(0, fun - 3);26 update();27}, 1000);28 29document.getElementById("feed").addEventListener("click", function () {30 food = Math.min(100, food + 20);31 update();32});33 34document.getElementById("play").addEventListener("click", function () {35 fun = Math.min(100, fun + 20);36 update();37});38 39update();Let a bar get low and Mochi looks sad. Step 6: Name your pet
Add a box to type a new name into, and a Rename button.
.valueis whatever was typed in the box. The name is kept inpetName, so the mood messages use it too.New: lines 18 to 21.
1<!DOCTYPE html>2<html>3 <head>4 <meta charset="utf-8">5 <title>Pet Pal</title>6 <link rel="stylesheet" href="style.css">7 </head>8 <body>9 <h1><span id="name">Mochi</span> the cat</h1>10 <div id="pet">🐱</div>11 <p id="mood">Mochi is happy.</p>12 <div class="bar"><span>Food</span><div class="track"><div id="food" class="fill"></div></div></div>13 <div class="bar"><span>Fun</span><div class="track"><div id="fun" class="fill"></div></div></div>14 <p>15 <button id="feed">Feed 🐟</button>16 <button id="play">Play 🧶</button>17 </p>18 <p>19 <input id="newname" placeholder="A new name" aria-label="A new name">20 <button id="rename">Rename</button>21 </p>22 <script src="script.js"></script>23 </body>24</html>New: lines 47 to 53.
1body {2 font-family: Arial, sans-serif;3 text-align: center;4 background: #fdf3e7;5}6 7#pet {8 font-size: 110px;9}10 11.bar {12 display: flex;13 align-items: center;14 gap: 10px;15 width: 300px;16 margin: 8px auto;17}18 19.bar span {20 width: 50px;21 text-align: right;22 font-weight: bold;23}24 25.track {26 flex: 1;27 height: 18px;28 background: #e3d5c5;29 border-radius: 9px;30 overflow: hidden;31}32 33.fill {34 height: 100%;35 width: 100%;36 background: #2f9e44;37}38 39button {40 font-size: 18px;41 padding: 8px 18px;42 border-radius: 20px;43 border: 2px solid #8a5a2b;44 background: white;45 cursor: pointer;46}47 48input {49 font-size: 16px;50 padding: 8px;51 border-radius: 8px;52 border: 2px solid #8a5a2b;53}New: lines 39 to 48.
1let food = 100;2let fun = 100;3let petName = "Mochi";4const pet = document.getElementById("pet");5const mood = document.getElementById("mood");6 7function update() {8 document.getElementById("food").style.width = food + "%";9 document.getElementById("fun").style.width = fun + "%";10 if (food === 0 && fun === 0) {11 clearInterval(timer);12 pet.textContent = "\u{1F4A8}";13 mood.textContent = petName + " ran away to find a better owner!";14 } else if (food < 30 || fun < 30) {15 pet.textContent = "\u{1F63F}";16 mood.textContent = petName + " is sad. Look after me!";17 } else {18 pet.textContent = "\u{1F431}";19 mood.textContent = petName + " is happy.";20 }21}22 23const timer = setInterval(function () {24 food = Math.max(0, food - 2);25 fun = Math.max(0, fun - 3);26 update();27}, 1000);28 29document.getElementById("feed").addEventListener("click", function () {30 food = Math.min(100, food + 20);31 update();32});33 34document.getElementById("play").addEventListener("click", function () {35 fun = Math.min(100, fun + 20);36 update();37});38 39document.getElementById("rename").addEventListener("click", function () {40 const box = document.getElementById("newname");41 if (box.value !== "") {42 petName = box.value;43 document.getElementById("name").textContent = petName;44 box.value = "";45 update();46 }47});48 49update();Type a name and press Rename.
Test it
Tick each one when it works. If one does not, look back at the step it comes from.
Stretch challenges
Finished early? Try these. Each one has a hint if you get stuck.
A sleep bar
Add a third bar, Sleep, with a Sleep button.
Show a hint
Copy a bar in the HTML with the id
sleep, then copy the code forfunin every place it appears, changingfuntosleep.Colour warnings
Turn a bar red when it is below 30.
Show a hint
In
update, setdocument.getElementById("food").style.backgroundto"red"or"#2f9e44"with anif.Choose a pet
Let the player choose a dog, a rabbit or a hamster at the start.
Show a hint
Add buttons for each pet. Keep the happy and sad emoji in variables, and change them when a pet is chosen.
The finished code
Show the whole page
1<!DOCTYPE html>2<html>3 <head>4 <meta charset="utf-8">5 <title>Pet Pal</title>6 <link rel="stylesheet" href="style.css">7 </head>8 <body>9 <h1><span id="name">Mochi</span> the cat</h1>10 <div id="pet">🐱</div>11 <p id="mood">Mochi is happy.</p>12 <div class="bar"><span>Food</span><div class="track"><div id="food" class="fill"></div></div></div>13 <div class="bar"><span>Fun</span><div class="track"><div id="fun" class="fill"></div></div></div>14 <p>15 <button id="feed">Feed 🐟</button>16 <button id="play">Play 🧶</button>17 </p>18 <p>19 <input id="newname" placeholder="A new name" aria-label="A new name">20 <button id="rename">Rename</button>21 </p>22 <script src="script.js"></script>23 </body>24</html>1body {2 font-family: Arial, sans-serif;3 text-align: center;4 background: #fdf3e7;5}6 7#pet {8 font-size: 110px;9}10 11.bar {12 display: flex;13 align-items: center;14 gap: 10px;15 width: 300px;16 margin: 8px auto;17}18 19.bar span {20 width: 50px;21 text-align: right;22 font-weight: bold;23}24 25.track {26 flex: 1;27 height: 18px;28 background: #e3d5c5;29 border-radius: 9px;30 overflow: hidden;31}32 33.fill {34 height: 100%;35 width: 100%;36 background: #2f9e44;37}38 39button {40 font-size: 18px;41 padding: 8px 18px;42 border-radius: 20px;43 border: 2px solid #8a5a2b;44 background: white;45 cursor: pointer;46}47 48input {49 font-size: 16px;50 padding: 8px;51 border-radius: 8px;52 border: 2px solid #8a5a2b;53}1let food = 100;2let fun = 100;3let petName = "Mochi";4const pet = document.getElementById("pet");5const mood = document.getElementById("mood");6 7function update() {8 document.getElementById("food").style.width = food + "%";9 document.getElementById("fun").style.width = fun + "%";10 if (food === 0 && fun === 0) {11 clearInterval(timer);12 pet.textContent = "\u{1F4A8}";13 mood.textContent = petName + " ran away to find a better owner!";14 } else if (food < 30 || fun < 30) {15 pet.textContent = "\u{1F63F}";16 mood.textContent = petName + " is sad. Look after me!";17 } else {18 pet.textContent = "\u{1F431}";19 mood.textContent = petName + " is happy.";20 }21}22 23const timer = setInterval(function () {24 food = Math.max(0, food - 2);25 fun = Math.max(0, fun - 3);26 update();27}, 1000);28 29document.getElementById("feed").addEventListener("click", function () {30 food = Math.min(100, food + 20);31 update();32});33 34document.getElementById("play").addEventListener("click", function () {35 fun = Math.min(100, fun + 20);36 update();37});38 39document.getElementById("rename").addEventListener("click", function () {40 const box = document.getElementById("newname");41 if (box.value !== "") {42 petName = box.value;43 document.getElementById("name").textContent = petName;44 box.value = "";45 update();46 }47});48 49update();Notes for the teacher
Timing
- 10 minutes: step 1, and how a bar is a box inside a box.
- 25 minutes: steps 2 to 4.
- 15 minutes: steps 5 and 6, then leave the pets running and see whose survives longest.
Mistakes to look out for
food + "%"without the"%": the width is a bare number, which CSS ignores.Math.minandMath.maxthe wrong way round: the bars stick at 0 or 100.update()called beforetimerexists: keep the order as it is in the steps.- In step 5, the check for both bars empty must come before the check for low bars.
Going further
This is state: a few numbers that describe the pet, changed by time and by events, and drawn by one update function. Most apps and games are organised exactly this way.