Computer Science
Grade 3
20 min
Using Variables in Games
Using Variables in Games
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define a variable as a container for information.
Explain the difference between a variable's name and its value.
Create a new variable and give it a starting value.
Change the value of an existing variable.
Use a variable to keep track of a score in a simple game scenario.
Identify variables used for tracking lives, points, or items in a game.
Have you ever collected coins in a game? 🪙 How does the computer remember how many you have? Let's find out!
We are going to learn about special memory spots called 'variables'. Think of them as magic boxes that help our games remember important things like your score, how many lives you have left, or your player's name. Knowing how to use these boxes is the first step to making awesome g...
2
Key Concepts & Vocabulary
TermDefinitionExample
VariableA container or a box in the computer's memory that holds a piece of information. We give it a name so we can find it later.A variable named `score` can hold the number of points you have.
ValueThe actual information that is stored inside a variable. The value can change!If your score is 10, the variable `score` has a value of `10`.
DeclareTo create a new, empty variable for the first time and give it a name.We can declare a variable called `player_lives` to get it ready to hold a number.
AssignTo put a value into a variable. We use the equals sign `=` to do this.`score = 0`. This assigns the value `0` to the variable `score`.
Data TypeThe kind of information a variable can hold. The two most common types are numbers and words (strings).The variable `scor...
3
Core Syntax & Patterns
Creating a Variable
variable_name = value
Use this pattern to create a variable and give it a starting value all at once. Pick a name that describes what the variable is for.
Changing a Variable's Value
variable_name = new_value
To update what's inside your variable box, you just assign a new value to the same variable name. The old value is replaced.
Using a Variable's Value
Look at the variable_name to see the value inside.
When you use the variable's name in your code, the computer looks inside the box and uses the value it finds there.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
You want to give a player a "Super Speed" power-up that lasts for exactly 10 seconds. To make this work, you would need a variable to track how much time is left. What OTHER variable would you need?
A.`score` variable to give the player points for using the power-up
B.`playerHealth` variable to make sure the player is alive
C.variable like `isSuperSpeedActive` (true/false) to tell the game if the power-up is currently working
D.`playerName` variable to know who got the power-up
Challenging
A game has a rule: "WHEN `score` reaches 100, THEN increase `level` by 1 AND reset `score` to 0". If a player's `score` is 95 and they collect a 10-point coin, what will the new values of the `level` and `score` variables be right after? (Assume `level` started at 1).
A.`level` is 1, `score` is 105
B.`level` is 2, `score` is 105
C.`level` is 2, `score` is 5
D.`level` is 2, `score` is 0
Challenging
Imagine trying to make a game like "Space Invaders" WITHOUT using a variable for the player's score. What would be the biggest problem?
A.The player's ship would not be able to move
B.The game would have no sound
C.The game could not remember or update the score when an alien is hit
D.The aliens would not be able to shoot
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free