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.

Sound

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.

Conclusion

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.  ↩
Share
  • omx

    hi!
    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!
    mike

  • http://cutemachine.com Joe

    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

    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

    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

    Hope that made sense. 8)

  • http://cutemachine.com Joe

    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.

  • http:grimlog.com Allen Baker

    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)

  • http://cutemachine.com Joe

    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

    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?

  • Pingback: Corona SDK Faydalı Linkler | Ramazan Akman

  • Devin

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

  • http://www.calendee.com Justin Noel

    Joe,

    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.

    https://github.com/calendee/fat-freddy

    Thanks,
    Justin

  • http://cutemachine.com Joe

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

  • http://www.thatssopanda.com Daniel

    Great tutorial! Love this!

  • http://www.guyduff.com Guy

    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.

  • http://cutemachine.com Joe

    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.

  • http://www.guyduff.com Guy

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

  • http://onestopdesktop.com Mike Kelly

    brilliant from start to finish! THANKS!!!

  • Vic

    Awesome!

  • Katie

    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.

    So:

    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.

    Regards

    Katie

  • http://www.digitaltutors.com/11/index.php Graw

    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

    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

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

  • admin

    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.

    Best
    Jo

  • Jack Sarraffe

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

  • Stephen

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

  • Guy

    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

    Hey, Nice Great Tutorial !!!! :D

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

  • admin

    Sure. It runs also on iOS.

  • emmalyn

    hello..
    I am new to corona sdk and got this mobile apps project for Map Guide of our province for the tourist.. could someone help me to do this… i badly need help