5.6 Arrays that Drive a Game
Using an Array to Drive a Greenfoot Game
Unit 5: Arrays · Lesson 5.6 · about 30 minutes · no coding experience needed
This is where arrays stop being an exercise. Put your level in data and you can design ten levels without writing another line of code, which is exactly how real games work and exactly what Unit 6 does in two dimensions.
What you will be able to do
- Drive actor placement from an array
- Read a per-level value from a schedule array
- Explain why data-driven beats hardcoded
Words you will need
- Data driven
- In plain words The numbers decide what happens, not the code.
- More precisely Behavior determined by data rather than control flow.
Build it step by step
Every step below leaves a scenario that compiles and runs. If you stop halfway you will have something that works, not something broken. Follow along in Greenfoot rather than reading straight through.
-
Step 1
On your screen A row of walls built from an array of heights.
int[] heights = {2, 5, 1, 4, 3}; for (int i = 0; i < heights.length; i++) { addObject(new Wall(), i, heights[i]); }A skyline, from data.
The INDEX becomes the x position and the VALUE becomes the y position. Two different jobs from one array, and it is worth pausing on because it is the exact idea Unit 6 extends.
Change a number and the level changes. The loop never changes.
-
Step 2
On your screen Actors placed into fixed lanes read from an array.
int[] laneY = {2, 6, 10}; int lane = Greenfoot.getRandomNumber(laneY.length); addObject(new Car(), 0, laneY[lane]);Lane positions in an array, and a random lane chosen by index.
`getRandomNumber(laneY.length)` is exactly right, and this is the payoff for 2.6: the exclusive upper bound produces 0 to length-1, which is precisely the valid index range. The two rules fit together with nothing to adjust.
Add a fourth lane and this code needs no change at all.
-
Step 3
On your screen A difficulty schedule array indexed by the current level.
private int[] enemiesPerLevel = {3, 5, 8, 12, 20}; private int level = 0; public void startLevel() { int howMany = enemiesPerLevel[level]; for (int i = 0; i < howMany; i++) { addObject(new Enemy(2), i * 2, 0); } }A difficulty curve as data. Level 0 gets 3 enemies, level 4 gets 20, and tuning the game means editing five numbers.
One caution: when `level` reaches 5 this throws, because the last valid index is 4. Either stop the game at the last level or clamp the index. That is a real design decision, not an oversight, and boundaries like it are where most real bugs live.
Mistakes almost everyone makes here
These are the wrong ideas students actually build at this point. Read them even if you think you understand, because a wrong idea you have not noticed is the expensive kind.
Common wrong idea Data-driven design is an advanced technique.
What is actually true It is an array and a loop. You already have both.
Why it matters Students hand-place fifty actors because this looks harder than it is.
Common wrong idea The index and the value are the same kind of thing.
What is actually true The index is usually WHERE, the value is usually WHAT.
Why it matters Separating these is the whole idea, and Unit 6 depends on it completely.
Common wrong idea A level array can be indexed by the level number safely forever.
What is actually true Going past the last level throws. Decide what happens at the end.
Why it matters A game that crashes on victory is a bad look, and it is a real boundary case.
Check your understanding
Answer these before moving on. They are graded instantly and you can retry.
-
1. In `addObject(new Wall(), i, heights[i])`, what does the index i provide?
-
2. Why is `getRandomNumber(laneY.length)` exactly right for choosing a lane?
-
3. You want to add a sixth level with 30 enemies. What do you change?
-
4. enemiesPerLevel has 5 entries and level reaches 5. What happens?
Fill in the code
Build a skyline where the array index is the column and the value is the height of the wall in that column.
int[] heights = {2, 5, 1, 4};
for (int i = 0; i < heights.; i++)
{
addObject(new Wall(), , heights[]);
}
Stuck? Open a hint for each blank
- Blank 1: How many columns there are. No parentheses on an array.
- Blank 2: The column position is the loop counter itself.
- Blank 3: Look up the height for this column.
Lesson quiz
This one counts toward your progress. Take it when the section above makes sense.
-
1. Putting level data in an array means:
-
2. In a skyline built from `{2, 5, 1}`, where does the wall of height 5 go?
-
3. A difficulty array is indexed by level. What must you decide?
-
4. The index of an array usually represents:
The short version
- The INDEX is usually where; the VALUE is usually what.
- A loop over level data means new levels are numbers, not code.
- getRandomNumber(a.length) picks a valid index exactly, with no adjustment.
- Decide what happens when a level index runs past the end.
If you get stuck
These pages cover the problems that come up most often in this lesson. Opening one is not cheating and it is not counted against you.
Get in Touch
Whether you're a student, parent, or teacher — I'd love to hear from you.
Just want free AP CS resources?
Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]