Corona SDK: Build a Shell Game – Setup

Corona SDK: Build a Shell Game – Setup

Tutorial Details
  • Technology: Corona SDK
  • Difficulty: Intermediate
  • Completion Time: 30 Minutes

In this tutorial series, you’ll learn how to create your own version of the classic Three Shell game. Read on!


Step 1: Application Overview

Corona Shell Game

Using pre made graphics we will code an entertaining game using Lua and the Corona SDK API’s.

The objective of the game is to follow the shell that has the hidden ball inside it, and then tap it after they have been shuffled. You will be able to configure the speed and movements performed by the shells.

Step 2: Target Device

Shell Game Figure 1 - Corona SDK

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


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

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.

<hr/>
<h2><span>Step 6:</span> Main.lua</h2>
<p>Let’s write the application!</p>
<p>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 <em>main.lua</em> in your project folder.</p>
<hr/>
<h2><span>Step 7:</span> Code Structure</h2>
<p>We’ll structure our code as if it were a Class. If you know ActionScript or Java, you should find the structure familiar.</p>

[sourcecode] Necesary Classes

Variables and Constants

Declare Functions

contructor (Main function)

class methods (other functions)

call Main function


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


Step 9: Background

A simple brown 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')

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.

local title
local playBtn
local creditsBtn
local titleView

Step 11: Credits

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

local creditsView

Step 12: Bank Credits

The bank credits are the equivalent of lives in this game.

local bank
local bankText

Step 13: Shells

These variables are used to hide the ball and distract the player from the correct answer:

local s1
local s2
local s3
local shells

Step 14: Ball

Corona SDK Shell Game

The objective of the game is to find the ball, which is stored in the following variable:

local ball

Step 15: Button Bar

This bar contains the button that will initiate the game and a message TextField used to give instructions.

-- [Button Bar]
local buttonBar
-- [Bet Button]
local betBtn
-- [Message Text]
local msg
-- [GameView]
local gameView -- A group to store all the gameview graphics

Step 16: Alert

Corona SDK Shell Game

An alert will be shown when all the bank credits are gone. This message will display the game over message and restart instructions.

  local alert
  


Step 17: Code Review

Here is the full code written in this tutorial alongside with comments to help you identify each part:

-- Three Shell Game
-- Developed by Carlos Yanez
-- Hide Status Bar
display.setStatusBar(display.HiddenStatusBar)
-- Graphics
-- [Background]
local bg = display.newImage('bg.png')
-- [Title View]
local title
local playBtn
local creditsBtn
local titleView
-- [Credits]
local creditsView
-- [Alert]
local alert
-- [Bank Credits]
local bank
local bankText
-- [Shells]
local s1
local s2
local s3
local shells
-- [Ball]
local ball
-- [Button Bar]
local buttonBar
-- [Bet Button]
local betBtn
-- [Message Text]
local msg
-- [GameView]
local gameView

Next Time…

In this part of the series you’ve learned the interface and the basic setup of the game. Stay tuned for part two where we will handle the logic of the application, button behavior, and more. See you next time!

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://Inventanew.com John

    This is great thank you. I will be using this to teach my high school kids programming. Can’t wait for the next round.

  • Margalus

    Great tutorial! I’m just getting into Corona SDK and i’m reading Ansca docs plus their step-by-step tutorials from the basics but it’s great to watch how “simple” games like this are done, it really helps you getting in the correct workflow. Thanks for the great job!

  • Jameel

    Hi,

    I need some help—
    I need to create score for game, then i need the application to tell add
    scores, i have tried everything and cannt find a way to do this, plz provide
    me some sample if you can so that it can make me undetstand better.
    pls help me.
    Thanks

    The below is the code …
    display.setStatusBar (display.HiddenStatusBar)
    require (“physics”)

    local score = 0
    local scoreText
    local levelText
    local levelNum

    function main()
    setUpPhysics()
    createWalls()
    createBricks()
    createBall()
    createPaddle()
    startGame()
    createScore ()
    createLevel ()

    end

    function setUpPhysics()
    physics.start()
    physics.setGravity(0,0)
    end

    function createPaddle()
    local paddleWidth = 200
    local paddleHeight = 30

    local paddle = display.newRect( display.contentWidth /1 -
    paddleWidth /1, display.contentHeight -30, paddleWidth, paddleHeight )
    physics.addBody(paddle, “static”, {friction=0, bounce=1})
    local movePaddle = function(event)
    paddle.x = event.x
    end

    Runtime:addEventListener(“touch”, movePaddle)
    end

    function createBall()
    local ballRadius = 20
    ball = display.newCircle( display.contentWidth / 2,
    display.contentHeight / 2, ballRadius )
    physics.addBody(ball, “dynamic”, {friction=0, bounce = 1,
    radius=ballRadius})
    ball.collision = function(self, event)
    if(event.phase == “ended”) then
    if(event.other.type == “destructible”) then
    event.other:removeSelf()
    end
    if(event.other.type == “bottomWall”) then
    self:removeSelf()
    local onTimerComplete = function(event)
    createBall()
    startGame()
    end
    timer.performWithDelay(500, onTimerComplete , 1)
    end
    end
    end
    ball:addEventListener(“collision”, ball)
    end

    function startGame()
    ball:setLinearVelocity(200, 600)

    end

    function createBricks()
    local brickWidth = 60
    local brickHeight = 30
    local numOfRows = 6
    local numOfCols = 7
    local topLeft = {x= display.contentWidth / 2 – (brickWidth *
    numOfCols ) / 2, y= 50}
    local row
    local col
    for row = 0, numOfRows – 1 do
    for col = 0, numOfCols – 1 do

    function createScore ()
    levelScore = display.newText(‘Score:’, 50, 10, ‘akashi’, 30)
    levelScore:setTextColor(254, 203, 50)
    levelNum = display.newText(’1′, 540, 10, ‘akashi’, 30)
    levelNum:setTextColor(254,203,50)
    end
    function createLevel ()
    levelText = display.newText(‘Level:’, 460, 10, ‘akashi’, 30)
    levelText:setTextColor(254, 203, 50)
    levelNum = display.newText(’1′, 540, 10, ‘akashi’, 30)
    levelNum:setTextColor(254,203,50)

    end

    – Create a brick
    local brick = display.newRect( topLeft.x + (col * brickWidth),
    topLeft.y + (row * brickHeight), brickWidth, brickHeight )
    brick:setFillColor(math.random(50, 255), math.random(50, 255),
    math.random(50, 255), 255)
    brick.type = “destructible”
    physics.addBody(brick, “static”, {friction=0, bounce = 1})
    end
    end
    end

    function createWalls()
    local wallThickness = 10
    – Left wall
    local wall = display.newRect( 0, 0, wallThickness,
    display.contentHeight )
    physics.addBody(wall, “static”, {friction=0, bounce = 1})
    – Top wall
    wall = display.newRect(0,0, display.contentWidth, wallThickness)
    physics.addBody(wall, “static”, {friction=0, bounce = 1})
    – Right wall
    wall = display.newRect(display.contentWidth – wallThickness, 0,
    wallThickness, display.contentHeight)
    physics.addBody(wall, “static”, {friction=0, bounce = 1})
    – Bottom wall
    wall = display.newRect(0, display.contentHeight – wallThickness,
    display.contentWidth, wallThickness)
    physics.addBody(wall, “static”, {friction=0, bounce = 1})
    wall.type = “bottomWall”

    end
    main()