AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Variables in games

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.

Startwhenclickedsetenergyto20setbatteriesto0broadcastgoRobotwhen I receivegochangeenergyby-4move-30stepsiftouchingwall?thenchangeenergyby6changebatteriesby1broadcastcollectediftouchingbattery?thensayOut of powerfor2secondsstopallifenergy<1thenforever
Worked examples
Eventenergybatteries
start200
wall160
wall120
battery181
wall141
wall101
wall61
wall21
wall: Out of power−21
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?
OrderThe robot touches
1a battery
2the wall
3the wall
2
The robot has 4 energy and bumps the wall. What happens?
sayOut of powerfor2secondsstopallifenergy<1then
3
Why is the check energy < 1 placed inside the forever loop?
4
Plan a game about collecting stars. Name three variables with their data types and say what starts the game and what ends it.