A Complete Game Made with Corona SDK – Tutorial Part 4

by Jo Meenen | Click to Follow Him on Twitter

Welcome back to the last part of the Corona SDK tutorial series. You might want to consider reading part one, part two, and part three first.

In this part of the programming tutorial we will add the game logic, a menu, and the sound.

The Code and Assets

Before we go through the code, download the complete Fat Freddy Corona SDK Tutorial Project. This bundle includes the following assets:

  • Sound files pickup.wav, powerup.wav, failed.wav
  • Source files build.settings, config.lua and main.lua
  • Adobe Fireworks file FatFreddyLogoFireworks.png[1]
  • Icon files in various sizes. 512×512, 57×57, and 114×114.

Unpack the zip file somewhere on your hard disk. To start the game, just open the main.lua file in your Corona SDK Simulator.

A word of caution. In a real project you would do a lot of things differently from what I have done in this tutorial. For example, for simplicity we only have one code file. In a bigger project this will bring trouble. It is much better to have different source files for various parts of the project. Have a look at the Director Class for the Corona SDK by Ricardo Rauber, to get a good start on structuring your next Corona SDK project.

You can do whatever you like with the project and the included files. Tweak it and learn. Let me know your achievements.

In the code you will find strange comments surrounding the function bodies: ---{{{ and ---}}}. These comments are there to support code folding for the Vim editor. If you use a different editor you can ignore these tags.

The Game Logic

The objective of the game is to collect the black squares and avoid touching the blue squares. The rotating square is our player – fat freddy. Freddy needs to collect the black squares and at the same time he needs to avoid touching the blue squares. This gets increasingly difficult as freddy will get bigger while eating the black squares. For each square freddy eats, he will get one point.

To add more fun to the game we will add rewards and penalties. Rewards will be triggered when fat freddy eats one of the black circles. These are the possible rewards:

  • All You Can Eat As long as the timer runs you can also eat blue squares.
  • Traffic Jam This will slow down the game. It is much easier to avoid the blue squares.
  • Weight Loss This will minify the player. Which is good as it will be easier to avoid the blue squares.

The penalties will be triggered when fat freddy eats a blue circle. The following penalties are supported:

  • Rush Hour This will speed up the game for a limited time. It is hard to avoid the blue squares.
  • Food Contaminated You are not allowed to eat any square. Try to avoid black and blue squares as long as the timer runs.
  • Weight Gain This will increase the size of fat freddy and hence makes it harder to avoid the blue squares.

Watch the video and you will get a good impression of a typical game session.

Code Annotations

After reading the first three parts of the Corona SDK tutorial you should be able to easily understand the code. There is only one minor code construct I have not explained in one of the previous tutorials: closures

Closures are very useful. You can learn about closures in the book Programming in Lua. Closures are blocks of code which can refer to its declaration context. Normally the code blocks are functions, which do not have a name[2]. You can spot these in the project where they are assigned to a variable called closure. In fat freddy these functions are used to reset variables after a transition has been completed.

For those of you who are familiar with closures you will notice that I discard the best part of closures by referencing the variables which would be in scope anyways and not the declaration context. This is another thing you would want to do differently in a real project.


Playing sound with Corona SDK is simple. Just preload a sound like this:

powerUpSound = audio.loadSound("powerup.wav")

Then play the sound with the following code:

audio.play ( powerUpSound  )

The game sounds have been created with the free audio effects tool cfxr. This is a Mac port of the Windows tool sfxr. It is really fun to use. Give it a try.


This completes the game tutorial series. It has become much longer than I planned. So forgive me for not being concise.

Please spread the word about the tutorial and leave your feedback in the comments.

I would be glad to have you as a reader of my RSS feed or as a follower on Twitter. You can also find me on Google+.

Happy coding.

  1. Adobe Fireworks uses the file extension .png!  ↩
  2. Also called anonymous functions.  ↩

{ 28 comments… read them below or add one }

omx September 15, 2011 at 10:49 am

thanks for this tut! it gave me a whole new look of the corona sdk! seriously, it’s a great help for the beginners in lua and corona!

by the way, i’ve not seen fat freddy on the android market, and it IS a nice game to play:) i encourage You to publish it !

thanks again,
keep up the good fight!

Joe September 15, 2011 at 10:04 pm

Hello Mike,

Thanks for your kind comment. It will keep me motivated to publish some more tuts in the future.

fat freddy is available on the App Store since last week and I hope to publish the game on the Android Market Place in November. Will let you know here on CuteMachine when it is available.

russell October 28, 2011 at 4:05 am

thank you for your knowledge, i am a newbie and although i couldn’t quite grasp everything you did, I did get it up and running on I my simulator and am able to follow most of the logic. I will try to see if i can implement some of the rewards you stated but i dont think i can yet. I will keep checking for Fat Freddy on Android! Keep up the great stuff!

Scott April 27, 2012 at 6:52 am

I have mad the squares and circles into graphics, the only thing I am having a problem with is figuring out where to place the .5 size graphics and the *2 graphics, i can get them to come up, but they just stay there, dont move or go away…this is during rush hour and weight loss BTW. ANy help would be awesome. Thanks BTW for this code, you really rock. 8)

Scott April 27, 2012 at 6:52 am

Hope that made sense. 8)

Joe April 27, 2012 at 9:29 am

Scott, thanks for the accolade. I’m glad you are using the code. If you want you can send me the changes you made via email and I will have a look. Unfortunately I’m away from my computer, therefore I can look into it on Wednesday the earliest.

Allen Baker May 8, 2012 at 8:08 am

I used this template and modified the bejeezus out of it.
Thank you so much for doing this my game is Molecule Madness its on googleplay. Its free. I adapted it with graphics and openfeint and music. Joe Meenan you rock. 8)

Joe May 8, 2012 at 12:05 pm

Great work Allen. Love it. And I love the fact that it is based on one of my tutorials. I guess I should write some more. I wish you great success with your game.

Daniel May 19, 2012 at 7:09 pm

In your code you have:
object:setFillColor ( 0, 0, 0, (gameIsOver and 20 or 255) )

I know :setFillColor is r,g,b,alpha. But, can you explain how you have this set up for alpha? Is it if gameIsOver == true, them set the alpha to a random number?

Devin December 9, 2012 at 1:17 am

The game lags and the collision is way off on a tablet. How could you fix this? More importantly the lag when you “eat” a block?.

Justin Noel January 12, 2013 at 1:00 am


Thanks so much for the tutorial. I’m glad to find a nice starting point for learning Corona.

I’ve set your example app up on GitHub for easy access for others. I plan on playing with the code to add new features while learning Corona.

If you are opposed to this, please let me know. I can delete the repository if you object to it.



Joe January 12, 2013 at 5:36 pm

Hey Justin,

Good to hear that the tutorial helped you with getting started. And many thanks for putting the code on GitHub and giving me credits.
I’m curious to see how you build upon it.

I will link back to the repository when I’m working on the blog again.

Good luck with your development.

Daniel March 14, 2013 at 9:40 pm

Great tutorial! Love this!

Guy March 21, 2013 at 1:03 am

First, thanks for putting this up. This tutorial has taught me more about Corona SDK and LUA programming than anything else I have come across yet.

I do wish that the video tutorial would cover the full version of the program. I realize that is a huge amount of time as the remaining code after this final section is at least 5 times greater than the code up to this point. None the less, for the many would be game developers lured by Corona, such a comprehensive tutorial would be immensely productive in my opinion. I found a document that covers the code up to the point where the real game logic begins but unfortunately it doesn’t get into the deeper game logic. While I’ve programmed in database languages like PL/SQL and SQL for many years, this type of programming is very different and in-depth tutorials are invaluable in making the jump. For anyone interested in the PDF file that uses your program as the basis for a written tutorial you check this link. http://campus.dyc.edu/academicsfiles/iprog/fatfreddyinstruct.pdf

In any case, I would be willing for a complete video tutorial which explained the entire complete program and I’m sure others would be too.

Joe March 21, 2013 at 9:16 am

Thank you Guy for your feedback and the kind words about the tutorial.

The PDF you are linking to is really awesome. I’m sure it will help new developers even more than my own tutorial. And whenever I come back and try to understand my own code I will go through your PDF. ;-) Seriously, I love the work you have put into this.

Let us know when you do these video tutorials. I’ will make sure to link to it. Or, if you prefer you can publish a guest post on CuteMachine.

Guy March 22, 2013 at 12:38 am

Joe, I didn’t write the tutorial. It was written by someone campus.dyc.edu.

It may be sometime before I am qualified to do a video tutorial on the full version of the game. Was hoping you might. If ever I do, I’ll post a link here for certain. Again, thanks.

Mike Kelly May 7, 2013 at 1:02 am

brilliant from start to finish! THANKS!!!

Vic June 7, 2013 at 10:21 am


Katie July 17, 2013 at 7:58 pm

Thanks for a great tutorial. Ideal starting point for a novice :-)

I have a few questions about lua and your approach in general. Lua is all new to me.


1) I have seen different ways of declaring a function i.e. local x = function() and local function(x). Could you explain which is the correct way. I tried to change them all the be the same at the end of tutorial 3 but that broke the code? Should a specific code base adopt a given single style ?

2) How do I know if an objects methods should be called using dot or : notation. i.e. display.setStatusBar ( … ) or saveLabel:setTextColor ( … )
This seems inconsistent.

3) Also I was trying to find in the corona reference what x0 means on a display object. I found xOrigin but changing the code to use this breaks it.



Graw December 6, 2013 at 11:54 am

Hey Mr. Joe, How to Setup Different Graphics for “Food” and “Freddy” ?
You got time to make a little freelance work ?
Thanks in advance.:

Jack Sarraffe February 24, 2014 at 11:59 pm

Thanks for this AWSOMe review, I learned a lot. I am a 13 year old app developer and I went through this tutorial and changed this game up a lot, and I need permission for me to puplish it.

Jack Sarraffe February 25, 2014 at 2:30 am

I made a game similar to this and i was wondering if it was ok to publish it

admin February 25, 2014 at 11:22 am

Hello Jack

You can use the code I published for this tutorial in any way you like.

I wish you great success with your game. Let me know where we can find it, so we can check it out.


Jack Sarraffe February 26, 2014 at 12:07 am

OK thank you Joe, if you have any concerns tell me .

Stephen March 11, 2014 at 4:35 pm

How can I replace the black square player with a .png image?

Guy March 19, 2014 at 8:03 pm

I would be willing to pay to see the tutorial completed to include a complete line by line explanation of the entire program in the same way that the current tutorial covers the basic framework. I bet you could sell this tutorial for $20 and move thousands of them because your mode of explanation within the context of such a complex program is very effective. Hope you will consider doing this. If you do make such a comprehensive tutorial, put me down to buy it even at $40. – Thanks

Rahmat June 14, 2014 at 9:03 am

Hey, Nice Great Tutorial !!!! :D

i wanna ask something. Can this apps run on iOS/iPhone ???

admin June 17, 2014 at 9:57 pm

Sure. It runs also on iOS.

Leave a Comment

Notify me of followup comments via e-mail. You can also subscribe without commenting.

{ 1 trackback }