liveBook · Manning

Counter

As explained in Classic Game Programming on the NES: Make your own retro video game

A counter is a stored value that is repeatedly updated to track occurrences or elapsed steps, including remaining player lives.

A counter is a stored value that is repeatedly updated to track occurrences or elapsed steps. In an NES game, a counter can represent time ticks or the player’s remaining lives. section 7.3 section 11.1

Two practical counter patterns are used:

  • A time counter increases whenever the NMI routine runs at vBlank. The main game loop compares it with a saved previous value and proceeds only after the counter changes. section 7.3
  • A lives counter starts at 5 and decreases when collision detection confirms that an enemy has hit the player. A separate flag tracks the player’s death sequence. section 11.1
  • A counter can grow beyond one byte. A 16-bit time counter increments its lower byte and increments the upper byte when the lower byte wraps from 255 to zero. section 8.1

Timing a game loop

Declare time and lasttime in zero-page memory, increment time from the NMI routine, then compare time with lasttime in the main loop. When they differ, copy time into lasttime and perform the next update.

How Classic Game Programming on the NES: Make your own retro video game covers it

How deep it goes
Explains declarations, increments, comparisons, decrements, and multi-byte rollover, without detailing broader counter abstractions.
Examples
Uses elapsed vBlank ticks, player lives, enemy collisions, and a 16-bit counter that supplies randomness seeds.
What it assumes
Assumes basic NES game-loop and interrupt context while teaching counter storage, update, comparison, decrement, and rollover.
What you can do after
Apply counters via NES assembly to pace updates, count lives, and represent values larger than one byte.
How it connects
Links counters with vBlank interrupts, NMI routines, main game loops, collision detection, and random seeds.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Classic Game Programming on the NES: Make your own retro video game. The section links point to the parts of the book it draws on.