Creating Circle Pop – Part 3

Hello and welcome to another post by That’s So Panda! In this tutorial series, we are learning how to create a circle pop game where players have to pop circles. This game is being built with Corona SDK. If you are new to this series, I strongly suggest reading Part 1 and Part 2. In Part 3 of this series, we will be covering the main.lua file. So, let’s get started!

If you would like to purchase this as a template to support this website or to avoid having to retype all of this code, you can buy the template at thatssopanda.com/corona-sdk-templates/.

main.lua

main.lua is where every game built with Corona SDK begins. When you open a project that uses the Corona platform, you’ll have to open main.lua. If you want to even start your game, you’ll have to use main.lua. To get started, I like to put the name of the file at the top and in some cases, I’ll even put a short description of the file. So, open up your project folder (the one that contains your build.settings and config.lua files) and create a new file called main.lua. As our first addition to main.lua, add the following comments.

-------------------------------------------------------------------------------------------------------------------------------------------
-- main.lua
-- The main.lua file is where your Corona SDK app begins.
--
-- Circle Pop: Circle Pop is a game where players touch a circle to 'pop' it. Whenever a circle is popped, the player will earn 1 point
-- which is stored in a text file.
-------------------------------------------------------------------------------------------------------------------------------------------

If you are brand new to Corona and Lua, the double dash — represents a commented line. This means that the line will not be read when the program runs and commenting your code is a good practice to get into now so your code makes more sense later. Next, we will want to hide that ugly status bar. Who wants to see that in a game anyways?

-------------------------------------------------------------------------------------------------------------------------------------------
-- HIDE THE STATUS BAR
-------------------------------------------------------------------------------------------------------------------------------------------
display.setStatusBar( display.HiddenStatusBar )

After we’ve hidden the status bar, we will want to require in two modules – storyboard and json. Storyboard is used to manage scenes within Corona and json will be used to allow us to save and load data from a text file.

-------------------------------------------------------------------------------------------------------------------------------------------
-- REQUIRE MODULES
-- We will require in modules that will be used within main.lua.
-------------------------------------------------------------------------------------------------------------------------------------------
-- Include the Corona "storyboard" module
local storyboard = require "storyboard"

-- Include the json library to enable file saving.
local json = require "json"

Then, we will create a function that saves information to a text file. To someone starting out with Corona SDK for the very first time, this function may look a little scary and I’ve tried to comment out the code in bite sized chunks to explain what’s going on. If you are anything like me, you learn more by doing instead of reading.

However, just as a quick overview, the function saveTable accepts two arguments – a table and a file name – and proceeds to open the file in the documents directory. Once the file is open, the table is saved to the specified file and then it’s closed. This function will be accessible throughout our game.

-------------------------------------------------------------------------------------------------------------------------------------------
-- SAVING INFORMATION TO A TEXT FILE
-- The saveTable function will save a table to a text file. If the file does not exist, then it will create one if it does not exist
-------------------------------------------------------------------------------------------------------------------------------------------
function saveTable(t, filename)

-- First, we will specify a path. The documents directory is used to store files that need to be permanent such as achievements or high score.
local path = system.pathForFile( filename, system.DocumentsDirectory)

-- Next, we will open the path for writing. By passing "w", we are telling Corona to create a new file if it doesnt exist.
local file = io.open(path, "w")

if file then
-- Then, we encode the table using json.
local contents = json.encode(t)
-- Put the contents into the file.
file:write(contents)
-- Close the file.
io.close(file)
-- Return true to signify that we have successfully saved a file. Otherwise, return false.
return true
else
return false
end
end

Since we are saving information, we also need a way to retrieve information! Inside the main.lua file, we will create another function called loadTable. This function will accept one argument – the file name – and return that information as a table. (Don’t worry, we’ll see this in action pretty soon.) Just like before I’ve commented out the function to make it digestible.

-------------------------------------------------------------------------------------------------------------------------------------------
-- GETTING INFORMATION FROM A TEXT FILE
-- The loadTable function will get the contents of a .json file and return it as a table.
-------------------------------------------------------------------------------------------------------------------------------------------
function loadTable(filename)

-- First, define the path just like the saveTable function.
local path = system.pathForFile( filename, system.DocumentsDirectory)

-- This variable will hold the contents of our file.
local contents = ""

-- Create a table that will be returned from the function.
local myTable = {}

-- Try to open up the file.
local file = io.open( path, "r" )

-- If the file exists...
if file then

-- Then get all of the contents of the file and put it into the variable contents.
local contents = file:read( "*a" )

-- Next, decode the contents with json.decode (We have to do this step because we are saving files using json).
myTable = json.decode(contents);

-- Close the file.
io.close( file )

-- Send back the contents of the table. Otherwise, return nil or nothing.
return myTable
end
return nil
end

Now that we have our saveTable and loadTable working, we will load our information for the first time into the table savedInfo. If the file does not exist, it will created on the fly. This is an important step because we are essentially checking if the player has ever played our game before. If they have played before, then the file already exists in the document directory. If they haven’t played, then our function will create the file.

-------------------------------------------------------------------------------------------------------------------------------------------
-- LOADING INFORMATION
-- This will load information from our saved file into a table. If the file does not exist, it will be created.
-------------------------------------------------------------------------------------------------------------------------------------------
-- We load the file using loadTable(filename) into a table.
savedInfo = loadTable("savedInfo.json")

After we’ve loaded the table, we need to check its contents. If the table is nil, we will know that the player has never played our game before and we need to create some information for them. In this case, we are creating a new table for our player and adding the property circlesPopped. This property will track the number of circles the player has popped throughout the game.

If the savedInfo table is not nil, then that means our player has already played our game and we want to skip this step. If we didn’t skip this step, we would be resetting our player score to 0 every time. And some players don’t like that.

-- If the file does not have any contents, our code will start the player off with 0.
if(savedInfo == nil) then
local myTable = {}
myTable.circlesPopped = 0
saveTable(myTable, "savedInfo.json")
end

Our last step in main.lua is to switch the scene to the main menu scene. Basically, Corona treats different parts of the game as different scenes. main.lua is one scene, the main menu screen is another screen and the game play is a totally different scene. To switch scenes, we will use the storyboard module (remember that we required this at the beginning of the file) to switch scenes by using storyboard.gotoScene.

-------------------------------------------------------------------------------------------------------------------------------------------
-- SWITCHING SCENES
-- This will send our player to the first scene of our app.
-------------------------------------------------------------------------------------------------------------------------------------------
storyboard.gotoScene( "mainmenu" )

And that’s it to part 3 of creating circle pop! So far, we’ve learned about the game, set the game up, and created the main.lua file. In Part 4, we will learn how to create the main menu scene and take advantage of the functions that we’ve created in Part 3. Thank you again for reading another post by That’s So Panda! If you have questions or comments about this series, please leave them below. Otherwise, stay tuned for Part 4!


Leave a Reply