In this tutorial series, we'll use the Corona SDK to build an entertaining Space Shooter Game. Read on!
Step 1: Application Overview
Using pre made graphics we will code an entertaining game using Lua and the Corona SDK API. The user will be able to control a ship sprite and shoot against the enemies.
Step 2: Target Device
The first thing we have to do is select the platform we want to run our app within, this way we'll be able to choose the size for the images we will use. The iOS platform has these characteristics:
- iPad: 1024x768px, 132 ppi
- iPhone/iPod Touch: 320x480px, 163 ppi
- iPhone 4: 960x640px, 326 ppi
Because Android is an open platform, there are many different devices and resolutions. A few of the more common screen characteristics are:
- Google Nexus One: 480x800px, 254 ppi
- Motorola Droid X: 854x480px, 228 ppi
- HTC Evo: 480x800px, 217 ppi
In this tutorial, we'll be focusing on the iOS platform with the graphic design, specifically developing for distribution to an iPhone/iPod touch, but the code presented here should apply to Android development with the Corona SDK as well.
Step 3: Interface
A dark, nice looking interface will be displayed. This involves multiple shapes, buttons, bitmaps, and more. A great sprite library is used in the demo of this tutorial, these are part of theSpriteLib by Flying Yogi.
The interface graphic resources necessary for this tutorial can be found in the attached download.
Step 4: Export Graphics
Depending on the device you have 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.
Step 5: Sound
We'll use Sound Effects to enhance the feeling of the game, you can find the sounds used in this example in Soungle.com using the keywords space, explosion and laser.
Step 6: App Configuration
An external file will be used to make the application go fullscreen 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.
application = { content = { width = 320, height = 480, scale = "letterbox" }, }
Step 7: Main.lua
Let's write the application!
Open your prefered Lua editor (any Text Editor will work, but you won't have syntax highlighting) and prepare to write your awesome app. Remember to save the file as main.lua in your project folder.
Step 8: Code Structure
We'll structure our code as if it were a Class. If you know ActionScript or Java, you should find the structure familiar.
Necessary Classes Variables and Constants Declare Functions constructor (Main function) class methods (other functions) call Main function
Step 9: Hide Status Bar
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.
Step 10: MovieClip Library
local movieclip = require('movieclip')
Animated graphics will be used in this game, the Corona movieclip library will make this a lot easier.
Step 11: Import Physics
We'll also use the Physics library to handle collisions. Use this code to import it:
local physics = require('physics') physics.start() physics.setGravity(0, 0)
Step 12: Background
A simple black background image is added as the background for the application interface, white dots have been added to represent stars.
-- Graphics -- [Background] local bg = display.newImage('bg.png')
Step 13: Title View
This is the Title View, it will be the first interactive screen to appear in our game, these variables store its components.
local title local playBtn local creditsBtn local titleView
Step 14: Credits
This view will show the credits, year and copyright of the game, this variable will be used to store it.
local creditsView
Step 15: Ship
The ship sprites will be stored by this variable.
local ship
Step 16: Boss
This variable stores the boss movieclip.
local boss
Step 17: Score
The score value will be handled by the next variable.
local score
Step 18: Lives
We'll use the same sprite as the ship for the lives, they are stored in the following table.
local lives
Step 19: Code Review
Here is the full code written in this tutorial alongside with comments to help you identify each part:
-- Space Shooter Game -- Developed by Carlos Yanez -- Hide Status Bar display.setStatusBar(display.HiddenStatusBar) -- Import MovieClip Library local movieclip = require('movieclip') -- Import Physics local physics = require('physics') physics.start() physics.setGravity(0, 0) -- Graphics -- Background local bg = display.newImage('bg.png') -- [Title View] local title local playBtn local creditsBtn local titleView -- [Credits] local creditsView -- [Ship] local ship -- [Boss] local boss -- [Score] local score -- [Lives] local lives
Next Time...
In this part of the series you've performed the basic setup of the application. Stay tuned for part two where we will handle the logic of the application, buttons, behavior, and more. See you next time!
Comments