Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Levels and difficulty

After this lesson: I can describe a level as data and work out its spawn schedule.

60 min lesson1 commandGame development lab: a game loop, gravity and collisions in the browser

A level is a table of numbers

A level is a set of numbers that tells a game how hard to be. It says how fast things fall and how often a new one appears. A spawn schedule is the rule for when each new thing appears.

When levels are data, a designer changes a table and plays again, with no new code to write or break.

By the end of this lesson

Note

I can describe a level as data and work out its spawn schedule.

Key words

WordWhat it meansExample
Levela named set of numbers that sets the difficulty
Spawnto add a new thing to the game
Spawn schedulethe rule for when new things appear
Scalea number that multiplies a base value1.4
Difficulty curvehow the challenge grows as the player goes on

The cells students fill in are left blank in this preview.

A table of levels holds every number

The lab's game keeps its levels in a list, one row for each. A row has a name, the score at which it starts, and two scales. One scales the gravity. The other scales how often stars appear.

The game finds the current level by looking down the list for the last row whose start the score has reached.

The lab game's table of levels

Code: web
var LEVELS = [
  { name: "Drizzle", from: 0, gravityScale: 1.0, spawnScale: 1.0 },
  { name: "Shower", from: 5, gravityScale: 1.4, spawnScale: 1.25 },
  { name: "Storm", from: 12, gravityScale: 1.8, spawnScale: 1.6 },
  { name: "Meteors", from: 20, gravityScale: 2.4, spawnScale: 2.0 }
];

// A score of 7 has reached 0 and 5, but not 12, so the level is Shower.

Work out each level's gravity and gap

Command
docker exec jupyter python3 steps/levels.py

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The script turns the scales into real numbers

The script reads the table and the base numbers, GRAVITY of 0.15 and SPAWN_EVERY of 50. It prints four lines, one for each level.

Drizzle has gravity 0.150 and a star every 50 frames. Shower has 0.210 and every 40. Storm has 0.270 and every 31. Meteors has 0.360 and every 25. The gap is 50 divided by the scale, rounded to a whole frame.

A spawn schedule is a remainder test

The game counts frames. A star spawns on a frame when the frame number divides exactly by the gap. In Python and in JavaScript, the remainder of a division is written %, so the test is frame % gap == 0.

With a gap of 40, stars spawn on frames 40, 80, 120 and so on. When the level changes, the gap changes, and the next test uses the new gap.

Worked example: whole stars in ten seconds

LevelGap (frames)Frames in 10 secondsWhole stars
Drizzle5060012
Shower4060015
Storm3160019
Meteors2560024

The cells students fill in are left blank in this preview.

A difficulty curve rises in steps

The table assumes a screen that draws 60 frames a second. It shows the stars doubling from 12 to 24 across four levels, while gravity rises from 0.15 to 0.36.

A good curve rises in steps a player can meet. If one level doubled everything at once, players would stop there and not know why.

A common mistake

Warning

Many people think a harder level needs new code. It needs new numbers. If the table holds the numbers and one function reads it, you can add a fifth level by adding a fifth row. Nothing else changes.

About this lab and its source: Game development lab: a game loop, gravity and collisions in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.