Create a Space Invaders Game in Corona: Finishing Gameplay

Final product image
What You'll Be Creating

In the previous part of this series, we got the player's ship moving, got the invaders moving, and detected when a player bullet had hit an invader. In this final part of the series, we will get the invaders attacking the player, handle levels, and add the ability for the player to die.

1. Firing Bullets

Every so often one of the invaders fires a bullet. We'll use a timer to accomplish this. Add the following code to gamelevel.lua.

In this function, we first check if the invadersWhoCanFire table has at least one element in it. If that's the case, then we execute the code in the if statement. Otherwise it means the level is over and we invoke the levelComplete function.

There will always be at least one invader who can fire a bullet until you kill the last invader, at which point the invadersWhoCanFire table will be empty.

Inside the if statement, we generate a random number randomIndex depending on how many items are in the invadersWhoCanFire table. We then choose that item, randomInvader, from the invadersWhoCanFire table.

We create a bullet image, give it a name property so we can identify it later, insert it into the scene, and set the same properties as we did on the player's bullet. Finally, we insert the bullet into the invaderBullets table so we can reference it later.

We now need to set up the timer. Add the following to the scene:show method.

Every 1500 milliseconds fireInvaderBullet is invoked. Note that the last parameter we pass in is -1, which means the timer repeats forever. Whenever you create a timer that repeats forever, you should eventually cancel it. We do this in the scene:hide function as shown below.

2. Removing Bullets

Like the player's bullets, the invaders' bullets will move off-screen and keep moving, taking up valuable memory. To remedy this, we remove them just like we did with the player's bullets.

This code is very similar to checking if the player's bullets are out of bounds so I won't discuss its implementation in detail.

3. Detecting a Hit

Step 1: Collision Detection

The next step is to detect whether an invader's bullet has hit the player. Add the following code to the onCollision function.

Like before, we do not know what object event.object1 and event.object2 will be so we use two if statements to check both situations. We remove the invader's bullet from the invaderBullets table, remove it from the display, and set it to nil. If the player isn't invincible, we kill the it.

Step 2: Killing the Player

When we kill the player, we give him a short time of invincibility. This gives the user time to regain focus on the game. If the numberOfLives variable is equal to 0, we know the game is over and transition to the start scene where the user can begin a  new game.

Step 3: Spawning a New Player

The spawnNewPlayer function makes the player fade in and out for a few seconds. It's a nice effect to let the user know that the ship is temporarily invincible.

We use a local function, fadePlayer, that uses the transition library to modify the alpha value of the player. We keep track of how many times the player has faded in and out, and set the player's invincibility to false once we reach the numberOfTimesToFadePlayer. We use a timer to call the fadePlayer function for however many times numberOfTimesToFadePlayer is equal to.

Run the game to test this out. The player should die when an invader's bullet hits the ship. If three bullets hit the ship, you should be taken to the start scene where you can start a new game.

To make this easier to test, comment out the call to moveInvaders in the gameLoop function as shown below.

4. Completing a Level

If you've managed to kill every invader, the game would have called the levelComplete function, which doesn't exist yet. Let fix that. Add the following code block.

We increment gameData.invaderNum and, if it is less than gameData.maxLevels, we transition to the gameover scene. Otherwise, the player has completed every level and we reset gameData.invaderNum to 1. We transition to the start scene where the player can begin a new game.

An easy way to test this is by commenting out the call to moveInvaders in the gameLoop function and use the buttons to move the ship. If that's still too hard, then you can also comment out the two calls to killPlayer in the onCollision method.

5. Game Over

Add the following code to gameover.lua to implement the game over scene.

This code is very similar to the start scene so you should be familiar with it by now.

6. Colliding with an Invader

The last collision check we need to perform is a collision between the player and on of the invaders. Add the following code block to the onCollision method we saw earlier.

As usual, we need to check both collision situations. We set the numberOfLives to 0 and call killPlayer. By setting numberOfLives to 0 and invoking killPlayer, the game is over and the game transitions to the start scene.

7. More Features

This completes our game, but I suggest you try to expand the game with a few additional features. For example, you could display the player's lives in a HUD.

I have also included a UFO graphic in the source files. You could try make a UFO randomly appear and if the player hits it with a bullet give them an extra life.

If you need help with these concepts, then check out my Plane Fighting Game series on Tuts+.


If you've followed this series, then you should now have a fully functional game similar to the original Space Invaders. Expand on it and make it your own. I hope you found this tutorial helpful and have learned some new techniques. Thank you for for reading.



Related Articles