Week 10 · Unit 4: Variables in programming · 3 lessons
3
Energy and batteries
Computing 6: Unit 4, pp. 52–67
GoalFollow and trace a small game program that changes two variables and ends when a condition becomes true.
New words
game over · the moment when a game ends because a condition is truestop all · a Scratch block that stops every scripttouching · a sensing block that tells whether two sprites meettrack · to keep a record of how a value changes
Explanation
Many games use variables to keep track of what happens. In this lesson a robot has two variables, energy and batteries. At the start, energy is 20 and batteries is 0. The player moves the robot with the arrow keys in another script. A forever loop keeps checking what the robot touches. If it touches the wall, energy goes down by 4 and the robot steps back, so one bump counts once. If it touches a battery, energy goes up by 6, batteries goes up by 1 and the message collected tells the battery to hide. A third if block checks whether the energy is less than 1. If it is, the robot says Out of power and the stop all block ends the game. Because this check is inside the forever loop, it happens again and again.
Worked examples
Event
energy
batteries
start
20
0
wall
16
0
wall
12
0
battery
18
1
wall
14
1
wall
10
1
wall
6
1
wall
2
1
wall: Out of power
−2
1
This trace table shows what happens during one game. The robot bumps into the wall, touches a battery and then bumps into the wall again and again. The game ends after the fifth wall bump after the battery, because the energy falls below 1.
Why does the last row show −2 and not 0? The wall always takes away 4, and 2 − 4 = −2. The check energy < 1 is true for −2, so the game ends. If the check were energy = 0, the program would miss this case and carry on.
The player’s final score is the number of batteries, 1. Variables let a game keep several facts at once: how much energy is left, how many batteries were collected and whether the game is over.
Class activity
Paper game (no computer): draw a board of 20 squares in a row, with walls on some squares and batteries on others. Use a counter as the robot. Keep two number cards, energy and batteries, with energy starting at 20. Roll a die and move that many squares. If you land on a wall square, change energy by −4. If you land on a battery square, change energy by 6 and batteries by 1. Check after every move whether energy is less than 1. Play until the energy runs out or you reach the end of the board. How many batteries did you collect? If you have a computer or tablet, build a simple version in Scratch.
Practice
1
The robot starts with 20 energy. It touches these things in this order. What are energy and batteries at the end?
Order
The robot touches
1
a battery
2
the wall
3
the wall
Battery: energy 26, batteries 1. Wall: energy 22. Wall: energy 18. So energy is 18 and batteries is 1.
2
The robot has 4 energy and bumps the wall. What happens?
The energy becomes 4 − 4 = 0. The condition (energy) < (1) is true, so the robot says Out of power and the game stops.
3
Why is the check energy < 1 placed inside the forever loop?
So that it is checked again after every event. If it were outside the loop, it would be checked only once, at the start, and the game would never end.
4
Plan a game about collecting stars. Name three variables with their data types and say what starts the game and what ends it.
For example: stars (integer), timeLeft (integer) and playerName (string). The game starts when the flag is clicked and ends when timeLeft reaches 0.