A Complete Game Made with Corona SDK - Tutorial Part 4

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. 512x512, 57x57, and 114x114.

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