A player score records progress in the shoot-em-up by awarding points for destroyed enemies and supporting score changes during play. section 1.1 section 10.0
Implement the score as a small NES game subsystem:
- Reserve three zero-page bytes, storing values from 0 to 99 so each byte represents two score digits. section 10.1
- Use the three bytes for scores from 0 to 9,999,990, adding more bytes when a game needs a higher range. section 10.1
- Reserve an update flag so the NMI routine redraws the score only after it changes. section 10.1
- Reset all three score bytes when a new game starts, before displaying the game screen. section 10.1
Listing 10.1 Adding storage for the player’s score (Megablast.s)
.segment "ZEROPAGE" time: .res 2 lasttime: .res 1 level: .res 1 animate: .res 1 enemydata: .res 10 enemycooldown: .res 1 temp: .res 10 score: .res 3 #1 update: .res 1 #2 #1 Player’s current score #2 Flag so we know when the score has changed
The storage declaration shows the score bytes and the flag that marks pending screen updates. section 10.1
Add points with an 8-bit routine: update the lowest byte, subtract 100 on overflow, carry 1 into the next byte, and repeat. Mark the score changed afterward. section 10.2
Awarding points for a hit
When collision detection confirms that a bullet hit an enemy, erase both objects, clear the enemy data flag, and call add_score with 2, representing 20 points. section 10.2
The third byte also wraps after 99; excess points are ignored so the score returns to zero without display corruption or a crash. The routine assumes no single addition exceeds 99. section 10.2
