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



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



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



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



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.
1 |
application = |
2 |
{
|
3 |
content = |
4 |
{
|
5 |
width = 320, |
6 |
height = 480, |
7 |
scale = "letterbox" |
8 |
}, |
9 |
} |
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.
1 |
Necessary Classes |
2 |
|
3 |
Variables and Constants |
4 |
|
5 |
Declare Functions |
6 |
|
7 |
contructor (Main function) |
8 |
|
9 |
class methods (other functions) |
10 |
|
11 |
call Main function |
8. Hide Status Bar
1 |
display.setStatusBar(display.HiddenStatusBar) |
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:
1 |
-- Physics |
2 |
|
3 |
local physics = require('physics')
|
4 |
physics.start() |
10. Background



The next line of code creates a simple background for the application interface.
1 |
-- Graphics |
2 |
|
3 |
-- [Background] |
4 |
|
5 |
local gameBg = display.newImage('gameBg.png')
|
11. Title View



This is the Title View; it will be the first interactive screen to appear in our game. These variables store its components:
1 |
-- [Title View] |
2 |
|
3 |
local title |
4 |
local playBtn |
5 |
local creditsBtn |
6 |
local titleView |
12. Credits View



This view shows the credits and copyright of the game. This variable stores it:
1 |
-- [CreditsView] |
2 |
|
3 |
local creditsView |
13. Instructions Message

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.
1 |
-- Instructions |
2 |
|
3 |
local ins |
14. Turret

This is the turret graphic, it will be placed at the center of our game.
1 |
-- Turret |
2 |
|
3 |
local turret |
15. Enemy

Enemies appear from the edge of the screen, the next Group stores them.
1 |
-- Enemy |
2 |
|
3 |
local enemies |
16. Alert



This is the alert that's displayed when the player runs out of bullets. It shows the score and ends the game.
1 |
-- Alert |
2 |
|
3 |
local alertView |
17. 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.
1 |
-- Sounds |
2 |
|
3 |
local bgMusic = audio.loadStream('POL-hard-corps-short.mp3')
|
4 |
local shootSnd = audio.loadSound('shoot.wav')
|
5 |
local exploSnd = audio.loadSound('explo.wav')
|
18. Variables
These are the variables we'll use. You can read the comments in the code to learn more about them.
1 |
-- Variables |
2 |
|
3 |
local timerSrc |
4 |
local yPos = {58, 138, 218} --posible Y positions for enemies
|
5 |
local speed = 3 |
6 |
local targetX --stores position of enemy when shot |
7 |
local targetY |
19. Declare Functions
Declare all functions as local at the start.
1 |
-- Functions |
2 |
|
3 |
local Main = {}
|
4 |
local startButtonListeners = {}
|
5 |
local showCredits = {}
|
6 |
local hideCredits = {}
|
7 |
local showGameView = {}
|
8 |
local gameListeners = {}
|
9 |
local createEnemy = {}
|
10 |
local shoot = {}
|
11 |
local update = {}
|
12 |
local onCollision = {}
|
13 |
local addExBullets = {}
|
14 |
local alert = {}
|
20. Constructor
Next we'll create the function that initializes the game logic:
1 |
function Main() |
2 |
-- code... |
3 |
end |
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.
1 |
function Main() |
2 |
titleBg = display.newImage('title.png')
|
3 |
playBtn = display.newImage('playBtn.png', 212, 163)
|
4 |
creditsBtn = display.newImage('creditsBtn.png', 191, 223)
|
5 |
titleView = display.newGroup(titleBg, playBtn, creditsBtn) |
6 |
|
7 |
startButtonListeners('add')
|
8 |
end |
22. Start Button Listeners
This function adds the necessary listeners to the TitleView buttons.
1 |
function startButtonListeners(action) |
2 |
if(action == 'add') then |
3 |
playBtn:addEventListener('tap', showGameView)
|
4 |
creditsBtn:addEventListener('tap', showCredits)
|
5 |
else |
6 |
playBtn:removeEventListener('tap', showGameView)
|
7 |
creditsBtn:removeEventListener('tap', showCredits)
|
8 |
end |
9 |
end |
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.
1 |
function showCredits:tap(e) |
2 |
playBtn.isVisible = false |
3 |
creditsBtn.isVisible = false |
4 |
creditsView = display.newImage('credits.png', -110, display.contentHeight-80)
|
5 |
transition.to(creditsView, {time = 300, x = 55, onComplete = function() creditsView:addEventListener('tap', hideCredits) end})
|
6 |
end |
24. Hide Credits
When the credits screen is tapped, it'll be tweened out of the stage and removed.
1 |
function hideCredits:tap(e) |
2 |
playBtn.isVisible = true |
3 |
creditsBtn.isVisible = true |
4 |
transition.to(creditsView, {time = 300, y = display.contentHeight+creditsView.height, onComplete = function() creditsView:removeEventListener('tap', hideCredits) display.remove(creditsView) creditsView = nil end})
|
5 |
end |
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.
1 |
function showGameView:tap(e) |
2 |
transition.to(titleView, {time = 300, x = -titleView.height, onComplete = function() startButtonListeners('rmv') display.remove(titleView) titleView = nil end})
|
26. Instructions Message
The following lines add the instructions message.
1 |
ins = display.newImage('ins.png', 135, 255)
|
2 |
transition.from(ins, {time = 200, alpha = 0.1, onComplete = function() timer.performWithDelay(2000, function() transition.to(ins, {time = 200, alpha = 0.1, onComplete = function() display.remove(ins) ins = nil end}) end) end})
|
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.
1 |
-- Bullets Left |
2 |
|
3 |
bullets = display.newGroup() |
4 |
bulletsLeft = display.newGroup() |
5 |
for i = 1, 5 do |
6 |
local b = display.newImage('bullet.png', i*12, 12)
|
7 |
bulletsLeft:insert(b) |
8 |
end |
28. Score TextField
This is the Score TextField created at the top-right of the stage:
1 |
-- TextFields |
2 |
|
3 |
scoreTF = display.newText('0', 70, 23.5, 'Courier Bold', 16)
|
4 |
scoreTF:setTextColor(239, 175, 29) |
29. Turret
Now we'll place the turret in the stage.
1 |
-- Turret |
2 |
|
3 |
turret = display.newImage('turret.png', 220, 301)
|
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.
1 |
enemies = display.newGroup() |
2 |
gameListeners('add')
|
3 |
audio.play(bgMusic, {loops = -1, channel = 1})
|
4 |
end |
31. Game Listeners
This function adds the necessary listeners to start the game logic:
1 |
function gameListeners(action) |
2 |
if(action == 'add') then |
3 |
timerSrc = timer.performWithDelay(1200, createEnemy, 0) |
4 |
Runtime:addEventListener('enterFrame', update)
|
5 |
else |
6 |
timer.cancel(timerSrc) |
7 |
timerSrc = nil |
8 |
Runtime:removeEventListener('enterFrame', update)
|
9 |
gameBg:removeEventListener('tap', shoot)
|
10 |
end |
11 |
end |
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.
1 |
function createEnemy() |
2 |
local enemy |
3 |
local rnd = math.floor(math.random() * 4) + 1 |
4 |
enemy = display.newImage('enemy.png', display.contentWidth, yPos[math.floor(math.random() * 3)+1])
|
5 |
enemy.name = 'bad' |
6 |
-- Enemy physics |
7 |
physics.addBody(enemy) |
8 |
enemy.isSensor = true |
9 |
enemy:addEventListener('collision', onCollision)
|
10 |
enemies:insert(enemy) |
11 |
end |
33. Shoot
When the player taps the screen a bullet is created and a sound plays. It has physics properties that detect collisions.
1 |
function shoot() |
2 |
audio.play(shootSnd) |
3 |
local b = display.newImage('bullet.png', turret.x, turret.y)
|
4 |
physics.addBody(b) |
5 |
b.isSensor = true |
6 |
bullets:insert(b) |
34. Update Bullets Left
Remove a bullet from the "Bullets Left" area at the top-left of the stage.
1 |
-- Remove Bullets Left |
2 |
bulletsLeft:remove(bulletsLeft.numChildren) |
3 |
-- End game 4 seconds after last bullet |
4 |
if(bulletsLeft.numChildren == 0) then |
5 |
timer.performWithDelay(4000, alert, 1) |
6 |
end |
7 |
end |
35. Move Enemies
The following function runs every frame. Here we use it to move every enemy in the enemies table.
1 |
function update() |
2 |
-- Move enemies |
3 |
if(enemies ~= nil)then |
4 |
for i = 1, enemies.numChildren do |
5 |
enemies[i].x = enemies[i].x - speed |
6 |
end |
7 |
end |
36. Move Shoot Bullets
A similar method is used for the bullets.
1 |
-- Move Shoot bullets |
2 |
if(bullets[1] ~= nil) then |
3 |
for i = 1, bullets.numChildren do |
4 |
bullets[i].y = bullets[i].y - speed*2 |
5 |
end |
6 |
end |
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.
1 |
-- Move Explosion Bullets |
2 |
if(exploBullets[1] ~= nil) then |
3 |
for j = 1, #exploBullets do |
4 |
if(exploBullets[j][1].y ~= nil) then exploBullets[j][1].y = exploBullets[j][1].y + speed*2 end |
5 |
if(exploBullets[j][2].y ~= nil) then exploBullets[j][2].y = exploBullets[j][2].y - speed*2 end |
6 |
if(exploBullets[j][3].x ~= nil) then exploBullets[j][3].x = exploBullets[j][3].x + speed*2 end |
7 |
if(exploBullets[j][4].x ~= nil) then exploBullets[j][4].x = exploBullets[j][4].x - speed*2 end |
8 |
end |
9 |
end |
10 |
end |
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.
1 |
function onCollision(e) |
2 |
audio.play(exploSnd) |
3 |
targetX = e.target.x |
4 |
targetY = e.target.y |
5 |
timer.performWithDelay(100, addExBullets, 1) |
6 |
-- Remove Collision Objects |
7 |
display.remove(e.target) |
8 |
e.target = nil |
9 |
display.remove(e.other) |
10 |
e.other = nil |
11 |
-- Increase Score |
12 |
scoreTF.text = tostring(tonumber(scoreTF.text) + 50) |
13 |
scoreTF.x = 90 |
14 |
end |
39. Add Explosion Bullets
This code creates and places the four additional bullets in the correct position to be moved by the update function.
1 |
function addExBullets() |
2 |
-- Explosion bullets |
3 |
local eb = {}
|
4 |
local b1 = display.newImage('bullet.png', targetX, targetY)
|
5 |
local b2 = display.newImage('bullet.png', targetX, targetY)
|
6 |
local b3 = display.newImage('bullet.png', targetX, targetY)
|
7 |
local b4 = display.newImage('bullet.png', targetX, targetY)
|
8 |
physics.addBody(b1) |
9 |
b1.isSensor = true |
10 |
physics.addBody(b2) |
11 |
b2.isSensor = true |
12 |
physics.addBody(b3) |
13 |
b3.isSensor = true |
14 |
physics.addBody(b4) |
15 |
b4.isSensor = true |
16 |
table.insert(eb, b1) |
17 |
table.insert(eb, b2) |
18 |
table.insert(eb, b3) |
19 |
table.insert(eb, b4) |
20 |
table.insert(exploBullets, eb) |
21 |
end |
40. Alert
The alert function creates an alert view, animates it, and ends the game.
1 |
function alert() |
2 |
audio.stop(1) |
3 |
audio.dispose() |
4 |
bgMusic = nil |
5 |
gameListeners('rmv')
|
6 |
alertView = display.newImage('alert.png', 160, 115)
|
7 |
transition.from(alertView, {time = 300, xScale = 0.5, yScale = 0.5})
|
8 |
|
9 |
local score = display.newText(scoreTF.text, (display.contentWidth * 0.5) - 18, (display.contentHeight * 0.5) + 5, 'Courier Bold', 18) |
10 |
score:setTextColor(44, 42, 49) |
11 |
|
12 |
-- Wait 100 ms to stop physics |
13 |
timer.performWithDelay(1000, function() physics.stop() end, 1) |
14 |
end |
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:
1 |
Main() |
42. 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

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



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



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!



