–Author: Steven Yogarajah
–App: Plane ways
–Date: February 2020
–Define the variables
rotateAmt = 5
rotateMin = -90
rotateMax = 40
starRotation = 0
starForce = 1200
playerPoints = 0
targetHit = false
timeLeft = 1000
–Main Functions
function main()
titleBg = display.newImage(‘titleBg.png’)
playBtn = display.newImage(‘playBtn.png’, display.contentCenterX - 25.5,
display.contentCenterY + 40)
creditsBtn = display.newImage(‘creditsBtn.png’, display.contentCenterX - 40.5,
display.contentCenterY + 85)
titleView = display.newGroup (titleBg, playBtn, creditsBtn)
startButtonListeners (‘add’)
end
function startButtonListeners (action)
if(action == ‘add’) then
playBtn:addEventListener (‘tap’, showGameView)
creditsBtn:addEventListner (‘tap’, showCredits)
end
end
function showGameView ( )
transition.to (titleView, {time = 300, x = -titleView.height, onComplete = function ( ) startButtonListeners (‘rmv’) display.remove (titleView) titleView = nil end } )
score = display.newText (‘0’ … ‘/’ … totalWorms, 58, 6, native.systemFontBold, 16)
score:setTextColor (238, 238, 238)
makeInterface( )
init( )
update( )
makeTarget( )
end
function showCredits ( )
playBtn.isVisible = false
creditsBtn.isVisible = false
creditsView = display.newImage (‘creditsView.png’)
transition.from (creditsView, {time = 300, x = -creditsView.width, onComplete = function ( ) creditsView:addEventListener (‘tap’, hideCredits) creditsView.x = creditsView.x - 0.5 end } )
end
function hideCredits ( )
playBtn.isVisible = true
creditsBtn.isVisible = true
transition.to (creditsView, {time = 300, x = -creditsView.width, onComplete = function ( ) creditsView:removeEventListener (‘tap’, hideCredits) display.remove (creditsView) creditsView = nil end})
end
function makeInterface( )
–up button
upButton = display.newImage(‘up_button.png’)
upButton:translate(99, 70)
upButton:addEventListener(‘touch’, moveUp)
–down button
downButton = display.newImage(‘down_button.png’)
downButton:translate(99, 291)
downButton:addEventListener(‘touch’, moveDown)
–fire button
fireButton = display.newImage(‘fire_button.png’)
fireButton:translate(35, 311)
fireButton:addEventListener(‘touch’, fire)
–display cannon parts
cannonBarrel = display.newImage(‘Plane.png’)
cannonBarrel:translate(75, 190)
–display score
scoreDisplay = display.newText( ('Points: ’ … playerPoints), 0, 0, native.systemFont, 20 )
scoreDisplay:setTextColor( 255,255,255 )
scoreDisplay:translate(display.contentHeight /1.1, 30)
end
function update( )
--decrease the time counter
timeLeft = timeLeft - 1
scoreDisplay.text = 'Points: ’ … playerPoints … ’ Time: ’ … timeLeft
–check if the time has run out
if (timeLeft <= 0) then
–display the final score
scoreDisplay.text = 'Your score was a massive ’ … playerPoints
–remove all of the screen objects
display.remove(cannonBase)
display.remove(cannonBarrel)
display.remove(target)
display.remove(upButton)
display.remove(downButton)
display.remove(fireButton)
–display the ‘game over’ sign
gameOver = display.newImage(‘gameover.png’, 310, 150)
gameOver:addEventListener(‘touch’, closeDownApp)
gameOver = display.newImage(‘leave.png’, 310, 265)
gameOver:addEventListener(‘touch’, closeDownApp)
end
–check if the target has been hit
if (targetHit == true) then
targetHit = false
playerPoints = playerPoints + 1
Hit = display.newImage(‘Hit.png’, target.x, target.y)
–replace the target with the hit picture
transition.dissolve(Hit, target, 1000, 0)
display.remove(bullet)
–put the target back to a random position
target.x = math.random(200, 450 )
target.y = math.random(100, 290 )
end
end
function onCollide(event)
targetHit=true
end
function fire(event)
–only fire at the beginning of a touch event
if (event.phase == ‘began’) then
media.playSound(‘bang.wav’)
bullet = display.newImage(‘star.png’)
–move the image
bullet.x, bullet.y = cannonBarrel:localToContent(70, 0)
bullet.rotation = starRotation
–apply physics to the star
physics.addBody( bullet, { density=2.0, friction=0.2, radius=15 } )
–determine the appropriate ratio of horizontal to vertical force
force_x = math.cos(math.rad(starRotation)) * starForce
force_y = math.sin(math.rad(starRotation)) * starForce
–fire the star
bullet:applyForce( force_x, force_y, bullet.x, bullet.y )
end
end
function moveDown(event)
–only move the barrel if the touch event started
if (event.phase == ‘began’) then
starRotation = starRotation + rotateAmt
if (starRotation >= rotateMax) then
starRotation = rotateMax
end
cannonBarrel.rotation = starRotation
fireButton.rotation = starRotation
end
end
function moveUp(event)
–only move the barrel if the touch event started
if (event.phase == ‘began’) then
starRotation = starRotation - rotateAmt
if (starRotation <= rotateMin) then
starRotation = rotateMin
end
cannonBarrel.rotation = starRotation
fireButton.rotation = starRotation
end
end
function makeTarget( )
target = display.newImage(‘target.png’)
target.x = math.random(200, 450)
target.y = math.random(100, 290)
physics.addBody(target,{density=1.0, friction=0.5, bounce=0.05, radius=15})
target.bodyType = ‘static’
target:addEventListener(‘collision’, onCollide)
end
–Define control structure
function init( )
display.setStatusBar(display.HiddenStatusBar)
background = display.newImage(‘bg.png’)
physics = require(‘physics’)
physics.setDrawMode(‘normal’)
physics.start( )
makeInterface( )
makeTarget( )
Runtime:addEventListener(‘enterFrame’, update)
end
–Call the code
init( )
–Define the functions
function closeDownApp(event)
os.exit ( )
end