Space Invaders - character maps

Posted by Peter Ibbotson on December 22, 2020 · 7 mins read
Categories: Software , Invaders
Tags: Invaders

Space Invaders - character usage

While I’ve been working on space invaders I thought I should write some notes on how the characters work on screen. A lot of the objects on the space invaders screen typically move on 8 pixel steps vertically and a 2 step horizontal. This is a table of all of the sprites within the game and their offsets.

Object Type Vertical Horizontal
    Step Step
Text Character 8 8
Shield Bitmap 8 2,5,7,8
Saucer Character 8 2,4,6,8
Saucer explosion Character 8 2,4,6,8
Player Character 8 1
Player explosion 1 Character 8 1
Player explosion 2 Character 8 1
Invader explosion Character 8 2,4,6,8
Invader A1 Character 8 4,8
Invader A2 Character 8 2,6
Invader B1 Character 8 4,8
Invader B2 Character 8 2,6
Invader C1 Character 8 4,8
Invader C2 Character 8 2,6
Players Shot Sprite 4 1
Players Shot explosion x3 Sprite 1 1
Invaders Shot Sprite 4,5 1
Invaders Shot explosion x3 Sprite 4,5 1

The character set in the end looks like this (I put far too much effort in creating this picture): Character Map
Character Map

When the last alien is on screen it moves with a right to left step of 3 pixels. In this case we use three of spare bitmap characters and rotate the image putting the results into the bitmap to match when we draw it. We also reuse the same characters when that alien explodes.

Update 24-Dec-2020

Part way through getting the invaders explosions correct I realised the character map was hurting me and I decided to redesign it. The alien explosion code is a nasty mess of special cases mostly to do with when things are shifted by four pixels. I needed some extra space so got rid of any unused characters (and made it easier to display hexadecimal) even then I had to steal some extra characters in the alien row at 0x26,0x28,0x2b and 0x2c and my space character is now 0x23. The little block of spaces at 0x87,0x97 and 0xa7 are used by the explosion logic so aren’t really free characters (although if I made the code even more complicated they could be).

Update 27-Dec-2020

I realised that U in BUTTON was missing so had to add it back into the character map. I fixed this by moving the ⅄ (upside down Y) to be at position 0xf7 and inserting the U next to V.

Invaders going in both directions
Invaders going in both directions

The invaders alternate their design as they march left and right meaning that type 1 invaders appear offset by 0 or 4 pixels and type 2 invaders appear offset by 2 or 6 pixels. What makes this slightly awkward is that the gaps between them as they move are either 2 or 6 pixels depending on the direction of travel as the image above shows you need at least 30 characters to represent the aliens in all their positions and once a single alien is left the positions can be any offset. As mentioned above once this happens we use the 0 position of the type 1 & 2 alien in the character map and put it directly into a bitmapped character shifted correctly. Quite a lot of work has taken place taking screen shots in Mame and overlaying a grid on top to figure out exactly how the aliens move. The image above shows how this works with the lower nybble of the character being shown next to images.

An interesting side effect of the original space invader hardware is as an invader is drawn it also passes through the hardware shift register (which would allow to appear on a non multiple of 8 vertical offset on screen). Although the invader is always on a multiple of 8 vertically this does mean the 8 pixels above the invader are cleared as a side effect of drawing it in it’s new position. With the character map approach there is some fiddly logic to replicate this in the code as aliens that had a quarter of the image stolen from the alien to their left or right now no longer have a neighbour and this needs fixing up.

Invaders exploding
Invaders exploding with different shifts

Similar issues occur with the alien explosion images and again certain offsets need to borrow pixels from their neighbours as shown in the images above. To help with the getting the shifts correct there are some diagnostics pages that stop the game and display various images with a correct shift. The grid lines are added afterwards in paint.net by layering a transparent image on top of a screen shot.

I may get rid of this approach and use a bit map characters for an exploding alien as there is only one on screen and the bitmap characters could copy from the alien character to either side then merge in an explosion.