Part 9 – Lives,attract, difficulty

We have a lot to get through and in this article we are going to now start moving away from the nitty gritty line be line explanations as hopefully you are becoming more competent in game coding by now. I will outline the changes, present the code and, apart from highlighting any specifics, we will leave it at that. You are free to examine the code to see how the latest instalment was implemented. This should also mean we’ll get through more content from now on with more frequent articles.

Changes in this release

  • You can now die if an Invader’s bomb hits you (but not by any other means yet).
  • Lives will be decremented and if it gets to zero then it’s game over
  • An attract screen
  • Congratulations screen for beating high score
  • Reset of high score
  • Invader and level difficulty, i.e. Invaders move faster the less of them there are and the start lower down per level.

A video covering these changes is available as well as this article, see below:

Highlights – difficulty
As in the arcade game the invaders speed increases as less of them are on screen. Famously this was not an intended game mechanic (i.e. not in any intentional design) but was a by-product of the speed of the available processor at the time. When there were a lot of Invaders to move on screen it took time to plot them to screen. But as their numbers declined the processor didn’t have to plot as many so wasn’t working quite so hard, meaning it could get round to moving and plotting those that were left much sooner than normal – hence they moved faster. This proved an excellent difficulty factor during any one level.

We have very fast processors (compared to the late 70’s early 80’s) and as such we have to deliberately code speeding up.  The code that handles this is in the routine MissileAndAlienCollisions. Its in this routine as this is where an Invader would be destroyed and thus they would be slightly less. Here’s the code extract:

We have a general speed calculation for most of the game (line 584) but then some specific code where we get to the last couple where we increase the speed dramatically.

Level Difficulty
In addition (an intended designed in game mechanic) was that for every level the Invaders would start a little lower down, up until a certain level (perhaps level 4 I cannot remember at time of writing). Then the start height position would revert to the level 1 screen increasing as the levels progressed again until another four levels had passed etc. etc. In this way Space Invaders as no real designed end as such. The code in the NextLevel routine handles this, here’s the extract:

Looks complex perhaps but all it’s doing is setting the Invaders Y start position per level.  The Amount to drop per level is defined in the constant  AMOUNT_TO_DROP_BY_PER_LEVEL.  4 pixels seems about right for this screen. In essence all we need to do is multiply the level number by this amount to get the Y position. However that would make the game technically impossible for higher level numbers as it could result with the Invaders starting on top of the players tank! So ( as it did in the original) we do this on a repeating cycle of levels. i.e. levels 1-4 they come down more and more then they reset back to the start for level 5 getting lower and lower again etc. etc. The  part that accomplishes this is this:

((Player->Level-1) % LEVEL_TO_RESET_TO_START_HEIGHT+1)

The constant LEVEL_TO_RESET_TO_START_HEIGHT contains the level that we reset the Y start position back to the top. In this case I chose level 5. So the Invaders get lower and lower for levels 1 to 4 and start again at the top for level 5 etc. But how is this achieved? Well it’s all in the % (percentage). This is the modulus operator.  It’s purpose is to return the remainder of an integer division. i.e. of I divide 4 by 3, then 3 would go in once and 1 would be left over, so 4 % 3 would give me 1 as the answer.

So in our code we divide the current level by 4 and then multiply this remainder by the amount to move down per level to get the Y Position for the Invaders. Look at this table of examples.

Player LevelPlayer level -1LEVEL_TO_RESET_TO_START_HEIGHTRemainder of :
(Player level-1) % LEVEL_TO_RESET_TO_START_HEIGHT
Multiply remainder by AMOUNT_TO_DROP_BY_PER_LEVEL to get Y Position
10400
21414
32428
434312
54400
65414
76428

Resetting the high score
If you wish to reset the high score it is implemented by pressing both the left and right buttons at once when on the attract screen. If you look back at previous articles and code it was originally intended to have it’s own button but at the last minute I’ve decided to reduce the button count and do it this way instead.

That’s it for this article, all that remains is to list the full source code for you to paste into your current Invaders project,

Enjoy 🙂