Full teaching narration is free with Private Starter.Create free account
Back to curriculum
Computer ScienceGrade 5· U.S. National — Common Core & NGSS
Aligned to:U.S. educational frameworks

Build a Variable-Based Game Score Tracker

Students use variables, arithmetic operators, and update commands to program a score tracker that responds correctly to different game actions.

Build a Variable-Based Game Score Tracker

Illustrations are auto-generated and may be placeholders. They can be refreshed to match the narration.

Full teaching narration is included free with a Private Starter account.Create free account

Explore Variables and Scores

A variable is a named place where a program stores information that can change. In a game, a variable named score can store the player’s current number of points. Imagine that the score begins at 0. When the player collects a star worth 5 points, the program changes the stored score. The expression score + 5 records the calculation the program should make. It means to take the current value of score and add 5. You can interpret this expression without finding a final number because the value of score might be different each time. If score represents 12, then score + 5 represents 12 + 5. Variables help one set of instructions work throughout the game, even as the player earns or loses points.

A labeled variable box named score changes from 0 to 5 after a player collects a five-point star.
A labeled variable box named score changes from 0 to 5 after a player collects a five-point star.Source: Illustrated for this lesson

Create and Name a Score Variable

Before tracking points, create a variable and give it a clear name. The name score immediately tells readers what the variable stores. A name such as s could work, but it is harder to understand. Set score to 0 when the game starts so every player begins with the same number of points. This command can be written as set score to 0. The command assigns 0 to the variable; it is not an equation to solve. Use one consistent name everywhere. Score, playerScore, and points might be treated as different variables by a programming tool. For example, if you create playerScore but later update score, the displayed number may not change. A clear name and a starting value make the tracker easier to program, test, and explain.

A game setup screen shows score as the clear variable name, s as an unclear choice, and the starting command beside two differently named storage boxes.
A game setup screen shows score as the clear variable name, s as an unclear choice, and the starting command beside two differently named storage boxes.Source: Illustrated for this lesson

Program Score Updates

A score update uses the variable’s current value to create a new value. Suppose collecting a coin is worth 3 points. The update command set score to score + 3 means, “Take the current score, add 3, and store the result back in score.” If the player is hit by an obstacle, the command might be set score to score - 2. The expressions score + 3 and score - 2 record two different calculations without requiring one fixed answer. The answer depends on the current score. Order matters when an update includes more than one operation. For example, score + 2 × 5 adds the value of 2 × 5 to the score because multiplication is performed before addition. Parentheses can show a different plan, such as (score + 2) × 5.

A game update flow shows a coin adding points, an obstacle subtracting points, and two operation-order examples with different results.
A game update flow shows a coin adding points, an obstacle subtracting points, and two operation-order examples with different results.Source: Illustrated for this lesson

Test Different Game Actions

Testing checks whether every game action changes the score as planned. Make a table with an action, a score before the action, an update expression, and a score after the action. For example, begin with a score of 10. If a player collects a gem worth 4 points, the expression is score + 4, so the tracker should display 14. If the next action is a penalty worth 3 points, the expression is score - 3, and the tracker should then display 11. Test actions in different orders because each update uses the score produced by the earlier action. Also test unusual cases, such as missing a target, which might use score + 0. Compare the displayed result with your predicted result after every action. A mismatch shows that the program needs closer inspection.

A four-column test table follows a gem, a penalty, and a missed target from each starting score to its predicted ending score.
A four-column test table follows a gem, a penalty, and a missed target from each starting score to its predicted ending score.Source: Illustrated for this lesson

Debug and Explain the Tracker

Debugging means finding and fixing problems in a program. If collecting a 5-point star changes the score from 8 to 5, inspect the update command. The command may say set score to 5, which replaces the old value instead of adding to it. Change it to set score to score + 5. Also check for a wrong operator, an incorrect number, a mismatched variable name, or an event connected to the wrong action. After making one change, run the same test again. Explain the corrected tracker by describing the expression rather than only giving its result. For example, score + 5 means that 5 is added to the current value stored in score. This explanation shows why the command works for any starting score, not just for a starting score of 8.

A debugging comparison shows the faulty command replacing 8 with 5 and the corrected command adding 5 to produce 13.
A debugging comparison shows the faulty command replacing 8 with 5 and the corrected command adding 5 to produce 13.Source: Illustrated for this lesson