In the below code, we are assigning our variables as ‘local’ so they don’t use too much RAM by being global. This is a good practise for all of your apps, so start doing it now. ‘enemyShape’ is a table to hold all the enemy objects.

-- Define our variables and tables as local to save memorylocal score
local hiScore
local enemyShape ={}local player
local isGameRunning
local scoreText
local hiScoreText
local MINNUMBEROFENEMIES = 7
local MAXNUMBEROFENEMIES = 101
local numberOfEnemies = MINNUMBEROFENEMIES
local enemyCountdown
local MAXCOUNTDOWN = 500
local gameoverImg, startbuttonImg

Assigning ‘math.random’ to ‘rnd’ will cache the function and make it faster to access. This is a standard in Lua, particularly for math functions.

This is our code for loading and saving the hiScore variable between games. This way, whenever the player plays our game, they will see their highest score to beat.
For loading, we first try to open the file. If it fails because it is the first time, we set the hiScore to ‘0’.

We need to set up a touch function so that when the player touches the player image, it will be draggable around the screen. We also ensure the image stays within the boundaries of the screen by limiting the ‘x’ and ‘y’ values.

-- Set up the player objectlocalfunction initPlayer()-- Create the player object
player = Bitmap.new(Texture.new("images/player.png"))
player.x, player.y =160,240
player.width, player.height =32, 32
player:setPosition(player.x, player.y)
stage:addChild(player)-- Make it touchable
player:addEventListener(Event.TOUCHES_MOVE, imagetouch, player)end

This one is quite long. We are creating all the enemies required, but we only add the first 6 to the active display. The rest are kept outside the display area until the timer adds them in. This way, the game slowly gets more difficult as more enemies appear.
We also make sure the initial enemies do not appear too close to the player’s initial position in the middle of the screen, otherwise it’ll be Game Over too quickly.

This is a simple collision test function. You can find example code for this on the internet with a quick search. Basically, it checks the co-ordinates of the player object compared to the enemy object specified. If they overlap, it returns ‘true’.

localfunction goTouch(gameOverImage, event)-- See if the Game Over object was touchedif gameOverImage:hitTestPoint(event.touch.x, event.touch.y)then
gameoverImg:removeEventListener(Event.TOUCHES_END, goTouch)-- Clean up our objects
stage:removeChild(gameoverImg)
gameoverImg=nillocal i
for i = 1,MAXNUMBEROFENEMIES do
stage:removeChild(enemyShape[i])
enemyShape[i]=nilend
stage:removeChild(player)
player=nil
stage:removeChild(hiScoreText)
hiScoreText=nil
stage:removeChild(scoreText)
scoreText=nil-- Restart the game
startGame()endend

When the player collides with an enemy object, this code is executed. Save the current hiScore, play the ‘game over’ sound effect and display the Game Over image.

-- Game over handlinglocalfunction gameOver()-- Save the current hiScore
saveScore()-- Play explosion
playEffect()-- Remove the listener from the player object
player:removeEventListener(Event.TOUCHES_MOVE, imagetouch)-- Create a Game Over object and display it
gameoverImg = Bitmap.new(Texture.new("images/gameover.png"))
gameoverImg.x, gameoverImg.y =0,200
gameoverImg:setPosition(gameoverImg.x, gameoverImg.y)
stage:addChild(gameoverImg)-- Make the Game Over object touchable
gameoverImg:addEventListener(Event.TOUCHES_BEGIN, goTouch, gameoverImg)end

Supply the player and enemy objects to test if any collided. If they did, go to the Game Over function.

This is the code to run every frame of the game. This means it constantly is run by the application. Sometimes, it’s good to check the game is actually running so they code does not accidentally get called elsewhere. It’s a fail-safe.