Corona SDK: Create a Shooter Game

In this tutorial, I'll show you how to create a shooter game with limited bullets with the Corona SDK. The objective of the game is to shoot a high amount of targets with only five bullets. During this tutorial, you'll work with timers, touch controls, and physics. To learn more, read on!

1. Application Overview

App Overview

Using supplied graphics we will code a shooting game using Lua and the Corona SDK API's. In the game the player uses five bullets to shoot at his enemies. Each enemy will then create another four bullets that help take down a larger number of targets. While coding, you can modify the parameters in the code to customize your game.


2. Target Device

Target Device

Our first step is to select the platform we want to run our app in. This is important so that we can choose the size for our images.

The iOS platform has the following characteristics:

  • iPad 1/2/Mini: 1024x768px, 132 ppi
  • iPad Retina: 2048x1536, 264 ppi
  • iPhone/iPod Touch: 320x480px, 163 ppi
  • iPhone/iPod Retina: 960x640px, 326 ppi
  • iPhone 5/iPod Touch: 1136x640, 326 ppi

Because Android is an open platform, there are several different devices and resolutions. A few of the more common screen characteristics are:

  • Asus Nexus 7 Tablet: 800x1280px, 216 ppi
  • Motorola Droid X: 854x480px, 228 ppi
  • Samsung Galaxy SIII: 720x1280px, 306 ppi

In this tutorial we'll focus on the iOS platform with the graphic design, specifically to develop for distribution to an iPhone/iPod touch, but the code presented here applies to Android development with the Corona SDK as well.


3. Interface

Interface

We'll use a simple interface with multiple shapes, buttons, and bitmaps. The interface graphic resources necessary for this tutorial can be found in the attached download.


4. Export Graphics

Export Graphics

Depending on the device you selected, you may need to export the graphics in the recommended ppi. You can do that in your favorite image editor. I used the Adjust Size... function in the Preview app on Mac OS X.

Remember to give the images a descriptive name and save them in your project folder.


5. App Configuration

An external file makes the application become full-screen across devices (the config.lua file). This file shows the original screen size and the method used to scale that content in case the app is run in a different screen resolution.


6. Main.lua

Now let's write the application! Open your preferred Lua editor (any Text Editor will work, but you won't have syntax highlighting) and prepare to write your new app. Remember to save the file as main.lua in your project folder.


7. Code Structure

We'll structure our code as if it were a Class. If you know ActionScript or Java, you'll find the structure familiar.


8. Hide Status Bar

This code hides the status bar. The status bar is the bar on top of the device screen that shows the time, signal, and other indicators.


9. Import Physics

We'll use the Physics library to handle collisions. Use this code to import it:


10. Background

Background

The next line of code creates a simple background for the application interface.


11. Title View

Title View

This is the Title View; it will be the first interactive screen to appear in our game. These variables store its components:


12. Credits View

Credits View

This view shows the credits and copyright of the game. This variable stores it:


13. Instructions Message

Instructions

An instructions message will appear at the start of the game and tween out after two seconds. You can change the time later in the code.


14. Turret

Turret

This is the turret graphic, it will be placed at the center of our game.


15. Enemy

Enemy

Enemies appear from the edge of the screen, the next Group stores them.


16. Alert

Alert

This is the alert that's displayed when the player runs out of bullets. It shows the score and ends the game.


17. Sounds

Sounds

We'll use sound effects to enhance the feeling of the game. The sounds used in this game were created in as3sfx and the background music is from PlayOnLoop.


18. Variables

These are the variables we'll use. You can read the comments in the code to learn more about them.


19. Declare Functions

Declare all functions as local at the start.


20. Constructor

Next we'll create the function that initializes the game logic:


21. Add Title View

Now we'll place the TitleView in the stage and call a function that adds the tap listeners to the buttons.


22. Start Button Listeners

This function adds the necessary listeners to the TitleView buttons.


23. Show Credits

The credits screen is shown when the user taps the "About" button. A tap listener is added to the credits view to remove it.


24. Hide Credits

When the credits screen is tapped, it'll be tweened out of the stage and removed.


25. Show Game View

When the Play button gets tapped the Title View is tweened and removed, revealing the Game View. There are many parts involved in this view so we'll split them into the next few steps.


26. Instructions Message

The following lines add the instructions message.


27. Bullets Left Indicator

This section adds the bullets at the top-left of the screen. It represents the available shots the player has left.


28. Score TextField

This is the Score TextField created at the top-right of the stage:


29. Turret

Now we'll place the turret in the stage.


30. Enemies Table & Background Music

Next we'll create the enemies table, call a function that adds the game listeners, and start the background music.


31. Game Listeners

This function adds the necessary listeners to start the game logic:


32. Create Enemy

The following function creates the enemies. It starts by selecting a random Y position from the previously created table, then adds physics to the newly created object. We'll add a collision listener to every enemy and also add them to the enemies table.


33. Shoot

When the player taps the screen a bullet is created and a sound plays. It has physics properties that detect collisions.


34. Update Bullets Left

Remove a bullet from the "Bullets Left" area at the top-left of the stage.


35. Move Enemies

The following function runs every frame. Here we use it to move every enemy in the enemies table.


36. Move Shoot Bullets

A similar method is used for the bullets.


37. Move Explosion Bullets

When a bullet hits an enemy, four additional bullets form that move in four different ways. This code moves every bullet in the correct direction.


38. Collisions

This function runs when the bullet collides with an enemy. It plays a sound, calls the function that adds the four additional bullets, increases the score, and removes the objects implicated in the collision.


39. Add Explosion Bullets

This code creates and places the four additional bullets in the correct position to be moved by the update function.


40. Alert

The alert function creates an alert view, animates it, and ends the game.


41. Call Main Function

In order to begin the game, the Main function needs to be called. With the above code in place, we'll do that here:


42. Loading Screen

Loading Screen

The Default.png file is an image that is displayed right when you start the application while the iOS loads the basic data to show the Main Screen. Add this image to your project source folder, it will automatically be added by the Corona compliler.


43. Icon

Icon

Using the graphics you created before, you can now create a nice-looking icon. The icon size for the non-retina iPhone icon is 57x57px, but the retina version is 114x114px and the iTunes store requires a 512x512px version. I suggest creating the 512x512 version first and then scaling down for the other sizes.

It doesn't need to have rounded corners or transparent glare, iTunes and iPhone does that for you.


44. Testing in Simulator

Testing

It's time for the final test! Open the Corona Simulator, browse to your project folder, and click "Open". If everything works as expected, you are ready for the last step!


45. Build

Build

In the Corona Simulator go to File > Build and select your target device. Fill in the required data and click Build. Wait a few seconds and your app is ready for device testing and/or submission for distribution!


Conclusion

In this tutorial, we learned about physics, timers, touch listeners, and other skills that are useful in a wide variety of games. Experiment with the final result and try to make your custom version of the game!

I hope you enjoyed this series and found it helpful. Thank you for reading!

Tags:

Comments

Related Articles