Part 7 – Mothership

In this episode we add the Mother-ship (Mystery-ship) that goes across the top of the screen. Also available as a video:

In this implementation it is random, in the real arcade game it appears approximately every 25 seconds or so. The scoring for it is also random, either 50,100,150 or 300 points. In the real arcade game it appears random but in fact it can be influenced by the players actions.

The Code
Full code that you can just paste in is below.

 

Lines 15-19 define some values for the mystery ship (I’ll call it both mystery and mothership in this article quite freely!).

Lines 15-16 are I hope self explanatory. Line 17 sets the speed that the ship travels across the screen, the higher the faster. It’s basically the number of pixels moved at a time. We then have a value that effects the frequency that the ship appears. The higher the less frequent. Line 19 controls how long the bonus score you got (i.e. 50,100,150,300) stays on screen in the position where the mothership was destroyed. Bigger the number the longer.

Lines 53-58 define the graphics – which we won’t go into as we have covered this in pevious articles.

Line 185 creates a global variable of type AlienStruct to keep track of the ship.

AlienStruct MotherShip;

We also have some other global variables that keep track of various aspects of the mothership, these are at lines 198-202

MotherShipSpeed has the current speed and direction of the mothership and it takes its value directly from MOTHERSHIP_SPEED defined earlier. If its a positive value then it will be move from left to right, if negative then right to left. MotherShipBonus stores the bonus earned if you hit the mothership. This is displayed where the mothership was destroyed. MotherShipBonusCounter is the counter that starts with the value defined by DISPLAY_MOTHERSHIP_BONUS_TIME and it is decremented and on reaching 0 the bonus score is no longer displayed.

 

Mother-ship physics
The physics routine has been expanded to handle the mothership:

It calls MotherShipPhysics(), which is below:

Looking at the code, if the ship is active we handle moving it across the screen and check if it goes off the screen, destroying it (without a score being earned) if it does.

If not active then we randomly decide if a ship should appear, if one should we then set a 50/50 chance as to which direction it should travel (setting its position and speed accordingly).

 

Mother-ship collisions
The check collisions routine at line 333 has also been expanded to call MotherShipCollisions().

MotherShipCollisions()

In this routine if the players missile is active and the mothership is active then we check for a collision between the two. If we get a collision then we choose a random bonus and set the position that this will be shown on screen and set the counter for the duration that its shown on screen.

Displaying the mothership and bonus score
The UpdateDisplay() routine now also displays any bonus earned as long as the bonus display counter is above 0. Lines 448 to 455 handle this:

Lines 502-517 display the actual ship:

If Active we just display it else we check if its currently exploding, if so we draw the explosion graphic at random widths where the ship was for the amount of time that explosions are supposed to appear on screen (discussed in an earlier article).

Lines 541-543 initialise the mothership to ensure sensible values

That’s it for this article, next time we’ll look at adding in the scoring and high score capability.

All for now, see you next time 🙂