Variables for Score, Lives and More
Store a game's score, lives and player name in variables, update them with arithmetic, and avoid the classic text-versus-number bug.
Welcome back!
Step 1 of 12Learn
Games are numbers that change
Think about the last game you played. Under the graphics, the game was tracking a pile of numbers: your score, how many lives you had left, your position, how much time remained, which level you were on. Programmers call all of these values together the game state. Every frame, the game reads the state, changes it a little, and draws the result.
In JavaScript, you store each piece of state in a variable, a named box that holds a value:
let score = 0;
let lives = 3;
let playerName = "Nova";
let isGameOver = false;letcreates a variable whose value can change.scoreis the name. Names can't contain spaces, so programmers write multi-word names in camelCase:playerName,isGameOver.=means "store this value in the box," not "is equal to." That trips people up at first.- The semicolon ends the statement.
There are three types of value here. 0 and 3 are numbers. "Nova" is a string, text inside quotes. false is a Boolean, which can only be true or false, perfect for yes-or-no state like whether the game is over or a door is open.
Step 2 of 12Practice · Sort into groups
What type of value would you use for each piece of game state?
Tap an item, then tap its group (or drag it there).
Step 3 of 12Diagram
The game state of a simple platformer
Every frame, the game reads these variables, changes some of them and draws the result. Tap a part to see its type.
Tap a shape with to see more.
- Game state
- score = 0
A number. It goes up with every coin.
- lives = 3
A number. It goes down when the player is hit.
- playerName = "Nova"
A string: text in quotes.
- isGameOver = false
A Boolean: only
trueorfalse.- level = 1
A number that counts up as the player progresses.
Step 4 of 12Learn
Changing values
A variable is only useful because it can change. You update it by assigning a new value, often calculated from the old one:
score = score + 10; // the player grabbed a coin
lives = lives - 1; // the player got hitRead the first line from right to left: take the current score, add 10, and store the result back in score. If score was 40, it's now 50.
This happens so often that JavaScript has shortcuts:
score += 10; // same as score = score + 10
lives -= 1; // same as lives = lives - 1
lives--; // subtract exactly 1
level++; // add exactly 1Some values should never change during the game, like the width of the screen or the gravity strength. For those, use const instead of let:
const GRAVITY = 0.5;If any code later tries GRAVITY = 2;, JavaScript throws an error. That's a feature: it protects you from changing something by accident. Many programmers write constants in UPPER_CASE so they stand out.
A good rule: start with const, and switch to let only when the value really needs to change.
Step 5 of 12Flashcards
Update shortcuts
Tap a card to flip it.
Step 6 of 12Practice · Work it out
A game runs these lines in order: let score = 0; score += 50; score = score * 2; score -= 30;. What is the final value of score?
Step 7 of 12Note
Step 8 of 12Note
Step 9 of 12Practice · True or false
In JavaScript, the line x = x + 1 is a mistake because x can never equal x + 1.
Step 10 of 12Learn
Showing state to the player
Players need to see the state: a score in the corner, hearts for lives, a timer. To build that text, you join strings and numbers with +:
let message = "Score: " + score + " Lives: " + lives;With a score of 70 and 2 lives, message becomes "Score: 70 Lives: 2". Here the joining behavior of + is exactly what you want, because the string on the left turns the whole expression into text.
Modern JavaScript also has template literals, strings written with backticks that can hold values inside ${ }:
let message = `Score: ${score} Lives: ${lives}`;Both lines produce the same text. Template literals are easier to read when there are many values.
While you're building a game, you'll also want to peek at values that players never see. console.log(score) prints a value in the browser's developer console. Professional game developers sprinkle these everywhere while hunting bugs, then remove them before release. If the score jumps by 20 instead of 10, a quick console.log inside the coin-collecting code usually reveals that it's running twice.
Step 11 of 12Practice · Code challenge
Write a function collectCoin(score) that returns the new score after collecting a coin worth 10 points. Then write hudText(score, lives) that returns text in exactly this format: "Score: 70 Lives: 2" (one space after each colon and one space between the two parts).
collectCoin(0)collectCoin(45)hudText(70, 2)hudText(0, 3)
Step 12 of 12Practice · Choose one
A score variable holds the string "20" instead of the number 20. What does score + 5 produce?
Lesson complete!