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.

Day 6 50 to 60 minutes 6 steps Key Stage 3
Start step 1
pet-pal/index.html

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.min and Math.max
  • Change a pet's mood with if and else 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 food and fun.
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

  1. 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 track with a green fill inside: the fill's width will show how full the bar is.

    index.htmlHTML
    <!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title>Pet Pal</title>    <link rel="stylesheet" href="style.css">  </head>  <body>    <h1><span id="name">Mochi</span> the cat</h1>    <div id="pet">&#128049;</div>    <p id="mood">Mochi is happy.</p>    <div class="bar"><span>Food</span><div class="track"><div id="food" class="fill"></div></div></div>    <div class="bar"><span>Fun</span><div class="track"><div id="fun" class="fill"></div></div></div>    <p>      <button id="feed">Feed &#128031;</button>      <button id="play">Play &#129526;</button>    </p>    <script src="script.js"></script>  </body></html>
    style.cssCSS
    body {  font-family: Arial, sans-serif;  text-align: center;  background: #fdf3e7;} #pet {  font-size: 110px;} .bar {  display: flex;  align-items: center;  gap: 10px;  width: 300px;  margin: 8px auto;} .bar span {  width: 50px;  text-align: right;  font-weight: bold;} .track {  flex: 1;  height: 18px;  background: #e3d5c5;  border-radius: 9px;  overflow: hidden;} .fill {  height: 100%;  width: 100%;  background: #2f9e44;} button {  font-size: 18px;  padding: 8px 18px;  border-radius: 20px;  border: 2px solid #8a5a2b;  background: white;  cursor: pointer;}
    script.jsJavaScript
     
    pet-pal/index.html
    Mochi, with full Food and Fun bars.
  2. Step 2: Bars from numbers

    food and fun start at 100. update sets each bar's width from its number: 100 is 100% wide, and 50 would be half full. update() on the last line draws the bars when the page opens.

    script.jsJavaScript
    let food = 100;let fun = 100; function update() {  document.getElementById("food").style.width = food + "%";  document.getElementById("fun").style.width = fun + "%";} update();
    pet-pal/index.html
    The bars are full: both numbers are 100.
  3. 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.

    script.jsJavaScript
    let food = 100;let fun = 100; function update() {  document.getElementById("food").style.width = food + "%";  document.getElementById("fun").style.width = fun + "%";} const timer = setInterval(function () {  food = Math.max(0, food - 2);  fun = Math.max(0, fun - 3);  update();}, 1000); update();
    pet-pal/index.html
    Watch the bars shrink, second by second.
  4. 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.

    script.jsJavaScript
    let food = 100;let fun = 100; function update() {  document.getElementById("food").style.width = food + "%";  document.getElementById("fun").style.width = fun + "%";} const timer = setInterval(function () {  food = Math.max(0, food - 2);  fun = Math.max(0, fun - 3);  update();}, 1000); document.getElementById("feed").addEventListener("click", function () {  food = Math.min(100, food + 20);  update();}); document.getElementById("play").addEventListener("click", function () {  fun = Math.min(100, fun + 20);  update();}); update();
    pet-pal/index.html
    Click Feed and Play to fill the bars up again.
  5. 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.

    script.jsJavaScript
    let food = 100;let fun = 100;let petName = "Mochi";const pet = document.getElementById("pet");const mood = document.getElementById("mood"); function update() {  document.getElementById("food").style.width = food + "%";  document.getElementById("fun").style.width = fun + "%";  if (food === 0 && fun === 0) {    clearInterval(timer);    pet.textContent = "\u{1F4A8}";    mood.textContent = petName + " ran away to find a better owner!";  } else if (food < 30 || fun < 30) {    pet.textContent = "\u{1F63F}";    mood.textContent = petName + " is sad. Look after me!";  } else {    pet.textContent = "\u{1F431}";    mood.textContent = petName + " is happy.";  }} const timer = setInterval(function () {  food = Math.max(0, food - 2);  fun = Math.max(0, fun - 3);  update();}, 1000); document.getElementById("feed").addEventListener("click", function () {  food = Math.min(100, food + 20);  update();}); document.getElementById("play").addEventListener("click", function () {  fun = Math.min(100, fun + 20);  update();}); update();
    pet-pal/index.html
    Let a bar get low and Mochi looks sad.
  6. Step 6: Name your pet

    Add a box to type a new name into, and a Rename button. .value is whatever was typed in the box. The name is kept in petName, so the mood messages use it too.

    New: lines 18 to 21.

    index.htmlHTML
    <!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title>Pet Pal</title>    <link rel="stylesheet" href="style.css">  </head>  <body>    <h1><span id="name">Mochi</span> the cat</h1>    <div id="pet">&#128049;</div>    <p id="mood">Mochi is happy.</p>    <div class="bar"><span>Food</span><div class="track"><div id="food" class="fill"></div></div></div>    <div class="bar"><span>Fun</span><div class="track"><div id="fun" class="fill"></div></div></div>    <p>      <button id="feed">Feed &#128031;</button>      <button id="play">Play &#129526;</button>    </p>    <p>      <input id="newname" placeholder="A new name" aria-label="A new name">      <button id="rename">Rename</button>    </p>    <script src="script.js"></script>  </body></html>

    New: lines 47 to 53.

    style.cssCSS
    body {  font-family: Arial, sans-serif;  text-align: center;  background: #fdf3e7;} #pet {  font-size: 110px;} .bar {  display: flex;  align-items: center;  gap: 10px;  width: 300px;  margin: 8px auto;} .bar span {  width: 50px;  text-align: right;  font-weight: bold;} .track {  flex: 1;  height: 18px;  background: #e3d5c5;  border-radius: 9px;  overflow: hidden;} .fill {  height: 100%;  width: 100%;  background: #2f9e44;} button {  font-size: 18px;  padding: 8px 18px;  border-radius: 20px;  border: 2px solid #8a5a2b;  background: white;  cursor: pointer;} input {  font-size: 16px;  padding: 8px;  border-radius: 8px;  border: 2px solid #8a5a2b;}

    New: lines 39 to 48.

    script.jsJavaScript
    let food = 100;let fun = 100;let petName = "Mochi";const pet = document.getElementById("pet");const mood = document.getElementById("mood"); function update() {  document.getElementById("food").style.width = food + "%";  document.getElementById("fun").style.width = fun + "%";  if (food === 0 && fun === 0) {    clearInterval(timer);    pet.textContent = "\u{1F4A8}";    mood.textContent = petName + " ran away to find a better owner!";  } else if (food < 30 || fun < 30) {    pet.textContent = "\u{1F63F}";    mood.textContent = petName + " is sad. Look after me!";  } else {    pet.textContent = "\u{1F431}";    mood.textContent = petName + " is happy.";  }} const timer = setInterval(function () {  food = Math.max(0, food - 2);  fun = Math.max(0, fun - 3);  update();}, 1000); document.getElementById("feed").addEventListener("click", function () {  food = Math.min(100, food + 20);  update();}); document.getElementById("play").addEventListener("click", function () {  fun = Math.min(100, fun + 20);  update();}); document.getElementById("rename").addEventListener("click", function () {  const box = document.getElementById("newname");  if (box.value !== "") {    petName = box.value;    document.getElementById("name").textContent = petName;    box.value = "";    update();  }}); update();
    pet-pal/index.html
    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.

  1. 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 for fun in every place it appears, changing fun to sleep.

  2. Colour warnings

    Turn a bar red when it is below 30.

    Show a hint

    In update, set document.getElementById("food").style.background to "red" or "#2f9e44" with an if.

  3. 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
index.htmlHTML
<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title>Pet Pal</title>    <link rel="stylesheet" href="style.css">  </head>  <body>    <h1><span id="name">Mochi</span> the cat</h1>    <div id="pet">&#128049;</div>    <p id="mood">Mochi is happy.</p>    <div class="bar"><span>Food</span><div class="track"><div id="food" class="fill"></div></div></div>    <div class="bar"><span>Fun</span><div class="track"><div id="fun" class="fill"></div></div></div>    <p>      <button id="feed">Feed &#128031;</button>      <button id="play">Play &#129526;</button>    </p>    <p>      <input id="newname" placeholder="A new name" aria-label="A new name">      <button id="rename">Rename</button>    </p>    <script src="script.js"></script>  </body></html>
style.cssCSS
body {  font-family: Arial, sans-serif;  text-align: center;  background: #fdf3e7;} #pet {  font-size: 110px;} .bar {  display: flex;  align-items: center;  gap: 10px;  width: 300px;  margin: 8px auto;} .bar span {  width: 50px;  text-align: right;  font-weight: bold;} .track {  flex: 1;  height: 18px;  background: #e3d5c5;  border-radius: 9px;  overflow: hidden;} .fill {  height: 100%;  width: 100%;  background: #2f9e44;} button {  font-size: 18px;  padding: 8px 18px;  border-radius: 20px;  border: 2px solid #8a5a2b;  background: white;  cursor: pointer;} input {  font-size: 16px;  padding: 8px;  border-radius: 8px;  border: 2px solid #8a5a2b;}
script.jsJavaScript
let food = 100;let fun = 100;let petName = "Mochi";const pet = document.getElementById("pet");const mood = document.getElementById("mood"); function update() {  document.getElementById("food").style.width = food + "%";  document.getElementById("fun").style.width = fun + "%";  if (food === 0 && fun === 0) {    clearInterval(timer);    pet.textContent = "\u{1F4A8}";    mood.textContent = petName + " ran away to find a better owner!";  } else if (food < 30 || fun < 30) {    pet.textContent = "\u{1F63F}";    mood.textContent = petName + " is sad. Look after me!";  } else {    pet.textContent = "\u{1F431}";    mood.textContent = petName + " is happy.";  }} const timer = setInterval(function () {  food = Math.max(0, food - 2);  fun = Math.max(0, fun - 3);  update();}, 1000); document.getElementById("feed").addEventListener("click", function () {  food = Math.min(100, food + 20);  update();}); document.getElementById("play").addEventListener("click", function () {  fun = Math.min(100, fun + 20);  update();}); document.getElementById("rename").addEventListener("click", function () {  const box = document.getElementById("newname");  if (box.value !== "") {    petName = box.value;    document.getElementById("name").textContent = petName;    box.value = "";    update();  }}); update();
Notes for the teacher

Timing

  1. 10 minutes: step 1, and how a bar is a box inside a box.
  2. 25 minutes: steps 2 to 4.
  3. 15 minutes: steps 5 and 6, then leave the pets running and see whose survives longest.

Mistakes to look out for

  1. food + "%" without the "%": the width is a bare number, which CSS ignores.
  2. Math.min and Math.max the wrong way round: the bars stick at 0 or 100.
  3. update() called before timer exists: keep the order as it is in the steps.
  4. 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.