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.
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
absoluteplaces an element atleftandtopinside a box that isrelative.- interval
- A timer that repeats.
setInterval(f, 1000)callsfevery 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
Step 1: The night sky
In the Web Studio, choose Day 5: Star Catcher. The game area is a
<div>with the idgame, and the star is a button inside it. In the CSS,position: relativeon the game andposition: absoluteon the star letleftandtopput the star anywhere inside the game.1<!DOCTYPE html>2<html>3 <head>4 <meta charset="utf-8">5 <title>Star Catcher</title>6 <link rel="stylesheet" href="style.css">7 </head>8 <body>9 <h1>Star Catcher</h1>10 <p class="stats">Score: <span id="score">0</span> · Time: <span id="time">20</span></p>11 <div id="game">12 <button id="star" aria-label="Star">⭐</button>13 </div>14 <script src="script.js"></script>15 </body>16</html>1body {2 font-family: Arial, sans-serif;3 text-align: center;4 background: #0b1026;5 color: white;6}7 8#game {9 position: relative;10 width: 100%;11 max-width: 420px;12 height: 260px;13 margin: 0 auto;14 background: #1b2452;15 border-radius: 14px;16}17 18#star {19 position: absolute;20 left: 180px;21 top: 100px;22 font-size: 34px;23 background: none;24 border: none;25 cursor: pointer;26}1A star in a night sky. Step 2: Click to score
moveStarpicks a random spot inside the game and moves the star there.clientWidthis the game's width in pixels, and taking away 50 keeps the whole star inside. Clicking the star scores a point and moves it.1const star = document.getElementById("star");2const game = document.getElementById("game");3const scoreText = document.getElementById("score");4 5let score = 0;6 7function moveStar() {8 const x = Math.random() * (game.clientWidth - 50);9 const y = Math.random() * (game.clientHeight - 50);10 star.style.left = x + "px";11 star.style.top = y + "px";12}13 14star.addEventListener("click", function () {15 score = score + 1;16 scoreText.textContent = score;17 moveStar();18});Click the star: it jumps, and the score goes up. Step 3: It moves by itself
setInterval(moveStar, 1000)callsmoveStarevery 1000 milliseconds, so the star jumps once a second even if you miss.New: lines 19 and 20.
1const star = document.getElementById("star");2const game = document.getElementById("game");3const scoreText = document.getElementById("score");4 5let score = 0;6 7function moveStar() {8 const x = Math.random() * (game.clientWidth - 50);9 const y = Math.random() * (game.clientHeight - 50);10 star.style.left = x + "px";11 star.style.top = y + "px";12}13 14star.addEventListener("click", function () {15 score = score + 1;16 scoreText.textContent = score;17 moveStar();18});19 20setInterval(moveStar, 1000);The star jumps every second. Can you catch it? Step 4: Beat the clock
Another interval counts down the time.
setIntervalgives back a number for each timer, kept inmoverandclock, andclearIntervalstops 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.
1const star = document.getElementById("star");2const game = document.getElementById("game");3const scoreText = document.getElementById("score");4const timeText = document.getElementById("time");5 6let score = 0;7let timeLeft = 20;8 9function moveStar() {10 const x = Math.random() * (game.clientWidth - 50);11 const y = Math.random() * (game.clientHeight - 50);12 star.style.left = x + "px";13 star.style.top = y + "px";14}15 16star.addEventListener("click", function () {17 score = score + 1;18 scoreText.textContent = score;19 moveStar();20});21 22function tick() {23 timeLeft = timeLeft - 1;24 timeText.textContent = timeLeft;25 if (timeLeft === 0) {26 clearInterval(mover);27 clearInterval(clock);28 star.hidden = true;29 }30}31 32const mover = setInterval(moveStar, 1000);33const clock = setInterval(tick, 1000);20 seconds, then the game stops. 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.
1<!DOCTYPE html>2<html>3 <head>4 <meta charset="utf-8">5 <title>Star Catcher</title>6 <link rel="stylesheet" href="style.css">7 </head>8 <body>9 <h1>Star Catcher</h1>10 <p class="stats">Score: <span id="score">0</span> · Time: <span id="time">20</span></p>11 <div id="game">12 <button id="star" aria-label="Star">⭐</button>13 </div>14 <button id="start">Start</button>15 <p id="message"></p>16 <script src="script.js"></script>17 </body>18</html>New: lines 27 to 36.
1body {2 font-family: Arial, sans-serif;3 text-align: center;4 background: #0b1026;5 color: white;6}7 8#game {9 position: relative;10 width: 100%;11 max-width: 420px;12 height: 260px;13 margin: 0 auto;14 background: #1b2452;15 border-radius: 14px;16}17 18#star {19 position: absolute;20 left: 180px;21 top: 100px;22 font-size: 34px;23 background: none;24 border: none;25 cursor: pointer;26}27 28#start {29 margin-top: 14px;30 font-size: 18px;31 padding: 10px 30px;32 border: none;33 border-radius: 30px;34 background: #f5b83d;35 cursor: pointer;36}New: lines 5, 6, 10 to 12, 31 and 35 to 54.
1const star = document.getElementById("star");2const game = document.getElementById("game");3const scoreText = document.getElementById("score");4const timeText = document.getElementById("time");5const startButton = document.getElementById("start");6const message = document.getElementById("message");7 8let score = 0;9let timeLeft = 20;10let mover = null;11let clock = null;12star.hidden = true;13 14function moveStar() {15 const x = Math.random() * (game.clientWidth - 50);16 const y = Math.random() * (game.clientHeight - 50);17 star.style.left = x + "px";18 star.style.top = y + "px";19}20 21star.addEventListener("click", function () {22 score = score + 1;23 scoreText.textContent = score;24 moveStar();25});26 27function tick() {28 timeLeft = timeLeft - 1;29 timeText.textContent = timeLeft;30 if (timeLeft === 0) {31 endGame();32 }33}34 35function endGame() {36 clearInterval(mover);37 clearInterval(clock);38 star.hidden = true;39 message.textContent = "Time's up! You caught " + score + " stars.";40 startButton.disabled = false;41}42 43startButton.addEventListener("click", function () {44 score = 0;45 timeLeft = 20;46 scoreText.textContent = score;47 timeText.textContent = timeLeft;48 message.textContent = "";49 star.hidden = false;50 startButton.disabled = true;51 moveStar();52 mover = setInterval(moveStar, 1000);53 clock = setInterval(tick, 1000);54});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.
Faster and faster
Make the star jump faster as the score goes up.
Show a hint
Use
setTimeoutinstead ofsetInterval, so each jump schedules the next one, and wait1000 - score * 30milliseconds, but never less than 300.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, andlocalStorage.getItem("best")reads it back. It only works when the page is opened from a file or a website, not inside a preview.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
movefunction and click listener.
The finished code
Show the whole page
1<!DOCTYPE html>2<html>3 <head>4 <meta charset="utf-8">5 <title>Star Catcher</title>6 <link rel="stylesheet" href="style.css">7 </head>8 <body>9 <h1>Star Catcher</h1>10 <p class="stats">Score: <span id="score">0</span> · Time: <span id="time">20</span></p>11 <div id="game">12 <button id="star" aria-label="Star">⭐</button>13 </div>14 <button id="start">Start</button>15 <p id="message"></p>16 <script src="script.js"></script>17 </body>18</html>1body {2 font-family: Arial, sans-serif;3 text-align: center;4 background: #0b1026;5 color: white;6}7 8#game {9 position: relative;10 width: 100%;11 max-width: 420px;12 height: 260px;13 margin: 0 auto;14 background: #1b2452;15 border-radius: 14px;16}17 18#star {19 position: absolute;20 left: 180px;21 top: 100px;22 font-size: 34px;23 background: none;24 border: none;25 cursor: pointer;26}27 28#start {29 margin-top: 14px;30 font-size: 18px;31 padding: 10px 30px;32 border: none;33 border-radius: 30px;34 background: #f5b83d;35 cursor: pointer;36}1const star = document.getElementById("star");2const game = document.getElementById("game");3const scoreText = document.getElementById("score");4const timeText = document.getElementById("time");5const startButton = document.getElementById("start");6const message = document.getElementById("message");7 8let score = 0;9let timeLeft = 20;10let mover = null;11let clock = null;12star.hidden = true;13 14function moveStar() {15 const x = Math.random() * (game.clientWidth - 50);16 const y = Math.random() * (game.clientHeight - 50);17 star.style.left = x + "px";18 star.style.top = y + "px";19}20 21star.addEventListener("click", function () {22 score = score + 1;23 scoreText.textContent = score;24 moveStar();25});26 27function tick() {28 timeLeft = timeLeft - 1;29 timeText.textContent = timeLeft;30 if (timeLeft === 0) {31 endGame();32 }33}34 35function endGame() {36 clearInterval(mover);37 clearInterval(clock);38 star.hidden = true;39 message.textContent = "Time's up! You caught " + score + " stars.";40 startButton.disabled = false;41}42 43startButton.addEventListener("click", function () {44 score = 0;45 timeLeft = 20;46 scoreText.textContent = score;47 timeText.textContent = timeLeft;48 message.textContent = "";49 star.hidden = false;50 startButton.disabled = true;51 moveStar();52 mover = setInterval(moveStar, 1000);53 clock = setInterval(tick, 1000);54});Notes for the teacher
Timing
- 10 minutes: step 1. Explain relative and absolute positioning with two sheets of paper.
- 25 minutes: steps 2 to 4.
- 15 minutes: step 5, then a class tournament.
Mistakes to look out for
- The star does not move: check
position: absoluteis in the CSS, and that"px"is added to the numbers. - Pressing Start twice before the end, in step 4, makes two timers and the clock runs at double speed. Step 5's
disabledbutton fixes this: a good debugging discussion. timeLeft == 0againsttimeLeft === 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.