Advertisement

Create a Sound Based Memory Game - Interface Creation

by

In this tutorial series, I'll be showing you how to create a sound based memory game with the Corona SDK. You'll learn about loading and playing sounds as well as storing information in Lua Tables. The objective of the game is to reproduce the level sound by tapping on the color squares. Read on!


Also available in this series:

  1. Create a Sound Based Memory Game - Interface Creation
  2. Create a Sound Based Memory Game - Game Logic

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 use the on-screen buttons to reproduce the current sound sequence, you can modify the parameters in the code to customize the game.


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


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


5. 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"
    },
}

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.


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

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. 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')

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.

-- [Title View]

local title
local playBtn
local creditsBtn
local titleView

11. Credits View

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

-- [CreditsView]

local creditsView

12. Square Buttons

The buttons used to reproduce the sound.

-- Color Squares

local r = {}
local g = {}
local ye = {}
local b = {}

13. Instructions Message

An instructions message will appear at the start of the game, when the message is dismissed the sound sequence will play.

-- Instructions

local ins

14. Check Button

This button will compare the user input to the current level to determine if the sequence is correct or not.

-- Check button

local check

15. Alert

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

-- Alert
  
local alertView

16. Sounds

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

-- Sounds

r.s = audio.loadSound('1.mp3')
g.s = audio.loadSound('2.mp3')
ye.s = audio.loadSound('3.mp3')
b.s = audio.loadSound('4.mp3')

17. Variables

These are the variables we'll use, read the comments in the code to know more about them.

-- Levels, modify these tables to change or add more levels, each letter represents a button/sound i.e. r = red, ye = yellow and so on

local l1 = {r, ye, g, b}
local l2 = {ye, r, b, g, b, r, g, ye}

-- Variables

local lastY
local currentLevel = l1
local gameTimer
local times = 1
local userInput = {} --stores the tapped buttons
local correct = 0

18. Declare Functions

Declare all functions as local at the start.

-- Functions

local Main = {}
local startButtonListeners = {}
local showCredits = {}
local hideCredits = {}
local showGameView = {}
local gameListeners = {}
local playLevel = {}
local onTap = {}
local checkInput = {}
local alert = {}

19. Constructor

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

function Main()
	-- code...
end

20. 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()
	title = display.newImage('title.png', 73, 53)
	playBtn = display.newImage('playBtn.png', 140, 245)
	creditsBtn = display.newImage('creditsBtn.png', 124, 305)
	titleView = display.newGroup(title, playBtn, creditsBtn)
	
	startButtonListeners('add')
end

Next Time...

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 the series, we'll handle the level creation, table comparison, and the final steps to take prior to release like app testing, creating a start screen, adding an icon and, finally, building the app. Read the final part now!

Advertisement
Related Posts