Build a Match Shapes Game - Interface Creation
In this tutorial series, I'll show you how to create a Match Shapes puzzle game with the Corona SDK. You'll learn how to drag objects across the screen and detect when they collide without using the physics engine. The objective of the game is to match the shapes on the stage to its corresponding container. Read on!
Also available in this series:
- Build a Match Shapes Game - Interface Creation
- Build a Match Shapes Game - Adding Interaction
Step 1: Application Overview
Using pre-made graphics we will code an entertaining game using Lua and the Corona SDK API's.
The player will be able to drag the shapes on the stage to match them with its container. You can modify the parameters in the code to customize the game.
Step 2: Target Device


The first step 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 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 many 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, but the code presented here should apply to Android development with the Corona SDK as well.
Step 3: Interface
We'll use a simple and friendly interface that involves multiple shapes, buttons, bitmaps, and more.
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: App Configuration
We'll use an external file to make the application go 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 |
|
2 |
application = |
3 |
{
|
4 |
content = |
5 |
{
|
6 |
width = 320, |
7 |
height = 480, |
8 |
scale = "letterbox" |
9 |
}, |
10 |
} |
Step 6: 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 7: Code Structure
We'll structure the code as if it were a Class. If you know ActionScript or Java, you should find the structure familiar.
1 |
|
2 |
Necessary Classes |
3 |
|
4 |
Variables and Constants |
5 |
|
6 |
Declare Functions |
7 |
|
8 |
contructor (Main function) |
9 |
|
10 |
class methods (other functions) |
11 |
|
12 |
call Main function |
Step 8: Hide Status Bar
1 |
|
2 |
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 9: Background
A simple graphic is used as the background for the application interface, the next line of code stores it.
1 |
|
2 |
-- Graphics |
3 |
|
4 |
-- [Background] |
5 |
|
6 |
local bg = display.newImage('bg.png')
|
Step 10: 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 |
|
2 |
-- [Title View] |
3 |
|
4 |
local titleBg |
5 |
local playBtn |
6 |
local creditsBtn |
7 |
local titleView |
Step 11: Credits View
This view will show the credits and copyright of the game, this variable will be used to store it.
1 |
|
2 |
-- [CreditsView] |
3 |
|
4 |
local creditsView |
Step 12: Game Background
This image will be placed on top of our previous background. This will be the game background.
1 |
|
2 |
-- Game Background |
3 |
|
4 |
local gameBg |
Step 13: Instructions
The next variable will store the instructions graphic.
1 |
|
2 |
-- Instructions |
3 |
|
4 |
local ins |
Step 14: Place Holders
The next images indicate where the shapes have to be placed.
1 |
|
2 |
-- Shapes Placeholder |
3 |
|
4 |
local sHolder |
5 |
local pHolder |
6 |
local tHolder |
Step 15: Shapes
The player will drag the shapes in order to place them in the correct spot.
1 |
|
2 |
-- Shapes |
3 |
|
4 |
local square |
5 |
local pentagon |
6 |
local triangle |
Step 16: Alert
This is the alert that displays when you win the game. It will complete the level and end the game.
1 |
|
2 |
-- Alert |
3 |
|
4 |
local alertView |
Step 17: Sounds
We'll use Sound Effects to enhance the feeling of the game. The sounds used in this app were generated by AS3SFXR.
1 |
|
2 |
-- Sounds |
3 |
|
4 |
local correctSnd = audio.loadSound('correct.caf')
|
5 |
local winSnd = audio.loadSound('win.mp3')
|
Step 18: Variables
Thees are the variables we'll use. Read the comments in the code to know more about them.
1 |
|
2 |
-- Variables |
3 |
|
4 |
local correct = 0 -- Counts the correct placed shapes |
Step 19: Declare Functions
Declare all functions as local in the beginning.
1 |
|
2 |
-- Functions |
3 |
|
4 |
local Main = {}
|
5 |
local startButtonListeners = {}
|
6 |
local showCredits = {}
|
7 |
local hideCredits = {}
|
8 |
local showGameView = {}
|
9 |
local gameListeners = {}
|
10 |
local hitTestObjects = {}
|
11 |
local dragShape = {}
|
12 |
local alert = {}
|
Step 20: Constructor
Next we'll create the function that will initialize the game logic:
1 |
|
2 |
function Main() |
3 |
-- code... |
4 |
end |
Step 21: Add Title View
Now we place the TitleView in the stage and call a function that will add the tap listeners to the buttons.
1 |
|
2 |
function Main() |
3 |
titleBg = display.newImage('titleBg.png', 17, 35)
|
4 |
playBtn = display.newImage('playBtn.png', 138, 240)
|
5 |
creditsBtn = display.newImage('creditsBtn.png', 122, 295)
|
6 |
titleView = display.newGroup(titleBg, playBtn, creditsBtn) |
7 |
|
8 |
startButtonListeners('add')
|
9 |
end |
Conclusion
In this part of the series you've learned the interface and the basic setup of the game. In the next and final part of this tutorial, we'll handle the level creation, collision detection, and the final steps to take before release, such as app testing, creating a start screen, adding an icon, and finally building the app. Read the final installment!



