6 Starting a game
This chapter kicks off development of a simple NES shooter, Mega Blast, chosen for its clear mechanics and suitability for teaching fundamentals like movement, animation, collision, and later sound. It walks through setting up a clean project skeleton you can build and test incrementally: a memory configuration, compile/link scripts for Windows, Linux, and macOS, and the core source assets (main program, a reusable library, and a character set). The immediate goal is to get a minimal, working ROM that boots, initializes hardware safely, and renders background graphics—first a title screen, then a basic in-game layout—so you can iterate quickly in an emulator.
To make the code maintainable and reusable, the chapter separates common routines into a shared library (neslib.s) and augments it with macros (macros.s) that inline frequent VRAM setup patterns without the subroutine overhead. The library defines PPU/APU/control constants and zero-page state, and implements essential routines: waiting for vBlank, toggling rendering safely, clearing the nametable, polling the controller, and writing zero-terminated text to VRAM. The console is initialized with a proper iNES header, vectors, OAM buffer, and a default palette; the reset routine clears RAM/OAM and waits for vBlank, while the NMI uploads sprite OAM via DMA, copies the current palette to $3F00, and commits control-register updates. The main routine seeds the palette, sets background/sprite pattern tables and rendering flags, and drives the game loop.
Background rendering is explained in practical terms: the NES displays 32×30 tiles of 8×8 pixels via nametables, with 2-bit tile patterns and per-attribute 2×2-tile palette selection; NTSC/PAL visibility and refresh differences are noted. The chapter adopts an ASCII-aligned tile set (A–Z, 0–9) plus a few custom tiles, simplifying text rendering. Using ppu_off/ppu_update and VRAM macros, the title screen is built by clearing the nametable, writing two text blocks, and setting attribute bytes so the title uses a different palette than the prompt. Input is handled by gamepad_poll; on button press the code switches to an in-game screen that draws a mountain row, a baseline, and a score label by streaming bytes to VRAM. The result is a complete end-to-end path: boot, initialize, draw a title, wait for input, and display the first game scene—ready for future gameplay logic.
Astro Smash on the Intellivision
NES Tile Layout (Top and bottom rows not visible on NTSC consoles – depending on type of display)
The four different name table layouts.
Visible screen area with scroll positions set.
How the two pattern table bytes combine.
An attribute value defines the palette for four 2x2 tile areas i.e., 8x8 tiles.
NES Color Palette values
Upper case alphabet patterns in our tile table.
Numeric patterns in our tile table
Example ROM running on an NES emulator.
Adding some more tile patterns for our game background.
Main game screen
Summary
- The NES can display 13 different colors from a palette of 52 for background tiles on screen at one time.
- NES Sprites have their own color palettes allowing another 12 colors for a total of 25 colors on screen at one time.
- The NES can display background graphics made up of 32x24 tiles or a resolution of 256x224 pixels.
- NTSC NES consoles refresh the screen 60 times per second, but the top and bottom 8 pixels are not shown on the screen.
- PAL NES consoles refresh the screen 50 times per second, which can change the timing of your game.
- As well as defining functions that can be called from your code, you can also create macros that will repeat common sections of code saving typing and improving readability.
- You can use the same tile patterns in different places on the screen but point them to a different set of palette entries using the attribute table which will make them use a different set of colors.
- Tile layouts can be set based on data ending in a terminating byte e.g., 0, a fixed length of tile number values or by repeating a tile multiple times.
Classic Game Programming on the NES ebook for free