Gideros Mobile tutorial – Creating Your First Game

It’s time to put all of your knowledge together and actually make something worthwhile.  We’ll start off with a few functions and build it up to a fully-fledged, fun game! […]

It’s time to put all of your knowledge together and actually make something worthwhile.  We’ll start off with a few functions and build it up to a fully-fledged, fun game!

Create a new project. I called mine “Square Dodge”.

Download the code and all resources for this post from: Square Dodge source code. (3.8 Mb)

Don’t forget to import your audio and image assets!

Square Dodge project








The code below is heavily commented to help you understand how each part works.

Click “Source Code” in each section below to expand the code to read it.

Wow! That’s a lot of code!

Here’s a screenshot of the game in action:

Square Dodge screenshot

















Let’s analyse our code.

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.

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

Save the hiScore so we can later retrieve it.

Initialise the score variable and set up the text at the top of the screen to display the score and hiScore.

Update the score. If ‘score’ is bigger than ‘hiScore’, make them the same.

Load our tune and set it to repeat forever. Load the sound effect for Game Over and play that effect.

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.

Load the player image and set its initial ‘x’ and ‘y’ co-ordinates. Configure the Event Listener for when the player touches the image.

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.

Every now and again, we bring one of the enemies hiding off-screen onto the active screen and set them bouncing around. We also make sure these don’t spawn too close to the player’s current position.

This function will move the enemies on the active screen and bounce them around if they hit the borders of the screen. It decrements the counter and if it’s time, spawns new enemies.

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

Set up the enemies and player object, load the score and set up the text.

Set up the logo and game Start button to be touchable.

Create a start button object and add it to the screen.

Create a Game Over object and make it touchable. Remove the player and enemy objects and text at the top of the screen.

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.

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.

Start the tune playing and start the game off.

This tells the application to call the ‘updateAll’ function constantly to keep things running.

And that’s how you can make a simple game. You’ve learned enough to get out there and get something started.

Next, we’ll go into more complex programming and explore some third-party applications to help make your game developing easier.


If you want more detailed information, I reccommend purchasing a book:

Pin It
Jason Oakley

About Jason Oakley