Skip to content
Computing · Year 5 · Variables in Games · Spring Term

Changing Variables

Programming triggers that increase or decrease variable values based on user input or sprite collisions.

National Curriculum Attainment TargetsKS2: Computing - Programming and Algorithms

About This Topic

Once students understand what variables are, they must learn how to manipulate them through interaction. This topic covers the logic of changing a variable's value based on events, such as a player clicking an item or a sprite hitting an obstacle. This is a vital part of the UK National Curriculum's programming requirements, focusing on sequence, selection, and repetition.

In Year 5, students move from static values to dynamic ones. They learn the difference between 'setting' a variable (giving it a specific value) and 'changing' it (adding or subtracting from the current value). This distinction is crucial for creating functional game mechanics like scoring systems or health bars.

This topic is best taught through collaborative investigations where students 'remix' existing code to see how different mathematical operators change the feel of a game.

Key Questions

  1. Explain how mathematical operators are used to change a score variable.
  2. Differentiate between changing a variable and setting a variable.
  3. Design a penalty system that reduces a player's health in a game.

Learning Objectives

  • Design a game mechanic that modifies a score variable using addition and subtraction operators.
  • Compare the outcomes of setting a variable to a specific value versus changing its current value.
  • Analyze how sprite collision events can trigger changes in a health variable.
  • Create a simple game sequence where user input directly alters a variable's value.

Before You Start

Introduction to Variables

Why: Students must first understand what a variable is and how to declare and assign it a value before they can learn to change it.

Basic Arithmetic Operators

Why: Familiarity with addition and subtraction is necessary to understand how variable values are modified.

Key Vocabulary

VariableA container in a program that holds a value which can change during the program's execution, like a score or a player's health.
Set VariableAssigning a specific, fixed value to a variable, for example, setting the score to 0 at the start of a game.
Change VariableModifying the current value of a variable by adding or subtracting from it, such as increasing the score by 10 points.
OperatorA symbol or function that performs an operation on one or more values, like '+' for addition or '-' for subtraction.
CollisionAn event that occurs when two sprites or objects in a game touch or overlap, often triggering an action.

Watch Out for These Misconceptions

Common MisconceptionChanging a variable by -1 is the same as setting it to -1.

What to Teach Instead

Explain that 'change' is relative to the current number, while 'set' is an absolute value. Using a physical number line or a counter helps students see that 'change by -1' means 'take one away' from whatever is already there.

Common MisconceptionVariables update themselves automatically.

What to Teach Instead

Students often forget that they must write a specific line of code to trigger the change. Peer-checking of scripts helps students identify 'missing links' where an action happens on screen but the score doesn't move.

Active Learning Ideas

See all activities

Real-World Connections

  • Video game developers use variable manipulation extensively to create dynamic gameplay. For instance, in 'Minecraft', variables track a player's health, hunger, and inventory items, changing based on player actions and game events.
  • App designers for fitness trackers, such as Fitbit, use variables to record steps taken, calories burned, and heart rate. These variables are constantly updated based on sensor data and user activity.

Assessment Ideas

Quick Check

Present students with short code snippets. Ask them to identify whether the code is 'setting' or 'changing' a variable and to predict the final value of the variable after execution.

Exit Ticket

Give each student a card with a game scenario (e.g., 'Player collects a coin', 'Player hits an enemy'). Ask them to write one code-like instruction to change a relevant variable (e.g., 'change score by 10' or 'change health by -5').

Discussion Prompt

Pose the question: 'Imagine a game where you lose points for missing a target. How would you use variables and operators to design this penalty system? What happens if you accidentally set the score to 0 instead of changing it?'

Frequently Asked Questions

What is a mathematical operator in coding?
Operators are the symbols we use to change numbers, like + (add), - (subtract), * (multiply), and / (divide). In programming, we use these to update variables. For example, 'change score by 10' uses the addition operator to increase the value.
Why does my score keep going up forever?
This usually happens if the 'change variable' block is placed inside a loop without a proper trigger or 'wait' command. The computer executes code very fast, so it might add to the score hundreds of times a second. You need to tell it exactly when to stop or wait.
How can active learning help students debug variable changes?
Active learning strategies like 'Peer Code Review' or 'Live Debugging' encourage students to explain their logic out loud. When a student has to walk a peer through their code step-by-step, they often spot the moment where they used 'set' instead of 'change' or forgot a trigger. This collaborative approach builds confidence and reduces the frustration of solo debugging.
Can I use variables to change the speed of a sprite?
Yes! Instead of typing a number like '5' into a move block, you can use a variable called 'speed'. This allows you to change the speed of the sprite during the game, for example, making it go faster as the player gets more points.