Web development

Star Catcher

Make a game: a star jumps around a night sky, and you have 20 seconds to click it as many times as you can.

Day 5 50 to 60 minutes 5 steps Key Stage 3
Start step 1
star-catcher/index.html

Today you will make a real game. A star appears somewhere in a night sky. Click it and it jumps somewhere else, and you score a point. But the star moves on its own every second, and the clock is ticking.

You will learn

  • Place things anywhere with position: absolute
  • Move an element with code
  • Repeat code every second with setInterval
  • Start, stop and restart a game

You will need

  • A computer with a web browser
  • The Web Studio, or Notepad
  • Day 4, for buttons and click events

Words to know

position
absolute places an element at left and top inside a box that is relative.
interval
A timer that repeats. setInterval(f, 1000) calls f every second.
function
A named block of code that runs when it is called.
game loop
Code that runs again and again to keep a game moving.

Step by step

  1. Step 1: The night sky

    In the Web Studio, choose Day 5: Star Catcher. The game area is a <div> with the id game, and the star is a button inside it. In the CSS, position: relative on the game and position: absolute on the star let left and top put the star anywhere inside the game.

    index.htmlHTML
    <!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title>Star Catcher</title>    <link rel="stylesheet" href="style.css">  </head>  <body>    <h1>Star Catcher</h1>    <p class="stats">Score: <span id="score">0</span> &middot; Time: <span id="time">20</span></p>    <div id="game">      <button id="star" aria-label="Star">&#11088;</button>    </div>    <script src="script.js"></script>  </body></html>
    style.cssCSS
    body {  font-family: Arial, sans-serif;  text-align: center;  background: #0b1026;  color: white;} #game {  position: relative;  width: 100%;  max-width: 420px;  height: 260px;  margin: 0 auto;  background: #1b2452;  border-radius: 14px;} #star {  position: absolute;  left: 180px;  top: 100px;  font-size: 34px;  background: none;  border: none;  cursor: pointer;}
    script.jsJavaScript
     
    star-catcher/index.html
    A star in a night sky.
  2. Step 2: Click to score

    moveStar picks a random spot inside the game and moves the star there. clientWidth is the game's width in pixels, and taking away 50 keeps the whole star inside. Clicking the star scores a point and moves it.

    script.jsJavaScript
    const star = document.getElementById("star");const game = document.getElementById("game");const scoreText = document.getElementById("score"); let score = 0; function moveStar() {  const x = Math.random() * (game.clientWidth - 50);  const y = Math.random() * (game.clientHeight - 50);  star.style.left = x + "px";  star.style.top = y + "px";} star.addEventListener("click", function () {  score = score + 1;  scoreText.textContent = score;  moveStar();});
    star-catcher/index.html
    Click the star: it jumps, and the score goes up.
  3. Step 3: It moves by itself

    setInterval(moveStar, 1000) calls moveStar every 1000 milliseconds, so the star jumps once a second even if you miss.

    New: lines 19 and 20.

    script.jsJavaScript
    const star = document.getElementById("star");const game = document.getElementById("game");const scoreText = document.getElementById("score"); let score = 0; function moveStar() {  const x = Math.random() * (game.clientWidth - 50);  const y = Math.random() * (game.clientHeight - 50);  star.style.left = x + "px";  star.style.top = y + "px";} star.addEventListener("click", function () {  score = score + 1;  scoreText.textContent = score;  moveStar();}); setInterval(moveStar, 1000);
    star-catcher/index.html
    The star jumps every second. Can you catch it?
  4. Step 4: Beat the clock

    Another interval counts down the time. setInterval gives back a number for each timer, kept in mover and clock, and clearInterval stops a timer by its number. At zero, both stop and the star hides. === checks whether two things are equal.

    New: lines 4, 7 and 22 to 33.

    script.jsJavaScript
    const star = document.getElementById("star");const game = document.getElementById("game");const scoreText = document.getElementById("score");const timeText = document.getElementById("time"); let score = 0;let timeLeft = 20; function moveStar() {  const x = Math.random() * (game.clientWidth - 50);  const y = Math.random() * (game.clientHeight - 50);  star.style.left = x + "px";  star.style.top = y + "px";} star.addEventListener("click", function () {  score = score + 1;  scoreText.textContent = score;  moveStar();}); function tick() {  timeLeft = timeLeft - 1;  timeText.textContent = timeLeft;  if (timeLeft === 0) {    clearInterval(mover);    clearInterval(clock);    star.hidden = true;  }} const mover = setInterval(moveStar, 1000);const clock = setInterval(tick, 1000);
    star-catcher/index.html
    20 seconds, then the game stops.
  5. Step 5: Start and play again

    Add a Start button and a message. The game now waits for Start, and Start resets the score and the time, so you can play again and again. The end of the game moves into its own function, endGame.

    New: lines 14 and 15.

    index.htmlHTML
    <!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title>Star Catcher</title>    <link rel="stylesheet" href="style.css">  </head>  <body>    <h1>Star Catcher</h1>    <p class="stats">Score: <span id="score">0</span> &middot; Time: <span id="time">20</span></p>    <div id="game">      <button id="star" aria-label="Star">&#11088;</button>    </div>    <button id="start">Start</button>    <p id="message"></p>    <script src="script.js"></script>  </body></html>

    New: lines 27 to 36.

    style.cssCSS
    body {  font-family: Arial, sans-serif;  text-align: center;  background: #0b1026;  color: white;} #game {  position: relative;  width: 100%;  max-width: 420px;  height: 260px;  margin: 0 auto;  background: #1b2452;  border-radius: 14px;} #star {  position: absolute;  left: 180px;  top: 100px;  font-size: 34px;  background: none;  border: none;  cursor: pointer;} #start {  margin-top: 14px;  font-size: 18px;  padding: 10px 30px;  border: none;  border-radius: 30px;  background: #f5b83d;  cursor: pointer;}

    New: lines 5, 6, 10 to 12, 31 and 35 to 54.

    script.jsJavaScript
    const star = document.getElementById("star");const game = document.getElementById("game");const scoreText = document.getElementById("score");const timeText = document.getElementById("time");const startButton = document.getElementById("start");const message = document.getElementById("message"); let score = 0;let timeLeft = 20;let mover = null;let clock = null;star.hidden = true; function moveStar() {  const x = Math.random() * (game.clientWidth - 50);  const y = Math.random() * (game.clientHeight - 50);  star.style.left = x + "px";  star.style.top = y + "px";} star.addEventListener("click", function () {  score = score + 1;  scoreText.textContent = score;  moveStar();}); function tick() {  timeLeft = timeLeft - 1;  timeText.textContent = timeLeft;  if (timeLeft === 0) {    endGame();  }} function endGame() {  clearInterval(mover);  clearInterval(clock);  star.hidden = true;  message.textContent = "Time's up! You caught " + score + " stars.";  startButton.disabled = false;} startButton.addEventListener("click", function () {  score = 0;  timeLeft = 20;  scoreText.textContent = score;  timeText.textContent = timeLeft;  message.textContent = "";  star.hidden = false;  startButton.disabled = true;  moveStar();  mover = setInterval(moveStar, 1000);  clock = setInterval(tick, 1000);});
    star-catcher/index.html
    Press Start, then catch as many stars as you can.

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. Faster and faster

    Make the star jump faster as the score goes up.

    Show a hint

    Use setTimeout instead of setInterval, so each jump schedules the next one, and wait 1000 - score * 30 milliseconds, but never less than 300.

  2. High score

    Remember the best score, even after the page is closed.

    Show a hint

    localStorage.setItem("best", score) saves a value in the browser, and localStorage.getItem("best") reads it back. It only works when the page is opened from a file or a website, not inside a preview.

  3. A bad star

    Add a second button, a skull, that loses a point when clicked.

    Show a hint

    Copy the star's HTML with a new id, and give it its own move function and click listener.

The finished code

Show the whole page
index.htmlHTML
<!DOCTYPE html><html>  <head>    <meta charset="utf-8">    <title>Star Catcher</title>    <link rel="stylesheet" href="style.css">  </head>  <body>    <h1>Star Catcher</h1>    <p class="stats">Score: <span id="score">0</span> &middot; Time: <span id="time">20</span></p>    <div id="game">      <button id="star" aria-label="Star">&#11088;</button>    </div>    <button id="start">Start</button>    <p id="message"></p>    <script src="script.js"></script>  </body></html>
style.cssCSS
body {  font-family: Arial, sans-serif;  text-align: center;  background: #0b1026;  color: white;} #game {  position: relative;  width: 100%;  max-width: 420px;  height: 260px;  margin: 0 auto;  background: #1b2452;  border-radius: 14px;} #star {  position: absolute;  left: 180px;  top: 100px;  font-size: 34px;  background: none;  border: none;  cursor: pointer;} #start {  margin-top: 14px;  font-size: 18px;  padding: 10px 30px;  border: none;  border-radius: 30px;  background: #f5b83d;  cursor: pointer;}
script.jsJavaScript
const star = document.getElementById("star");const game = document.getElementById("game");const scoreText = document.getElementById("score");const timeText = document.getElementById("time");const startButton = document.getElementById("start");const message = document.getElementById("message"); let score = 0;let timeLeft = 20;let mover = null;let clock = null;star.hidden = true; function moveStar() {  const x = Math.random() * (game.clientWidth - 50);  const y = Math.random() * (game.clientHeight - 50);  star.style.left = x + "px";  star.style.top = y + "px";} star.addEventListener("click", function () {  score = score + 1;  scoreText.textContent = score;  moveStar();}); function tick() {  timeLeft = timeLeft - 1;  timeText.textContent = timeLeft;  if (timeLeft === 0) {    endGame();  }} function endGame() {  clearInterval(mover);  clearInterval(clock);  star.hidden = true;  message.textContent = "Time's up! You caught " + score + " stars.";  startButton.disabled = false;} startButton.addEventListener("click", function () {  score = 0;  timeLeft = 20;  scoreText.textContent = score;  timeText.textContent = timeLeft;  message.textContent = "";  star.hidden = false;  startButton.disabled = true;  moveStar();  mover = setInterval(moveStar, 1000);  clock = setInterval(tick, 1000);});
Notes for the teacher

Timing

  1. 10 minutes: step 1. Explain relative and absolute positioning with two sheets of paper.
  2. 25 minutes: steps 2 to 4.
  3. 15 minutes: step 5, then a class tournament.

Mistakes to look out for

  1. The star does not move: check position: absolute is in the CSS, and that "px" is added to the numbers.
  2. Pressing Start twice before the end, in step 4, makes two timers and the clock runs at double speed. Step 5's disabled button fixes this: a good debugging discussion.
  3. timeLeft == 0 against timeLeft === 0: both work here, but === is the safer habit in JavaScript.

Going further

setInterval is the heartbeat of a game, like the while True: loop with sleep() on the micro:bit. Ask students to find the game loop in each.