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.
