Build an Ostrich Infinite Scroller Game


In this tutorial series, I'll be showing you how to create an infinite scroller game with the Corona SDK by building an Ostrich Runner game. You'll learn about scrolling objects, physics manipulation, touch controls, and collision detection. The objective of the game is to move the character and collect items to raise the score. Read on!

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 touch screen on the device to move the character and to collect items. 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 S III: 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. The interface will involve 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 this using 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 to 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

Now 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 the Status Bar


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 this code to import it:

-- Physics

local physics = require('physics')

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 the components:

-- [Title View]

local title
local playBtn
local creditsBtn
local titleView

12. Credits View

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

-- [CreditsView]

local creditsView

13. Game Background

The level background, it also adds the score textfield.

-- Game Background

local gameBg

14. Instructions Message

An instructions message will appear at the start of the game, it will be tweened out after 2 seconds.

-- Instructions

local ins

15. Character

The character graphic. The objective of the game is to move it up and down in the screen in order to collect the cherries.

-- Ostrich

local ostrich

16. Cherries

The cherry graphic. There are also bad cherries that will make you lose the game.

-- Cherrys

local cherrys

17. Pad

The pad graphics. Tap on them to move the character.

-- Pad

local up
local down

18. Alert

This is the alert that will be displayed when a bad cherry is collected. It will complete the level and end the game.

-- Alert
local alertView

19. Sounds

We'll use Sound Effects to enhance the feeling of the game, you can find the music used in this example on The sounds were created in as3sfxr.

-- Sounds

local bgMusic = audio.loadStream('POL-purple-hills-short.mp3')
local cherrySnd = audio.loadSound('cherry.mp3')
local badCherrySnd = audio.loadSound('badCherry.mp3')

20. Variables

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

-- Variables

local timerSrc -- add cherry timer
local yPos = {108, 188, 268} -- possible ostrich y positions
local speed = 5 -- cherry speed
local speedTimer -- timer to change speed of the cherrys

21. Declare Functions

Declare all functions as local at the start.

-- Functions

local Main = {}
local startButtonListeners = {}
local showCredits = {}
local hideCredits = {}
local showGameView = {}
local gameListeners = {}
local startGame = {}
local createCherry = {}
local movePlayer = {}
local increaseSpeed = {}
local update = {}
local onCollision = {}
local alert = {}

22. Constructor

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

function Main()
	-- code...

23. 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('title.png', 113, 43)
	playBtn = display.newImage('playBtn.png', 219, 160)
	creditsBtn = display.newImage('creditsBtn.png', 205, 223)
	titleView = display.newGroup(bg, titleBg, playBtn, creditsBtn)

24. Start Button Listeners

This function adds the necessary listeners to the TitleView buttons.

function startButtonListeners(action)
	if(action == 'add') then
		playBtn:addEventListener('tap', showGameView)
		creditsBtn:addEventListener('tap', showCredits)
		playBtn:removeEventListener('tap', showGameView)
		creditsBtn:removeEventListener('tap', showCredits)

25. Show Credits

The credits screen is shown when the user taps the about button. A tap listener is added to the credits view to remove it.

function showCredits:tap(e)
	playBtn.isVisible = false
	creditsBtn.isVisible = false
	creditsView = display.newImage('credits.png', -130, display.contentHeight-140), {time = 300, x = 65, onComplete = function() creditsView:addEventListener('tap', hideCredits) end})

26. Hide Credits

When the credits screen is tapped, it'll be tweened out of the stage and removed.

function hideCredits:tap(e)
	playBtn.isVisible = true
	creditsBtn.isVisible = true, {time = 300, y = display.contentHeight+creditsView.height, onComplete = function() creditsView:removeEventListener('tap', hideCredits) display.remove(creditsView) creditsView = nil end})

27. Show Game View

When the Play button is tapped, the title view is tweened and removed, revealing the game view. There are many parts involved in this view, so we'll split them in the next steps.

function showGameView:tap(e), {time = 300, x = -titleView.height, onComplete = function() startButtonListeners('rmv') display.remove(titleView) titleView = nil end})

28. Instructions Message

The following lines add the game instructions.

-- Instructions Message
ins = display.newImage('ins.png', 187, 199)

29. Score TextField

This part creates the Score TextField on the stage.

-- TextFields
scoreTF = display.newText('0', 258, 26.5, 'Marker Felt', 16)
scoreTF:setTextColor(184, 165, 104)

30. Ostrich

Add the ostrich character to the level.

-- Ostrich
ostrich = display.newImage('ostrich.png', 11, 180)

31. Pad

These are the on screen controls for the game.

-- Pad
up = display.newImage('up.png', 418, 20)
down = display.newImage('down.png', 418, 258) = 'up' = 'down'

32. Physics

Next, we add physics to the game objects. We also create a Table for the cherries and call the gameListeners function.

	-- Add Ostrich Physics
	ostrich.isSensor = true

	cherrys = display.newGroup()

33. Game Listeners

This function adds the necessary listeners to start the game logic.

function gameListeners(action)
	if(action == 'add') then
		ins:addEventListener('tap', startGame)
		Runtime:addEventListener('enterFrame', update)
		up:addEventListener('tap', movePlayer)
		down:addEventListener('tap', movePlayer)
		speedTimer = timer.performWithDelay(5000, increaseSpeed, 5)
		Runtime:removeEventListener('enterFrame', update)
		up:removeEventListener('tap', movePlayer)
		down:removeEventListener('tap', movePlayer)
		timerSrc = nil
		speedTimer = nil

34. Start Game

In this part, we remove the instructions message, start playing the game background music, and create a timer that will add a cherry every 400 milliseconds.

function startGame()
	ins:removeEventListener('tap', startGame)
	display.remove(ins), {loops = -1, channel = 1})
	timerSrc = timer.performWithDelay(400, createCherry, 0)

35. Create Cherry

The next snippet of code creates a regular or bad cherry based on a random number and places it on the screen. The resulting object is added to the physics engine to check for collisions.

function createCherry()
	local cherry
	local rnd = math.floor(math.random() * 4) + 1
	if(rnd == 4) then
		cherry = display.newImage('badCherry.png', display.contentWidth, yPos[math.floor(math.random() * 3)+1]) = 'bad'
		cherry = display.newImage('cherry.png', display.contentWidth, yPos[math.floor(math.random() * 3)+1]) = 'cherry'
	-- Cherry physics
	cherry.isSensor = true
	cherry:addEventListener('collision', onCollision)

36. Move Player

We change the Y position of the Ostrich using the control pad we created earlier.

function movePlayer(e)
	if( == 'up' and ostrich.y ~= 122) then
		ostrich.y = ostrich.y - 80
	elseif( == 'down' and ostrich.y ~= 282) then
		ostrich.y = ostrich.y + 80

37. Increase Speed

A timer will increase the speed every 5 seconds. An icon is displayed to alert the player of the speed change.

function increaseSpeed()
	speed = speed + 2
	-- Icon
	local icon = display.newImage('speed.png', 204, 124)
	transition.from(icon, {time = 200, alpha = 0.1, onComplete = function() timer.performWithDelay(500, function(), {time = 200, alpha = 0.1, onComplete = function() display.remove(icon) icon = nil end}) end) end})

38. Update Function

This function handles the cherries movement. It uses the speed variable to determine how many pixels to move the cherry every frame.

function update()
	if (cherrys ~= nil) then
		for i = 1, cherrys.numChildren do
			cherrys[i].x = cherrys[i].x - speed

39. Collisions

Now we check if the cherry collides with the Ostrich using the following code. The score raises when a regular cherry collides and an alert is called when a bad cherry is touched. In both cases the cherry is removed and a sound is played.

function onCollision(e)'collision', onCollision)
	if( == 'cherry') then
		scoreTF.text = tostring(tonumber(scoreTF.text) + 50)
	-- Bad cherry
	elseif( == 'bad') then

40. Alert

The alert function creates an alert view, animates it, and then ends the game.

function alert()
	bgMusic = nil
	alertView = display.newImage('alert.png', 160, 115)
	transition.from(alertView, {time = 300, xScale = 0.5, yScale = 0.5})
	local score = display.newText(scoreTF.text, (display.contentWidth * 0.5) - 12, (display.contentHeight * 0.5) + 5, 'Marker Felt', 18)
	score:setTextColor(184, 165, 104)
	-- Wait 100 ms to stop physics
	timer.performWithDelay(1000, function() physics.stop() end, 1)

41. Call Main Function

In order to start the game, the Main function needs to be called. With the above code in place, we'll do that here:


42. Loading Screen

The Default.png file is an image that will be displayed right when you start the application while the iOS loads the basic data to show the Main Screen. Add this image to your project source folder, it will be automatically added by the Corona compiler.

43. Icon

Using the graphics you created before, you can now create a nice and good looking icon. The icon size for the non-retina iPhone icon is 57x57px, but the retina version is 114x114px and the iTunes store requires a 512x512px version. I suggest creating the 512x512 version first and then scaling down for the other sizes.

It doesn't need to have the rounded corners or the transparent glare, iTunes and the iPhone will do that for you.

44. Testing in the Simulator

It's time to do the final test. Open the Corona Simulator, browse to your project folder, and then click open. If everything works as expected, you are ready for the final step!

45. Build

In the Corona Simulator, go to File > Build and select your target device. Fill the required data and click build. Wait a few seconds and your app will be ready for device testing and/or submission for distribution!


In this series, we've learned about physics behavior, tap listeners, and collisions. These skills can be really useful in a wide number of games!

Experiment with the final result and try to make your custom version of the game!

I hope you liked this tutorial and find it helpful. Thank you for reading!