Corona Cannon – a new sample game

Corona Cannon – a new sample game

A long time ago, a demo was made for Corona SDK by Jonathan and Biffy Beebe called Ghosts vs. Monsters (GvM). It was a simple clone of a very popular game of that time – Angry Birds.

Screen Shot 2016-01-29 at 7.19.02 PM

Updating the game for today

Five years have passed since then. The original GvM was developed for Graphics v1, it only supported 480×320 or 960×640 resolution screens, and the code didn’t follow current best practices.

For instance, there were many global variables, the project didn’t use OOP (Object Oriented Programming practices), and the code wasn’t DRY (Don’t Repeat Yourself). Each level needed a new levelN.lua file, which repeated the entire logic of the game plus level layout.

The project wasn’t perfect, but it was a great example 5 years ago. Director class was the only scene management choice at the time and there was no way to organize projects with sub-directories. Dark times.

After the acquisition of Corona Labs by Perk, and with addition of new hires, it was time that we made a fresh new clone! Or a remake of the clone. Or is it a clone’s clone? Too many clones… Let’s just call it a remake.

The “remake” started as a simple code update to the original game, to make it compatible with modern versions of Corona SDK. As a side note, it was actually my first task at Corona Labs as a new hire. Initially, I started updating the code, but quickly realized it was not enough. Developers would not fully benefit from simple code update. We needed a game that shows best practices. So, I created a new game with similar features, from scratch.

The old graphics weren’t made for high resolutions, so we decided to use free assets by kenney.nl, which are great for prototyping. I also wanted to use Tiled to showcase how to integrate it into a Corona game. I wanted to integrate a simple level editor too. It also needed to be playable on all platforms including OS X, Windows desktop and Apple TV, so controller support was a must.

Screen-Shot-w-badge

As for the new game theme, I came up with an idea to fire Corona Labs and Perk logos from a cannon, like cannonballs. We can’t fight pigs, right? No unnecessary animal cruelty… So what can we fight with in Corona? Why, bugs of course! Corona Debugger could have been a cool name for this game, but we settled down on Corona Cannon instead.

During development, some fixes and improvements were also introduced in Corona. The easing.continuousLoop was fixed, and Apple tvOS remote support was improved, as was desktop support.

Fun fact

Ghost vs. Monsters: 11 files, 1080 blank lines, 771 comment lines, 3487 code lines.
Corona Cannon: 21 files, 320 blank lines, 179 comment lines, 2288 code lines.

With considerably fewer lines of code, Corona Cannon is much more feature rich. Less code lines per file also means it’s much easier for a new developers to understand the project.

To use the demo, you will need to use daily build 2016.2818 or later.

Code structure

At the root of the project, you will find sub-directories, icons and standard files like build.settings, config.lua and main.lua. Here is the short directory structure with descriptions:

CoronaCannon
├── classes/ - components of the game, simple OOP approach.
│ ├── ball.lua - cannonball that flies and destroys bugs.
│ ├── block.lua - building blocks of the structures where bugs are hiding.
│ ├── bug.lua - main enemy in the game.
│ ├── cannon.lua - launches balls.
│ ├── end_level_popup.lua - dialog window that is shown at the end of a level.
│ ├── level_editor.lua - simple shortcut based in-game level editor.
│ ├── puff.lua - white or yellow cloud to represent dust or an explosion.
│ ├── shade.lua - dims the screen and doesn’t let touch/tap events through.
│ └── sidebar.lua - pause menu and settings bar.
├── images/ - all in-game images go here.
│ ├── ammo/ - cannonballs.
│ ├── background/ - sprites for close background elements, used in Tiled.
│ ├── background_flat/ - sprites for distant background elements, used in Tiled.
│ ├── blocks/ - building blocks.
│ │ ├── stone/ - stone material.
│ │ └── wood/ - wood material.
│ ├── buttons/ - all the buttons.
│ ├── controls/ - instructions on various controls like a gamepad or a touchsceeen.
│ ├── green_tiles/ - level tiles sprites, used in Tiled.
│ ├── puffs/ - white clouds.
│ └── … - all the other images used in the game.
├── levels/ - 10 levels of the game.
├── libs/ - reusable libraries.
│ ├── controller.lua - gamepads, Apple TV Remote, keyboard.
│ ├── databox.lua - persistent storage of user data.
│ ├── eachframe.lua - enterFrame manager.
│ ├── relayout.lua - reposition elements on the screen on resize event.
│ ├── sounds.lua - sounds and music manager.
│ └── tiled.lua - Tiled importer.
├── maps/ - Tiled maps and their Lua exports.
├── scenes/ - composer scenes.
│ ├── game.lua - main gameplay.
│ ├── level_select.lua - select one level.
│ ├── menu.lua - start screen.
│ └── reload_game.lua - “Loading...” scene, required for restarting a level.
├── sounds/ - all the sounds and music files.
├── build.settings - important platform specific settings.
├── config.lua - extended letterbox scaling, defaults to 640x960.
├── main.lua - entry point for every Corona app.
├── prepare_icons.sh - command line script to generate icons from 1024px files.
└── … - Icons and launchscreens.

Dive into the code starting from main.lua and study the project based on its comments. We will be providing a detailed video that explains the code. Stay tuned for that!

Moving forward, all new demos will be published in our special GitHub directory https://github.com/coronalabs-samples

Conclusion

Corona Cannon, as a demo project, provides great examples and best practices for both novice and experienced developers. I tried to keep the code structure simple, files small and everything clean overall. There’s no spaghetti code, no global variables, no cryptic variable names, and no useless comments.

Grab the source and start working with your own version or download one of the pre-built versions to see what the game is like.

This demo also runs everywhere. It’s published on Google Play and it’s pending review on the Apple stores (iOS, tvOS, and OS X). We will update this post with the links to all stores as they become available.

Source Code – https://github.com/coronalabs-samples/CoronaCannon
Google Play – https://play.google.com/store/apps/details?id=com.coronalabs.coronacannon
iOS – https://itunes.apple.com/us/app/corona-cannon/id1078778611
tvOS – https://itunes.apple.com/us/app/corona-cannon/id1078778611
OS X desktop – https://itunes.apple.com/us/app/corona-cannon/id1078798116
Amazon – http://www.amazon.com/Corona-Labs-Cannon/dp/B01BEG102A
OUYA – https://www.ouya.tv/game/Corona-Cannon/
Windows Desktop – http://developer.coronalabs.com/sites/default/files/Corona%20Cannon.zip
Windows Phone 8 – https://www.microsoft.com/store/apps/9NBLGGH5PWVN

Feel free to discuss this sample game in the forums!

sergeylerg
25 Comments
  • Thomas Vanden Abeele
    Posted at 23:25h, 02 February

    Now THIS is what Corona really needs! I’m very happy with this. I don’t need it anymore personally, but complete and realistic projects with (pseudo-) OOP and modularized code need to get out there to both guide new devs and demonstrate Corona. Very good work – keep ’em coming!

  • ojnab
    Posted at 03:22h, 03 February

    I agree this is very good stuff to study for Corona newcomers. Well done.

  • Sat
    Posted at 04:37h, 03 February

    Great stuff, thanks for putting this together, looking forward to the video tutorial too.

  • Perry
    Posted at 07:21h, 03 February

    This is excellent, thanks!

  • Muhammad Talha
    Posted at 08:24h, 03 February

    Though I liked this new corona Sample it is really a fun as corona labs always rocks.

  • abramq
    Posted at 15:23h, 03 February

    That’s what I was waiting for! Actually, that’s what I was asking for, here some time ago and two good people helped me with making “game template” – /blog/2015/04/14/tutorial-the-basic-game-template and https://github.com/sekodev/starter_template_coronasdk.
    This one here is complex and complete, thanks once more and please keep the project in developent as Corona SDK is developing 🙂

  • GloryOfThe80s
    Posted at 19:24h, 03 February

    As a newcomer to Corona, this will be very helpful. Likely even more so if an accompanying video is made. Thank you much!

  • Dave Haynes
    Posted at 07:41h, 04 February

    Great job, Sergey. I’m going to look over the code to see if I can glean some bits for my apps…

  • Tony
    Posted at 15:46h, 05 February

    Thank you.
    so waited..

  • rebelkov
    Posted at 09:09h, 11 February

    Great job ! thanks

  • Aleena
    Posted at 23:28h, 16 February

    great post 🙂 This is very good stuff for newcomers in corona.
    Thank !

  • Anam
    Posted at 23:29h, 16 February

    Great !
    I liked this new corona staff 🙂

  • Nischal
    Posted at 23:00h, 18 February

    Why doesnt corona officially support tiled integration, that will make our lives easy 🙂

    • Sergey Lerg
      Posted at 03:57h, 19 February

      Because tile engines are tricky and there is no solution that would suit everyone. With Corona Cannon I showed it is not too difficult to make your own implementation specifically tailored for your needs. So a variety of 3rd party options are fine for now.

  • Vince
    Posted at 10:16h, 26 February

    Will there be a video tutorial or step-by-step guide for this?

    • Sergey Lerg
      Posted at 11:09h, 26 February

      Yes, there is a video guide in development. Stay tuned!

  • test
    Posted at 12:43h, 10 April

    is the video code guide ready?

    • Lerg
      Posted at 13:13h, 11 April

      Hi, the video guide got postponed because of currently more important plugins to do. I will finish it next time I have an opportunity. Sorry for the wait.

  • Jton
    Posted at 06:16h, 18 May

    I get a warning when trying to run the project in corona simulator 2016.2830 about missing a plugin, and then an error when trying to select a level. It seems it has to do with some bit plugin? I’m a corona noob so I’m not sure how to fix this…

    • Rob Miracle
      Posted at 11:20h, 18 May

      Do you have the specific error message? Are there any additional messages in the console log?

  • Mike Kelly
    Posted at 08:57h, 06 June

    absolutely brilliant!!! thanks! my students will LOVE this!

  • Ashu
    Posted at 01:46h, 27 July

    Hi All,

    i am new in corona how can we get the gravity of an object like we want to rotate a circle on gravity part of another circle like in “one more line” game.

    Thanks

  • Michael
    Posted at 12:50h, 31 August

    How do I fix this error?

    Error loading module ‘plugin_iCloud’ from file ‘/Users/michaelharrison/Library/Application Support/Corona/Simulator/Plugins/plugin_iCloud.dylib’:
    dlopen(/Users/michaelharrison/Library/Application Support/Corona/Simulator/Plugins/plugin_iCloud.dylib, 2): Symbol not found: _CoronaEventIsErrorKey
    Referenced from: /Users/michaelharrison/Library/Application Support/Corona/Simulator/Plugins/plugin_iCloud.dylib
    Expected in: flat namespace
    in /Users/michaelharrison/Library/Application Support/Corona/Simulator/Plugins/plugin_iCloud.dylib

    File: error loading module ‘plugin_iCloud’ from file ‘/Users/michaelharrison/Library/Application Support/Corona/Simulator/Plugins/plugin_iCloud.dylib’

    • Rob Miracle
      Posted at 11:07h, 01 September

      Have you included it in your build.settings?
      Have you activated it in the Marketplace?

      This would probably be better solved in the forums (https://forums.coronalabs.com) since we will likely need to see code at some point and code doesn’t format well in blog comments.

  • Lar
    Posted at 18:08h, 02 September

    Still no video…to go with this..?