Advertisement

Create a Physics-Based Puzzle Game - Interface Creation

by

In this tutorial series, I'll be showing you how to create a physics-based puzzle game in Corona SDK. You'll learn about physics manipulation and touch controls. The objective of the game is to get the ball into the container by tapping the correct boxes. Read on!


Also available in this series:

  1. Create a Physics-Based Puzzle Game - Interface Creation
  2. Create a Physics-Based Puzzle Game - Adding Interaction

1. Application Overview

Using pre-made graphics, we'll code an entertaining game using Lua and the Corona SDK APIs.

The player will be able to use the touch screen on the device to remove the boxes on the stage. You can modify the parameters in the code to customize the game.


2. Target Device

First we're going to select the platform we want to run our app within. This will allow us to choose the size for the images we'll use.

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

Since Android is an open platform, there are many different devices and resolutions. Here are a few of the more common screen characteristics.

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

In this tutorial we'll be focusing on the iOS platform with the graphic design, specifically developing it for distribution to an iPhone/iPod touch. The code presented here should apply to Android development with the Corona SDK as well.


3. Interface

A simple and friendly interface will be used. This involves multiple shapes, buttons, bitmaps, and more.

The interface graphic resources necessary for this tutorial can be found in the attached download.


4. Export Graphics

Depending on the device you've 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 will be used to make the application fullscreen across devices. This is 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"
    },
}

6. Main.lua

Let's write the application!

Open your preferred Lua editor and prepare to write your awesome 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.

Necesary Classes

Variables and Constants

Declare Functions

    contructor (Main function)
	
    class methods (other functions)

call Main function

8. 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.


9. Import Physics

We'll use the physics library to handle collisions. Use the code below to import it.

-- Physics

local physics = require('physics')
physics.start()

10. Background

A simple graphic is used as the background for the application interface. The next line of code stores it.

-- Graphics

-- [Background]

local bg = display.newImage('bg.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.

-- [Title View]

local title
local playBtn
local creditsBtn
local titleView

12. Credits View

This view will show the credits and the game's copyright, and this variable will be used to store it.

-- [CreditsView]

local creditsView

13. Boxes

The boxes create the level. There are three sizes in this application.


14. Instructions Message

An instructions message will appear at the start of the game. It'll be tweened out after two seconds.

-- Instructions

local ins

15. Ball

This is the ball graphic. The objective of the game is to put this item in the goal box.

-- Ball

local ball

16. Goal Box

This code stores the goal box graphic.

-- Goal Box

local gBox

17. Alert

This is the alert that'll be displayed when you win the game. It'll complete the level and end the game.

-- Alert
  
local alertView

18. Sounds

We'll use sound effects to enhance the feeling of the game. The sounds used in this app were generated by AS3SFXR.

-- Sounds

local win = audio.loadSound('win.caf')

19. Variables

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

-- Variables

local lvlImg -- stores the current level image title

20. Declare Functions

Declare all functions as local at the start.

-- Functions

local Main = {}
local startButtonListeners = {}
local showCredits = {}
local hideCredits = {}
local showGameView = {}
local addBox = {}
local removeBox = {}
local onCollision = {}
local alert = {}

21. Constructor

Next we'll create the function that will initialize all the game logic.

function Main()
	-- code...
end

22. Add Title View

Now we place the TitleView in the stage and call a function that will add the tap listeners to the buttons.

function Main()
	titleBg = display.newImage('titleBg.png', 84, 12)
	playBtn = display.newImage('playBtn.png', 220, 150)
	creditsBtn = display.newImage('creditsBtn.png', 204, 202)
	titleView = display.newGroup(titleBg, playBtn, creditsBtn)
	
	startButtonListeners('add')
end

Next Time...


In this part of the series, we learned about the interface and the basic setup of the game. In the next and final part of the series, we'll handle level creation, collision detection, and the final steps prior to release. We'll go through app testing, creating a start screen, adding an icon, and lastly, building the app. Stay tuned for the final part!

Advertisement