– Hide Status Bar
display.setStatusBar(display.HiddenStatusBar)
– Physics
local physics = require(‘physics’)
physics.setDrawMode(“hybrid”)
physics.start()
–physics.setDrawMode(‘hybrid’)
– Graphics
– [Background]
local gameBg = display.newImage(‘gameBg.png’)
– [Title View]
local TitlePage
local playBtn
local creditsBtn
local titleView
– [Credits]
local creditsVeiw
–Helicopter
local helicopter
– Blocks
local blocks = {}
–GameOver
local GameOverVeiw
– Variables
local timerSrc
local yPos = {90, 140, 180}
local speed = 5
local speedTimer
local up = false
local impulse = -60
– Functions
local Main = {}
local startButtonListeners = {}
local showCredits = {}
local hideCredits = {}
local showGameView = {}
local gameListeners = {}
local createBlock = {}
local movePlayer = {}
local increaseSpeed = {}
local update = {}
local alert = {}
function Main ()
TitlePage = display.newImage(‘TitlePage.png’)
TitlePage.x = 200
playBtn = display.newImage (‘playBtn.png’,-75, 50)
creditsBtn = display.newImage(‘creditsBtn.png’,-75, 150)
titleView = display.newGroup(TitlePage, playBtn, creditsBtn)
startButtonListeners(‘add’)
end
function startButtonListeners(action)
if(action == ‘add’) then
playBtn:addEventListener(‘tap’, showGameView)
creditsBtn:addEventListener(‘tap’, showCredits)
else
playBtn:removeEventListener(‘tap’, showGameView)
creditsBtn:removeEventListener(‘tap’, showCredits)
end
end
function showCredits:tap(e)
playBtn.isVisible = false
creditsBtn.isVisible = false
creditsView = display.newImage(‘credits.png’, -110, display.contentHeight-80)
transition.to(creditsView, {time = 300, x = 55, onComplete = function() creditsView:addEventListener(‘tap’, hideCredits) end})
end
function hideCredits:tap(e)
playBtn.isVisible = true
creditsBtn.isVisible = true
transition.to(creditsView, {time = 300, y = display.contentHeight+creditsView.height, onComplete = function() creditsView:removeEventListener(‘tap’, hideCredits) display.remove(creditsView) creditsView = nil end})
end
function showGameView:tap(e)
transition.to(titleView, {time = 300, x = -titleView.height, onComplete = function() display.remove(titleView) titleView = nil end})
– [Add GFX]
– TextFields
scoreTF = display.newText(‘0’, 450, 5, ‘Marker Felt’, 14)
scoreTF:setTextColor(255, 255, 255)
– Helicopter
helicopter = display.newImage(‘helicopter.png’, 23, 152)
– Walls
local top = display.newRect(0, 60, 480, 1)
top:setFillColor(34, 34, 34)
local bottom = display.newRect(0, 260, 480, 1)
bottom:setFillColor(34, 34, 34)
physics.addBody(helicopter)
top.collision=onCollision
top:addEventListener(“collision”, top)
physics.addBody(top,“static”)
bottom.collision=onCollision
bottom:addEventListener(“collision”, bottom)
physics.addBody(bottom,“static”)
blocks = display.newGroup()
gameListener(‘add’)
end
function gameListener(action)
if(action == ‘add’) then
gameBg:addEventListener(‘touch’, movePlayer)
Runtime:addEventListener(‘enterFrame’, update)
timerSrc = timer.performWithDelay(1300, createBlock, 0)
speedTimer = timer.performWithDelay(5000, increaseSpeed, 5)
else
gameBg:addEventListener(‘touch’, movePlayer)
Runtime:removeEventListener(‘enterFrame’, update)
timer.cancel(timerSrc)
timerSrc = nil
timer.cancel(speedTimer)
speedTimer = nil
helicopter:removeEventListener(‘collision’, onCollision)
end
end
function createBlock()
local b
local rnd = math.floor(math.random() * 4) + 1
b = display.newImage(‘block.png’, display.contentWidth, yPos[math.floor(math.random() * 3)+1])
b.name = ‘block’
– Block physics
b.collision=onCollision
b:addEventListener(“collision”, b)
physics.addBody(b,“static”)
physics.addBody(b, ‘kinematic’)
b.isSensor = true
blocks:insert(b)
end
function movePlayer(e)
if(e.phase == ‘began’) then
up = true
end
if(e.phase == ‘ended’) then
up = false
impulse = -60
end
end
function update(e)
– Move helicopter up
if(up) then
impulse = impulse - 3
helicopter:setLinearVelocity(0, impulse)
end
– Move Blocks
if(blocks ~= nil)then
for i = 1, blocks.numChildren do
blocks[i].x = blocks[i].x - speed
end
end
– Score
scoreTF.text = tostring(tonumber(scoreTF.text) + 1)
end
function onCollision(e)
display.remove(helicopter)
display.newImage(‘GameOver.png’)
display.newImage(‘restartBtn.png’, 30, 80)
end
function GameOver ()
display.newImage(‘restartBtn.png’, 30, 80)
restartBtn:addEventListener( “touch”, restart )
end
Main()